@@ -0,0 +1,72 @@
|
||||
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?projectId=xxx
|
||||
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 });
|
||||
|
||||
const days = await db.shootDay.findMany({
|
||||
where: { projectId },
|
||||
orderBy: { date: "desc" },
|
||||
include: {
|
||||
setups: {
|
||||
orderBy: { sortOrder: "asc" },
|
||||
include: {
|
||||
takes: {
|
||||
orderBy: { takeNumber: "asc" },
|
||||
include: {
|
||||
_count: { select: { attachments: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
createdBy: { select: { id: true, name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ days });
|
||||
}
|
||||
|
||||
// POST /api/shoot-log/days
|
||||
export async function POST(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 body = await req.json();
|
||||
const { projectId, date, unit, label, notes } = body;
|
||||
if (!projectId || !date) {
|
||||
return NextResponse.json({ error: "projectId and date required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const day = await db.shootDay.create({
|
||||
data: {
|
||||
projectId,
|
||||
date: new Date(date),
|
||||
unit: unit ?? "A",
|
||||
label: label ?? null,
|
||||
notes: notes ?? null,
|
||||
createdById: session.user.id,
|
||||
},
|
||||
include: {
|
||||
setups: {
|
||||
orderBy: { sortOrder: "asc" },
|
||||
include: { takes: { orderBy: { takeNumber: "asc" }, include: { _count: { select: { attachments: true } } } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ day }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user