Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/indice/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-11-2612 * Last Modified : 2026-11-2613 *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// GET /api/indice[?type=unifamilial|condo|plex|indéterminé] — indice de marché43// mensuel par type de transaction (base : le DERNIER mois vaut 1,0 ; un mois à44// 0,80 signifie des prix 20 % sous le niveau courant), prix médian au m² et45// nombre de ventes du mois. Sert aux ajustements « conditions de marché » des46// comparables et aux graphiques de tendance.47import { NextRequest } from "next/server";48import { marketIndexRows } from "@/lib/db";49import { cache, erreur, json } from "@/lib/api";5051export const dynamic = "force-dynamic";5253const TYPES = ["unifamilial", "condo", "plex", "indéterminé"];5455export function GET(req: NextRequest) {56 const type = req.nextUrl.searchParams.get("type");57 if (type && !TYPES.includes(type)) return erreur(400, `type inconnu : ${TYPES.join(", ")}`, "bad_type");58 try {59 const rows = marketIndexRows(type ?? undefined);60 const series: Record<string, { month: string; idx: number; ppm2: number | null; n: number }[]> = {};61 for (const r of rows) (series[r.type_prop] ??= []).push({ month: r.month, idx: r.idx, ppm2: r.ppm2, n: r.n });62 const resume = Object.entries(series).map(([t, pts]) => {63 const dernier = pts[pts.length - 1];64 const ilYaUnAn = pts.length >= 13 ? pts[pts.length - 13] : null;65 const premier = pts[0];66 return {67 type: t,68 from: premier.month,69 to: dernier.month,70 variation12mPct: ilYaUnAn && ilYaUnAn.idx > 0 ? (dernier.idx / ilYaUnAn.idx - 1) * 100 : null,71 variationDepuisDebutPct: premier.idx > 0 ? (dernier.idx / premier.idx - 1) * 100 : null,72 ppm2: dernier.ppm2,73 };74 });75 return json({ base: "dernier mois = 1,0", types: resume, series }, cache(3600));76 } catch (err) {77 return erreur(500, (err as Error).message);78 }79}80