import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/auth"; import { db } from "@/lib/db"; function requireRole(role: string) { return ["ADMIN", "PRODUCER", "SUPERVISOR"].includes(role); } // GET /api/shoot-log/days/[dayId]/setups export async function GET( _req: NextRequest, { params }: { params: Promise<{ dayId: string }> } ) { const session = await auth(); if (!session?.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); if (!requireRole(session.user.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); const { dayId } = await params; const setups = await db.setup.findMany({ where: { shootDayId: dayId }, orderBy: { sortOrder: "asc" }, include: { takes: { orderBy: { takeNumber: "asc" }, include: { _count: { select: { attachments: true } } }, }, }, }); return NextResponse.json({ setups }); } // POST /api/shoot-log/days/[dayId]/setups export async function POST( req: NextRequest, { params }: { params: Promise<{ dayId: string }> } ) { const session = await auth(); if (!session?.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); if (!requireRole(session.user.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); const { dayId } = await params; const body = await req.json(); const { name, description } = body; if (!name) return NextResponse.json({ error: "name required" }, { status: 400 }); const last = await db.setup.findFirst({ where: { shootDayId: dayId }, orderBy: { sortOrder: "desc" }, }); const setup = await db.setup.create({ data: { shootDayId: dayId, name, description: description ?? null, sortOrder: (last?.sortOrder ?? -1) + 1, }, include: { takes: { orderBy: { takeNumber: "asc" }, include: { _count: { select: { attachments: true } } } } }, }); return NextResponse.json({ setup }, { status: 201 }); }