SPB Git

Repository Public

route.ts 3.7 KB 78 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/app/api/cost/assemblies/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 : catalogue des assemblages avec coût unitaire décomposé (localisation au choix).43import { NextRequest, NextResponse } from "next/server";44import { buildContext } from "@/lib/cost/catalog";45import { assemblyUnitCost } from "@/lib/cost/engine";46import { cached } from "@/lib/cost/cache";4748export const runtime = "nodejs";4950export function GET(req: NextRequest) {51  const loc = req.nextUrl.searchParams.get("location") ?? "QC-MTL";52  const q = (req.nextUrl.searchParams.get("q") ?? "").trim().toLowerCase();53  const cat = req.nextUrl.searchParams.get("category");54  try {55    const data = cached(`assemblies:${loc}`, 10 * 60_000, () => {56      const ctx = buildContext({ locationCode: loc });57      const f = ctx.location;58      return {59        asOf: ctx.asOf, location: f,60        assemblies: [...ctx.assemblies.values()].map((a) => {61          const u = assemblyUnitCost(a, ctx);62          return { code: a.code, nameFr: a.nameFr, nameEn: a.nameEn, descriptionFr: a.descriptionFr, descriptionEn: a.descriptionEn, unit: a.unit, category: a.category, masterformat: a.masterformat, quality: a.quality, economicLife: a.economicLife, conditionGroup: a.conditionGroup,63            material: u.material, labour: u.labour, equipment: u.equipment, direct: u.direct,64            adjusted: Math.round(100 * (u.material * f.materialFactor + u.labour * f.labourFactor + u.equipment * f.equipmentFactor)) / 100,65            sigmaPct: Math.round(u.sigmaPct * 1000) / 1000, observedShare: Math.round(u.observedShare * 100) / 100, freshestObservation: u.freshestObservation, components: a.components.length };66        }),67      };68    });69    const fold = (s: string) => s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase();70    let list = data.assemblies;71    if (q) list = list.filter((a) => fold(a.nameFr).includes(fold(q)) || fold(a.nameEn).includes(fold(q)) || a.code.toLowerCase().includes(q));72    if (cat) list = list.filter((a) => a.category === cat);73    return NextResponse.json({ asOf: data.asOf, location: data.location, count: list.length, assemblies: list });74  } catch (e) {75    return NextResponse.json({ error: (e as Error).message }, { status: 500 });76  }77}78
Files main
Menu

Theme