import type { NextRequest } from "next/server"; import { gateSession } from "@/auth/api"; import { getRunSheet } from "@/db/runSheet"; import { boothSheetFilename, buildBoothSheetPdf } from "@/lib/booth-sheet-pdf"; import { errorResponse } from "@/lib/http"; /** * GET /api/run-sheet/:instanceId/pdf * * The printable booth sheet as a real PDF. Any signed-in team member can pull * it (the run sheet itself is team-visible); it is generated from the current * database state each time, so it can never show a stale team. */ export const GET = async ( _req: NextRequest, { params }: { params: Promise<{ instanceId: string }> }, ) => { const gate = await gateSession(); if (gate instanceof Response) return gate; const { instanceId } = await params; const sheet = await getRunSheet(instanceId); if (!sheet) return errorResponse(404, "unknown run sheet"); try { const pdf = await buildBoothSheetPdf(sheet); const filename = boothSheetFilename(sheet).replace(/[^A-Za-z0-9._-]/g, "_"); return new Response(new Uint8Array(pdf), { status: 200, headers: { "content-type": "application/pdf", "content-disposition": `attachment; filename="${filename}"`, "cache-control": "private, no-store", "x-content-type-options": "nosniff", }, }); } catch (error) { console.error("[run-sheet-pdf] build failed", error); return errorResponse(500, "could not build the booth sheet"); } };