import { NextRequest, NextResponse } from "next/server"; import { attestRegisteredFile, FileRegistryError } from "@/server/file-registry-attestation"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; function failure(status: number, code: string): NextResponse { return NextResponse.json({ code }, { status, headers: { "Cache-Control": "no-store" }, }); } function field(value: unknown): string { return typeof value === "string" ? value.trim() : ""; } /** Return only registry fields re-read with the caller's dc-service JWT. */ export async function POST(request: NextRequest): Promise { try { if (Number(request.headers.get("content-length") ?? 0) > 4096) { return failure(413, "FILE_AUTHORITY_REQUEST_TOO_LARGE"); } const body: unknown = await request.json(); if (!body || typeof body !== "object" || Array.isArray(body)) { return failure(400, "FILE_AUTHORITY_REQUEST_INVALID"); } const input = body as Record; const fileSn = field(input.fileSn); const projectId = field(input.projectId); const clsfSystmSn = field(input.clsfSystmSn); if (!fileSn) return failure(400, "FILE_AUTHORITY_REQUEST_INVALID"); const record = await attestRegisteredFile(request, { fileSn, projectId, clsfSystmSn }, { requireHeader: true }); return NextResponse.json(record, { headers: { "Cache-Control": "no-store" } }); } catch (error) { if (error instanceof FileRegistryError) return failure(error.status, error.code); if (error instanceof SyntaxError) return failure(400, "FILE_AUTHORITY_REQUEST_INVALID"); return failure(502, "FILE_AUTHORITY_UNAVAILABLE"); } }