Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/cost/report/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Vrai-Prix — méthode du coût : rapport PDF « Rapport — Méthode du coût » .43// GET ?estimate=<uuid> → estimation sauvegardée ; POST {input} → calcul sans sauvegarde.44import { NextRequest, NextResponse } from "next/server";45import { getEstimate, runEstimate, sanitizeInput } from "@/lib/cost/estimate";46import { buildCostReport } from "@/lib/report-cost";47import type { CostEstimate } from "@/lib/cost/types";4849export const runtime = "nodejs";50export const maxDuration = 60;5152function slugOf(est: CostEstimate): string {53 const base = est.input.address ?? est.input.municipality ?? `${est.input.building.type}-${Math.round(est.input.building.grossFloorAreaSqft)}pi2`;54 return (55 base56 .toLowerCase()57 .normalize("NFD")58 .replace(/[̀-ͯ]/g, "")59 .replace(/[^a-z0-9]+/g, "-")60 .replace(/^-+|-+$/g, "") || "batiment"61 );62}6364async function respond(est: CostEstimate): Promise<Response> {65 const pdf = await buildCostReport(est);66 return new NextResponse(new Uint8Array(pdf), {67 headers: {68 "Content-Type": "application/pdf",69 "Content-Disposition": `attachment; filename="vrai-prix-cout-${slugOf(est)}.pdf"`,70 "Cache-Control": "private, max-age=300",71 },72 });73}7475export async function GET(req: NextRequest) {76 const id = req.nextUrl.searchParams.get("estimate") ?? "";77 if (!/^[0-9a-f-]{36}$/i.test(id)) return NextResponse.json({ error: "paramètre estimate (uuid) requis" }, { status: 400 });78 try {79 const est = getEstimate(id);80 if (!est) return NextResponse.json({ error: "estimation introuvable" }, { status: 404 });81 return await respond(est);82 } catch (e) {83 return NextResponse.json({ error: (e as Error).message }, { status: 500 });84 }85}8687export async function POST(req: NextRequest) {88 let body: unknown;89 try { body = await req.json(); } catch { return NextResponse.json({ error: "JSON invalide" }, { status: 400 }); }90 try {91 const input = sanitizeInput((body as { input?: unknown })?.input ?? body);92 const est = runEstimate(input, { save: false });93 return await respond(est);94 } catch (e) {95 return NextResponse.json({ error: (e as Error).message }, { status: 500 });96 }97}98