SPB Git

Repository Public

route.ts 5.5 KB 92 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/dashboard/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é : tableau de bord (projets, révisions récentes, fraîcheur du catalogue, devoirs).63import { NextRequest, NextResponse } from "next/server";64import { handle, requireUser } from "@/lib/metre/auth";65import { listProjects } from "@/lib/metre/projects";66import { getCostDb, costDatabaseVersion } from "@/lib/cost/db";6768export const runtime = "nodejs";6970export async function GET(req: NextRequest) {71  return handle(() => {72    const ctx = requireUser(req);73    const projects = listProjects(ctx.db, ctx.user);74    const ids = projects.map((p) => p.id);75    const versions = ids.length ? ctx.db.prepare(`SELECT v.id, v.project_id projectId, v.number, v.label, v.total, v.created_at createdAt, p.name projectName FROM metre_estimate_versions v JOIN metre_projects p ON p.id=v.project_id WHERE v.project_id IN (${ids.map(() => "?").join(",")}) ORDER BY v.created_at DESC LIMIT 12`).all(...ids) : [];76    let catalogue: Record<string, unknown> = { available: false };77    try {78      const cdb = getCostDb();79      const prices = cdb.prepare("SELECT COUNT(*) n, MAX(observation_date) last, COUNT(DISTINCT cost_item_id) items FROM cost_item_prices WHERE price_kind IN ('observed','official')").get() as Record<string, unknown>;80      const labour = cdb.prepare("SELECT COUNT(*) n, MAX(effective_from) last FROM labour_rates").get() as Record<string, unknown>;81      const idx = cdb.prepare("SELECT MAX(period) last, COUNT(*) n FROM construction_cost_indices").get() as Record<string, unknown>;82      const items = (cdb.prepare("SELECT COUNT(*) n FROM cost_items WHERE active=1").get() as { n: number }).n;83      const asm = (cdb.prepare("SELECT COUNT(*) n FROM cost_assemblies WHERE active=1").get() as { n: number }).n;84      catalogue = { available: true, version: costDatabaseVersion(cdb), items, assemblies: asm, observedPrices: prices.n, itemsObserved: prices.items, lastPrice: prices.last, labourRates: labour.n, labourLast: labour.last, indexLast: idx.last, indexPoints: idx.n };85    } catch { /* base de coûts indisponible */ }86    const observations = ctx.db.prepare("SELECT evidence_class evidenceClass, COUNT(*) n, MAX(date_basis) last FROM metre_price_observations GROUP BY evidence_class").all();87    const course = (ctx.db.prepare("SELECT course FROM metre_users WHERE id=?").get(ctx.user.id) as { course: string | null } | undefined)?.course;88    const assignments = ctx.db.prepare(`SELECT a.id, a.course, a.title, a.due_at dueAt, (SELECT COUNT(*) FROM metre_submissions s WHERE s.assignment_id=a.id) submissions, (SELECT s.id FROM metre_submissions s WHERE s.assignment_id=a.id AND s.user_id=? LIMIT 1) mySubmission FROM metre_assignments a WHERE a.active=1 ${ctx.user.role === "student" && course ? "AND a.course=?" : ""} ORDER BY a.due_at IS NULL, a.due_at LIMIT 10`).all(...(ctx.user.role === "student" && course ? [ctx.user.id, course] : [ctx.user.id]));89    return NextResponse.json({ user: ctx.user, projects, versions, catalogue, observations, assignments });90  });91}92
Files main
Menu

Theme