Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/cost/items/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 : recherche d'articles (coûts unitaires) avec prix canonique courant.43import { NextRequest, NextResponse } from "next/server";44import { buildContext, loadItems, searchItems } from "@/lib/cost/catalog";45import { cached } from "@/lib/cost/cache";4647export const runtime = "nodejs";4849export function GET(req: NextRequest) {50 const q = (req.nextUrl.searchParams.get("q") ?? "").trim();51 const loc = req.nextUrl.searchParams.get("location") ?? "QC-MTL";52 const cat = req.nextUrl.searchParams.get("category");53 try {54 const ctx = cached(`ctx:${loc}`, 10 * 60_000, () => buildContext({ locationCode: loc }));55 let items = q.length >= 2 ? searchItems(q, 40) : [...loadItems().values()];56 if (cat) items = items.filter((i) => i.category === cat);57 const out = items.slice(0, 200).map((i) => {58 const p = ctx.prices.get(i.code) ?? null;59 return { code: i.code, nameFr: i.nameFr, nameEn: i.nameEn, unit: i.unit, category: i.category, masterformat: i.masterformat, division: i.division, trade: i.trade,60 price: p ? Math.round(p.price * 100) / 100 : null, low: p ? Math.round(p.low * 100) / 100 : null, high: p ? Math.round(p.high * 100) / 100 : null, kind: p?.kind ?? null, sourceCount: p?.sourceCount ?? 0, observedAt: p?.observedAt ?? null, confidence: p?.confidence ?? null, note: p?.note ?? null, sources: p?.sources ?? [] };61 });62 return NextResponse.json({ asOf: ctx.asOf, location: ctx.location.code, count: out.length, items: out });63 } catch (e) {64 return NextResponse.json({ error: (e as Error).message }, { status: 500 });65 }66}67