73 lines
2.1 KiB
TypeScript
73 lines
2.1 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);
|
|
}
|
|
|
|
// GET /api/shoot-log/recent-values?projectId=xxx
|
|
// Returns recently used values for smart autofill on new takes
|
|
export async function GET(req: NextRequest) {
|
|
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 projectId = req.nextUrl.searchParams.get("projectId");
|
|
if (!projectId) return NextResponse.json({ error: "projectId required" }, { status: 400 });
|
|
|
|
// Fetch the 5 most recent takes for this project to extract autofill candidates
|
|
const recentTakes = await db.take.findMany({
|
|
where: {
|
|
setup: { shootDay: { projectId } },
|
|
},
|
|
orderBy: { createdAt: "desc" },
|
|
take: 5,
|
|
select: {
|
|
cameraModel: true,
|
|
cameraLetter: true,
|
|
resolution: true,
|
|
codec: true,
|
|
fps: true,
|
|
shutter: true,
|
|
iso: true,
|
|
whiteBalance: true,
|
|
colourSpace: true,
|
|
lensSet: true,
|
|
lens: true,
|
|
filters: true,
|
|
isAnamorphic: true,
|
|
weather: true,
|
|
clipName: true,
|
|
},
|
|
});
|
|
|
|
// Return the most recent non-null value for each field
|
|
function latest<T>(field: keyof typeof recentTakes[0]): T | null {
|
|
for (const take of recentTakes) {
|
|
const v = take[field];
|
|
if (v !== null && v !== undefined) return v as T;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
return NextResponse.json({
|
|
cameraModel: latest("cameraModel"),
|
|
cameraLetter: latest("cameraLetter"),
|
|
resolution: latest("resolution"),
|
|
codec: latest("codec"),
|
|
fps: latest("fps"),
|
|
shutter: latest("shutter"),
|
|
iso: latest("iso"),
|
|
whiteBalance: latest("whiteBalance"),
|
|
colourSpace: latest("colourSpace"),
|
|
lensSet: latest("lensSet"),
|
|
lens: latest("lens"),
|
|
filters: latest("filters"),
|
|
isAnamorphic: latest("isAnamorphic"),
|
|
weather: latest("weather"),
|
|
lastClipName: latest("clipName"),
|
|
});
|
|
}
|