SPB Git

Repository Public

route.ts 5.4 KB 99 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/admin/sources/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 — Métré : administration des sources (vue d'ensemble ; actions : ingest, import CSV, QA d'un document).63import { NextRequest, NextResponse } from "next/server";64import { HttpError, handle, readJson, requireUser } from "@/lib/metre/auth";65import { importScheduleCsv, ingestPermits, ingestSeao, setDocumentQa, sourcesOverview } from "@/lib/metre/sources/ingest";6667export const runtime = "nodejs";68export const maxDuration = 300;6970export async function GET(req: NextRequest) {71  return handle(() => { const ctx = requireUser(req, { roles: ["instructor", "admin"] }); return NextResponse.json(sourcesOverview(ctx.db)); });72}7374export async function POST(req: NextRequest) {75  return handle(async () => {76    const ctx = requireUser(req, { roles: ["admin", "instructor"], limit: { key: "admin.sources", max: 20, windowMs: 60_000 } });77    const b = await readJson<Record<string, unknown>>(req);78    switch (b.action) {79      case "ingest": {80        if (ctx.user.role !== "admin") throw new HttpError(403, "Ingestion réservée aux administrateurs.");81        if (b.source === "seao") return NextResponse.json(await ingestSeao(ctx, { weeks: Number(b.weeks ?? 2) }));82        if (b.source === "mtl_permits") return NextResponse.json(await ingestPermits(ctx, { url: typeof b.url === "string" ? b.url : undefined }));83        throw new HttpError(400, "Source non ingérable automatiquement : seao | mtl_permits (les autres passent par l'import CSV).");84      }85      case "import": {86        const cls = ["published_schedule", "awarded_unit_bid", "supplier_quote", "contractor_quote"];87        return NextResponse.json(importScheduleCsv(ctx, { sourceKey: String(b.source ?? ""), csv: String(b.csv ?? ""), title: String(b.title ?? "import"), url: typeof b.url === "string" ? b.url : null, publicationDate: typeof b.publicationDate === "string" ? b.publicationDate.slice(0, 10) : null, effectiveDate: typeof b.effectiveDate === "string" ? b.effectiveDate.slice(0, 10) : null, license: String(b.license ?? ""), evidenceClass: cls.includes(String(b.evidenceClass)) ? (b.evidenceClass as "published_schedule") : "published_schedule", geography: typeof b.geography === "string" ? b.geography : null, regionCode: typeof b.regionCode === "string" ? b.regionCode : null }), { status: 201 });88      }89      case "qa": {90        if (ctx.user.role !== "admin") throw new HttpError(403, "QA réservée aux administrateurs.");91        if (typeof b.documentId !== "string") throw new HttpError(400, "documentId requis.");92        setDocumentQa(ctx, b.documentId, b.status === "rejected" ? "rejected" : "published", typeof b.notes === "string" ? b.notes.slice(0, 1000) : null);93        return NextResponse.json({ ok: true });94      }95      default: throw new HttpError(400, "action : ingest | import | qa");96    }97  });98}99
Files main
Menu

Theme