Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/map/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/map?layer=units|sales|listings&bbox=ouest,sud,est,nord[&limit=][&months=]43// Points de carte dans la zone visible.44// units unités d'évaluation avec leur estimation 2026 (défaut)45// sales ventes publiées des `months` derniers mois (défaut 24)46// listings propriétés à vendre avec leur estimation47// La boîte est bornée (0,5° de côté) : au-delà, zoomer. `truncated` signale48// que la limite a été atteinte (les points les plus proches du centre, ou les49// ventes les plus récentes, sont servis d'abord).50import { NextRequest } from "next/server";51import { salesInBbox, unitsInBbox } from "@/lib/db";52import { listingsInBbox } from "@/lib/immoka";53import { vsRolePct } from "@/lib/ventes";54import { cache, entier, erreur, json, lireBbox } from "@/lib/api";5556export const dynamic = "force-dynamic";5758const MAX_COTE_DEG = 0.5;5960function depuisMois(mois: number): string {61 const d = new Date();62 d.setMonth(d.getMonth() - mois);63 return d.toISOString().slice(0, 10);64}6566export function GET(req: NextRequest) {67 const q = req.nextUrl.searchParams;68 const layer = q.get("layer") ?? "units";69 const b = lireBbox(q.get("bbox"));70 if (!b) return erreur(400, "bbox requis : ouest,sud,est,nord (degrés)", "bad_bbox");71 if (b.east - b.west > MAX_COTE_DEG || b.north - b.south > MAX_COTE_DEG) {72 return erreur(400, `zone trop grande (max ${MAX_COTE_DEG}° de côté) : zoomez`, "bbox_too_large");73 }74 try {75 if (layer === "units") {76 const limit = entier(q.get("limit"), 600, 1, 1500);77 const points = unitsInBbox(b.west, b.south, b.east, b.north, limit).map((u) => ({78 id: u.id_provinc,79 adresse: u.adresse,80 apt: u.apt,81 municipalite: u.municipalite,82 typeProp: u.type_prop,83 lat: u.lat,84 lng: u.lng,85 est2026: u.est_2026,86 }));87 return json({ layer, bbox: b, count: points.length, truncated: points.length >= limit, points }, cache(300));88 }89 if (layer === "sales") {90 const limit = entier(q.get("limit"), 500, 1, 1000);91 const months = entier(q.get("months"), 24, 1, 72);92 const since = depuisMois(months);93 const points = salesInBbox(b.west, b.south, b.east, b.north, since, limit).map((t) => ({94 id: t.id,95 date: t.date,96 amount: t.amount,97 street: t.street,98 city: t.city,99 propertyType: t.property_type,100 lat: t.lat,101 lng: t.lng,102 idProvinc: t.id_provinc,103 valeurRole: t.valeur_role,104 vsRolePct: vsRolePct(t.amount, t.valeur_role),105 }));106 return json({ layer, bbox: b, since, count: points.length, truncated: points.length >= limit, points }, cache(300));107 }108 if (layer === "listings") {109 const limit = entier(q.get("limit"), 300, 1, 600);110 const points = listingsInBbox(b.west, b.south, b.east, b.north, limit);111 return json({ layer, bbox: b, count: points.length, truncated: points.length >= limit, points }, cache(120));112 }113 return erreur(400, "layer inconnu : units, sales ou listings", "bad_layer");114 } catch (err) {115 return erreur(500, (err as Error).message);116 }117}118