SPB Git

Repository Public

route.ts 4.2 KB 77 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/app/api/admin/cost/mappings/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 — admin méthode du coût : correspondances produit ↔ article (liste, création manuelle).43import { NextRequest, NextResponse } from "next/server";44import { adminAuth } from "@/lib/cost/connectors/admin";45import { getCostDb, sourceIdByKey } from "@/lib/cost/db";46import { reviewMapping } from "@/lib/cost/connectors/matching";47import { isRunnable } from "@/lib/cost/connectors/registry";4849export const runtime = "nodejs";5051export function GET(req: NextRequest) {52  const denied = adminAuth(req);53  if (denied) return denied;54  const status = req.nextUrl.searchParams.get("status");55  const rows = getCostDb().prepare(`SELECT m.id, s.key source, m.product_url url, m.product_title title, m.proposed_item_code itemCode, m.pack_qty packQty, m.pack_unit packUnit, m.method, m.confidence, m.rationale, m.status, m.reviewed_at reviewedAt, m.created_at createdAt56    FROM product_mappings m JOIN cost_sources s ON s.id=m.source_id ${status ? "WHERE m.status=?" : ""} ORDER BY m.created_at DESC LIMIT 500`).all(...(status ? [status] : []));57  return NextResponse.json({ mappings: rows });58}5960/** Création/approbation manuelle : { source, itemCode, url, title?, packQty?, packUnit? } */61export async function POST(req: NextRequest) {62  const denied = adminAuth(req);63  if (denied) return denied;64  let b: { source?: string; itemCode?: string; url?: string; title?: string; packQty?: number; packUnit?: string };65  try { b = await req.json(); } catch { return NextResponse.json({ error: "JSON invalide" }, { status: 400 }); }66  if (!b.source || !isRunnable(b.source) || !b.itemCode || !b.url) return NextResponse.json({ error: "source, itemCode et url requis" }, { status: 400 });67  const d = getCostDb();68  const item = d.prepare("SELECT id FROM cost_items WHERE canonical_code=?").get(b.itemCode) as { id: number } | undefined;69  if (!item) return NextResponse.json({ error: "article inconnu" }, { status: 404 });70  const sid = sourceIdByKey(b.source, d);71  d.prepare(`INSERT INTO product_mappings(source_id,product_url,product_title,proposed_item_code,pack_qty,pack_unit,method,confidence,rationale,status) VALUES(?,?,?,?,?,?,'manual',1,'saisie administrateur','pending')72    ON CONFLICT(source_id,product_url) DO UPDATE SET proposed_item_code=excluded.proposed_item_code, pack_qty=excluded.pack_qty, pack_unit=excluded.pack_unit, method='manual', confidence=1`).run(sid, b.url, b.title ?? null, b.itemCode, b.packQty ?? null, b.packUnit ?? null);73  const id = (d.prepare("SELECT id FROM product_mappings WHERE source_id=? AND product_url=?").get(sid, b.url) as { id: number }).id;74  reviewMapping(id, "approved", d);75  return NextResponse.json({ ok: true, id });76}77
Files main
Menu

Theme