import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/auth"; import { db } from "@/lib/db"; import { sortShotsForList } from "@/lib/shot-order"; export async function GET( req: NextRequest, { params }: { params: Promise<{ shotId: string }> } ) { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const { shotId } = await params; const shot = await db.shot.findUnique({ where: { id: shotId }, include: { artist: { select: { id: true, name: true, email: true, image: true } }, footagePlates: { orderBy: { sortOrder: "asc" } }, versions: { orderBy: { versionNumber: "desc" }, include: { artist: { select: { id: true, name: true, email: true, image: true } }, _count: { select: { comments: true } }, approvals: { orderBy: { createdAt: "desc" }, include: { user: { select: { id: true, name: true, image: true } } }, }, }, }, }, }); if (!shot) { return NextResponse.json({ error: "Shot not found" }, { status: 404 }); } const project = await db.project.findUnique({ where: { id: shot.projectId }, select: { name: true, projectType: true, shots: { orderBy: { createdAt: "asc" }, select: { id: true, shotCode: true, episode: true, scene: true, shotNumber: true, shotGroup: { select: { name: true } }, }, }, }, }); // Neighbours in the project page's shot list order, for prev/next navigation const orderedShots = project ? sortShotsForList(project.shots, project.projectType) : []; const shotIndex = orderedShots.findIndex((s) => s.id === shotId); const neighbour = (i: number) => shotIndex >= 0 && orderedShots[i] ? { id: orderedShots[i].id, shotCode: orderedShots[i].shotCode } : null; const shotNav = { prev: neighbour(shotIndex - 1), next: neighbour(shotIndex + 1), position: shotIndex + 1, total: orderedShots.length, }; const [tasks, artists] = await Promise.all([ db.task.findMany({ where: { shotId }, orderBy: { sortOrder: "asc" }, include: { assignedArtist: { select: { id: true, name: true, email: true, image: true } }, _count: { select: { versions: true } }, versions: { take: 1, orderBy: { versionNumber: "desc" }, select: { id: true, versionNumber: true, approvalStatus: true, createdAt: true, approvals: { take: 1, orderBy: { createdAt: "desc" }, select: { id: true, status: true, notes: true, createdAt: true, user: { select: { id: true, name: true, image: true } }, }, }, }, }, }, }), db.user.findMany({ where: { isActive: true }, select: { id: true, name: true, email: true }, orderBy: { name: "asc" }, }), ]); const canApprove = ["ADMIN", "PRODUCER", "SUPERVISOR"].includes( session.user.role as string ); // Serialize BigInt fields (fileSize, highResSize) so JSON.stringify doesn't throw const shotSerialized = { ...shot, footagePlates: shot.footagePlates.map((p) => ({ ...p, fileSize: p.fileSize != null ? p.fileSize.toString() : null, })), versions: shot.versions.map((v) => ({ ...v, fileSize: v.fileSize != null ? v.fileSize.toString() : null, highResSize: v.highResSize != null ? v.highResSize.toString() : null, })), }; return NextResponse.json({ shot: shotSerialized, projectName: project?.name ?? "", shotNav, canApprove, canInternallyApprove: ["ADMIN", "PRODUCER", "SUPERVISOR"].includes(session.user.role as string), tasks, artists, }); }