SPB Git

Repository Public

route.ts 5.4 KB 97 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | ACADEMIC RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : UQO Éval10 * File             : src/app/api/metre/rsmeans/route.ts11 * Module           : app12 * Repository       : https://git.spboucher.ai/uqo-eval13 * Project Website  : https://www.uqo-eval.app14 * Created          : 2026-10-2815 * Last Modified    : 2026-10-2816 *17 * =============================================================================18 * SOFTWARE MAINTAINER & AFFILIATION19 * =============================================================================20 *21 * Maintainer       : Simon-Pierre Boucher22 * Position         : Professor of Finance23 * Institution      : Université du Québec en Outaouais (UQO)24 * Department       : Département des sciences administratives25 * Website          : https://spboucher.ai26 * Git              : https://git.spboucher.ai27 *28 * Research Contact : research@spboucher.ai29 * Direct Contact   : simon@spboucher.ai30 * Security/Admin   : admin@spboucher.ai31 *32 * The affiliation is descriptive. It is not an institutional endorsement,33 * a license grant or a determination of ownership.34 *35 * =============================================================================36 * COPYRIGHT, AUTHORSHIP & DATA37 * =============================================================================38 *39 * Copyright (c) the author(s) of this work.40 * Ownership is not asserted by this notice; see the repository records.41 *42 * This header identifies the maintainer of the software. Scientific43 * authorship of any associated paper is stated in the paper itself and44 * is not changed by this notice. Datasets and third-party materials45 * remain the property of their respective owners and licensors.46 *47 * This file is governed by the applicable project license, copyright48 * notices, institutional agreements and relevant law.49 *50 * =============================================================================51 * LICENSE & MAINTENANCE52 * =============================================================================53 *54 * License          : None specified (no license file in this repository)55 * Classification   : Public56 *57 * =============================================================================58 * END OF SP BOUCHER SOURCE HEADER59 * =============================================================================60 */6162// UQO Éval — Quantity Survey : données RSMeans localisées Québec — recherche de lignes, prix par ville / type de MO, facteurs, métadonnées.63//   GET ?meta=1                                   → métadonnées, localisations, livres, types de MO, divisions64//   GET ?q=…&division=03&loc=238&labor=STD&limit=50 → lignes + prix pour la localisation (loc=auto&project=<id> : ville du projet)65//   GET ?line=061110400100&labor=…                 → détail d'une ligne + prix dans toutes les localisations66//   GET ?factors=238                               → facteurs de localisation par division (ville échantillonnée ou validation)67import { NextRequest, NextResponse } from "next/server";68import { HttpError, handle, requireUser } from "@/lib/metre/auth";69import { RSM_DIVISIONS, RSM_LABORS, type RsmLabor, rsmFactors, rsmLine, rsmLocationForProject, rsmLocations, rsmMeta, rsmPrice, rsmSearch, rsmUnit, rsmeansAvailable } from "@/lib/metre/rsmeans";70import { getProject } from "@/lib/metre/projects";7172export const runtime = "nodejs";7374const LAB = (s: string | null): RsmLabor => (s === "OPN" || s === "RES" || s === "FMR" ? s : "STD");7576export async function GET(req: NextRequest) {77  return handle(() => {78    const ctx = requireUser(req, { limit: { key: "rsmeans", max: 600, windowMs: 60_000 } });79    if (!rsmeansAvailable()) return NextResponse.json({ available: false, error: "Base RSMeans non installée." }, { status: 503 });80    const q = req.nextUrl.searchParams;81    if (q.get("meta")) return NextResponse.json({ available: true, meta: rsmMeta(), locations: rsmLocations(), labors: RSM_LABORS, divisions: RSM_DIVISIONS }, { headers: { "Cache-Control": "private, max-age=600" } });82    let loc = Number(q.get("loc") ?? 419) || 419; const labor = LAB(q.get("labor"));83    if (q.get("loc") === "auto") { const pid = q.get("project"); const p = pid && /^[0-9a-f-]{36}$/i.test(pid) ? getProject(ctx.db, pid) : null; loc = p ? rsmLocationForProject(p).id : 419; }84    const factorsOf = q.get("factors"); if (factorsOf) return NextResponse.json({ factors: rsmFactors(Number(factorsOf)) });85    const one = q.get("line");86    if (one) {87      const line = rsmLine(one); if (!line) throw new HttpError(404, "Ligne RSMeans inconnue.");88      const prices = rsmLocations().map((l) => rsmPrice(one, l.id, labor)).filter(Boolean);89      return NextResponse.json({ line, unit: rsmUnit(line.unit), prices });90    }91    const division = q.get("division"); const limit = Math.min(200, Number(q.get("limit") ?? 50) || 50);92    const lines = rsmSearch(q.get("q") ?? "", { division: division && /^\d\d$/.test(division) ? division : null, limit, priced: q.get("all") !== "1" });93    const rows = lines.map((l) => ({ ...l, unitMap: rsmUnit(l.unit), price: l.unit ? rsmPrice(l.line, loc, labor) : null }));94    return NextResponse.json({ loc, labor, rows });95  });96}97
Files main
Menu

Theme