Repository Public
1// Auteur : Simon-Pierre Boucher — contact@spboucher.ai2/**3 * POST /api/stats/report/custom — v3 : rapport PDF personnalisé4 * (src/ka/stats/SPEC.md §3bis). Corps : {"title", "period", "from", "to",5 * "blocks": [{"key": "series:…", "render": "bar"}, …]} — ordre respecté,6 * clés inconnues ignorées, aucune clé valide → 400.7 */8import { type NextRequest, NextResponse } from "next/server";9import { buildCustomReport, reportFilename, type CustomSpec } from "@/lib/report-stats";1011export async function POST(req: NextRequest) {12 let spec: CustomSpec;13 try {14 spec = (await req.json()) as CustomSpec;15 } catch {16 return NextResponse.json({ error: "Corps JSON invalide" }, { status: 400 });17 }18 try {19 const pdf = await buildCustomReport(spec ?? {});20 return new NextResponse(new Uint8Array(pdf), {21 headers: {22 "Content-Type": "application/pdf",23 "Content-Disposition": `attachment; filename="${reportFilename("personnalise")}"`,24 },25 });26 } catch (e) {27 if (e instanceof Error && e.message === "aucun-bloc") {28 return NextResponse.json({ error: "Aucun bloc valide dans la composition" }, { status: 400 });29 }30 throw e;31 }32}33