97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
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);
|
|
}
|
|
|
|
/** Convert BigInt values to Number so JSON.stringify doesn't throw */
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function bigIntSafe(obj: any): any {
|
|
return JSON.parse(JSON.stringify(obj, (_, v) => (typeof v === "bigint" ? Number(v) : v)));
|
|
}
|
|
|
|
function sanitize(data: Record<string, unknown>) {
|
|
const allowed = new Set([
|
|
"scene","shotLabel","unitLabel","cameraLetter","clipName","roll","cameraModel",
|
|
"resolution","codec","fps","shutter","iso","whiteBalance","colourSpace",
|
|
"lensSet","lens","tStop","filters","isAnamorphic",
|
|
"hasHdri","hasChromeBall","hasGreyBall","hasMacbeth","hasCleanPlate",
|
|
"hasSurvey","hasLidar","hasWitnessCamera","hasLensGrid","hasTexturePhotos","hasPhotogrammetry",
|
|
"weather","sunDirection","artificialLights",
|
|
"supervisorNotes","continuityNotes","vfxRequirements","quality",
|
|
]);
|
|
return Object.fromEntries(
|
|
Object.entries(data)
|
|
.filter(([k]) => allowed.has(k))
|
|
.map(([k, v]) => [k, v === "" ? null : v])
|
|
);
|
|
}
|
|
|
|
// GET /api/shoot-log/takes/[takeId]
|
|
export async function GET(
|
|
_req: NextRequest,
|
|
{ params }: { params: Promise<{ takeId: 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 { takeId } = await params;
|
|
|
|
const take = await db.take.findUnique({
|
|
where: { id: takeId },
|
|
include: {
|
|
attachments: { orderBy: { sortOrder: "asc" } },
|
|
setup: {
|
|
include: {
|
|
shootDay: { select: { id: true, date: true, unit: true, label: true, projectId: true } },
|
|
takes: { orderBy: { takeNumber: "asc" }, select: { id: true, takeNumber: true } },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!take) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
return NextResponse.json(bigIntSafe({ take }));
|
|
}
|
|
|
|
// PATCH /api/shoot-log/takes/[takeId]
|
|
export async function PATCH(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ takeId: 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 { takeId } = await params;
|
|
const body = await req.json();
|
|
|
|
const take = await db.take.update({
|
|
where: { id: takeId },
|
|
data: sanitize(body),
|
|
include: { attachments: { orderBy: { sortOrder: "asc" } } },
|
|
});
|
|
|
|
return NextResponse.json(bigIntSafe({ take }));
|
|
}
|
|
|
|
// DELETE /api/shoot-log/takes/[takeId]
|
|
export async function DELETE(
|
|
_req: NextRequest,
|
|
{ params }: { params: Promise<{ takeId: 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 { takeId } = await params;
|
|
await db.take.delete({ where: { id: takeId } });
|
|
return NextResponse.json({ ok: true });
|
|
}
|