75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
import { AttachmentFileType, AttachmentCategory } from "@prisma/client";
|
|
|
|
function requireRole(role: string) {
|
|
return ["ADMIN", "PRODUCER", "SUPERVISOR"].includes(role);
|
|
}
|
|
|
|
// 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)));
|
|
}
|
|
|
|
// GET /api/shoot-log/takes/[takeId]/attachments
|
|
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 attachments = await db.takeAttachment.findMany({
|
|
where: { takeId },
|
|
orderBy: { sortOrder: "asc" },
|
|
include: { uploadedBy: { select: { id: true, name: true } } },
|
|
});
|
|
|
|
return NextResponse.json(bigIntSafe({ attachments }));
|
|
}
|
|
|
|
// POST /api/shoot-log/takes/[takeId]/attachments
|
|
export async function POST(
|
|
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 { fileUrl, fileKey, fileName, fileSize, fileType, category, caption } = body;
|
|
|
|
if (!fileUrl || !fileName) {
|
|
return NextResponse.json({ error: "fileUrl and fileName required" }, { status: 400 });
|
|
}
|
|
|
|
const last = await db.takeAttachment.findFirst({
|
|
where: { takeId },
|
|
orderBy: { sortOrder: "desc" },
|
|
});
|
|
|
|
const attachment = await db.takeAttachment.create({
|
|
data: {
|
|
takeId,
|
|
fileUrl,
|
|
fileKey: fileKey ?? "",
|
|
fileName,
|
|
fileSize: fileSize ? BigInt(fileSize) : undefined,
|
|
fileType: (fileType as AttachmentFileType) ?? "IMAGE",
|
|
category: (category as AttachmentCategory) ?? "MISCELLANEOUS",
|
|
caption: caption ?? null,
|
|
sortOrder: (last?.sortOrder ?? -1) + 1,
|
|
uploadedById: session.user.id,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(bigIntSafe({ attachment }), { status: 201 });
|
|
}
|