import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/auth"; import { db } from "@/lib/db"; export async function GET(req: NextRequest) { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const projectId = req.nextUrl.searchParams.get("projectId"); if (!projectId) { return NextResponse.json({ error: "projectId required" }, { status: 400 }); } // All versions are linked to tasks (taskId), not directly to shots. // We query shots with their tasks, and for each task get the latest version. const shots = await db.shot.findMany({ where: { projectId, tasks: { some: { versions: { some: { isLatest: true } }, }, }, }, orderBy: [{ episode: "asc" }, { scene: "asc" }, { shotNumber: "asc" }], select: { id: true, shotCode: true, episode: true, status: true, shotApprovalStatus: true, sharedWithClient: true, thumbnailUrl: true, tasks: { select: { id: true, title: true, versions: { where: { isLatest: true }, take: 1, orderBy: { createdAt: "desc" }, select: { id: true, versionNumber: true, fileUrl: true, thumbnailUrl: true, posterUrl: true, fps: true, approvalStatus: true, mimeType: true, createdAt: true, }, }, }, }, }, }); // For each shot, pick the most recently uploaded latest version across all tasks const playlist = shots .map((shot) => { const versions = shot.tasks .flatMap((t) => t.versions.map((v) => ({ ...v, taskTitle: t.title }))) .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); const latest = versions[0] ?? null; if (!latest) return null; return { id: shot.id, shotCode: shot.shotCode, episode: shot.episode, status: shot.status, shotApprovalStatus: shot.shotApprovalStatus, sharedWithClient: shot.sharedWithClient, thumbnailUrl: shot.thumbnailUrl, latestVersion: { id: latest.id, versionNumber: latest.versionNumber, fileUrl: latest.fileUrl, thumbnailUrl: latest.thumbnailUrl, posterUrl: latest.posterUrl, fps: latest.fps, approvalStatus: latest.approvalStatus, taskTitle: latest.taskTitle, }, }; }) .filter((s): s is NonNullable => s !== null); return NextResponse.json({ shots: playlist }); }