SPB Git

Repository Public

route.ts 2.9 KB 73 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/app/api/report/route.ts9 * Module           : app10 * Repository       : https://git.spboucher.ai/vrai-prix11 * Created          : 2026-08-0912 * Last Modified    : 2026-08-0913 *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 */4142import { NextRequest, NextResponse } from "next/server";43import { estimateByUnitId } from "@/lib/estimator";44import { buildReport } from "@/lib/report";4546export async function GET(req: NextRequest) {47  const rawId = req.nextUrl.searchParams.get("id");48  if (!rawId) return NextResponse.json({ error: "id requis" }, { status: 400 });49  const id = rawId.replace(/\D/g, "") || rawId; // tolère un id décoré (espaces, tirets)50  // tolère les liens du type ?format=professionnel générés par des agents51  const format = (req.nextUrl.searchParams.get("format") ?? "").toLowerCase();52  if (format.startsWith("pro")) {53    return NextResponse.redirect(new URL(`/api/report/pro?id=${encodeURIComponent(id)}`, req.url));54  }55  const data = estimateByUnitId(id);56  if (!data || !data.unit)57    return NextResponse.json({ error: "unité introuvable" }, { status: 404 });58  const pdf = await buildReport(data);59  const slug = (data.unit.adresse ?? "propriete")60    .toLowerCase()61    .normalize("NFD")62    .replace(/[̀-ͯ]/g, "")63    .replace(/[^a-z0-9]+/g, "-")64    .replace(/^-+|-+$/g, "");65  return new NextResponse(new Uint8Array(pdf), {66    headers: {67      "Content-Type": "application/pdf",68      "Content-Disposition": `attachment; filename="vrai-prix-${slug}.pdf"`,69      "Cache-Control": "private, max-age=300",70    },71  });72}73
Files main
Menu

Theme