import { NextRequest, NextResponse } from "next/server"; import { listOrders } from "@/lib/orders-store"; import { requireRouteAuth } from "@/src/auth/require-route-auth"; // GET /api/admin/metrics — order queue + financial aggregates for the admin // dashboard. Outside middleware.ts's matcher (/api/0pt/:path* only) and // previously unauthenticated entirely, so auth is enforced here directly via // the same fail-closed Zitadel verifier — see src/auth/require-route-auth.ts. export async function GET(req: NextRequest) { const auth = await requireRouteAuth(req); if (auth.response) return auth.response; const orders = await listOrders(); const byStatus: Record = {}; let depositPaidOre = 0; let pipelineOre = 0; for (const o of orders) { byStatus[o.status] = (byStatus[o.status] ?? 0) + 1; if (o.status !== "draft") depositPaidOre += o.deposit_ore ?? 0; pipelineOre += o.total_ore ?? 0; } return NextResponse.json({ total: orders.length, by_status: byStatus, in_production: byStatus["in_production"] ?? 0, review: byStatus["review"] ?? 0, delivered: byStatus["delivered"] ?? 0, deposit_paid_ore: depositPaidOre, pipeline_ore: pipelineOre, }); }