SPB Git

Repository Public

route.ts 2.1 KB 57 lines typescript

1// Vrai-Prix — méthode du coût : rapport PDF « Rapport — Méthode du coût » .2// GET ?estimate=<uuid> → estimation sauvegardée ; POST {input} → calcul sans sauvegarde.3import { NextRequest, NextResponse } from "next/server";4import { getEstimate, runEstimate, sanitizeInput } from "@/lib/cost/estimate";5import { buildCostReport } from "@/lib/report-cost";6import type { CostEstimate } from "@/lib/cost/types";78export const runtime = "nodejs";9export const maxDuration = 60;1011function slugOf(est: CostEstimate): string {12  const base = est.input.address ?? est.input.municipality ?? `${est.input.building.type}-${Math.round(est.input.building.grossFloorAreaSqft)}pi2`;13  return (14    base15      .toLowerCase()16      .normalize("NFD")17      .replace(/[̀-ͯ]/g, "")18      .replace(/[^a-z0-9]+/g, "-")19      .replace(/^-+|-+$/g, "") || "batiment"20  );21}2223async function respond(est: CostEstimate): Promise<Response> {24  const pdf = await buildCostReport(est);25  return new NextResponse(new Uint8Array(pdf), {26    headers: {27      "Content-Type": "application/pdf",28      "Content-Disposition": `attachment; filename="vrai-prix-cout-${slugOf(est)}.pdf"`,29      "Cache-Control": "private, max-age=300",30    },31  });32}3334export async function GET(req: NextRequest) {35  const id = req.nextUrl.searchParams.get("estimate") ?? "";36  if (!/^[0-9a-f-]{36}$/i.test(id)) return NextResponse.json({ error: "paramètre estimate (uuid) requis" }, { status: 400 });37  try {38    const est = getEstimate(id);39    if (!est) return NextResponse.json({ error: "estimation introuvable" }, { status: 404 });40    return await respond(est);41  } catch (e) {42    return NextResponse.json({ error: (e as Error).message }, { status: 500 });43  }44}4546export async function POST(req: NextRequest) {47  let body: unknown;48  try { body = await req.json(); } catch { return NextResponse.json({ error: "JSON invalide" }, { status: 400 }); }49  try {50    const input = sanitizeInput((body as { input?: unknown })?.input ?? body);51    const est = runEstimate(input, { save: false });52    return await respond(est);53  } catch (e) {54    return NextResponse.json({ error: (e as Error).message }, { status: 500 });55  }56}57
Files main
Menu

Theme