import { NextRequest, NextResponse } from "next/server"; import { createOrder, listOrders, type OrderConfig } from "@/lib/orders-store"; import { requireRouteAuth } from "@/src/auth/require-route-auth"; // POST /api/orders (docs/04) — create a draft order from a configurator config. // Intentionally anonymous: a prospect drafts/estimates a job before signing in // to pay the deposit (see docs/01, the configurator flow). Only reading back // the order queue (GET, below) is a data-exposure risk. export async function POST(req: NextRequest) { let body: { flow?: string; config?: OrderConfig }; try { body = await req.json(); } catch { return NextResponse.json({ error: { code: "bad_json", message: "Invalid JSON" } }, { status: 400 }); } if (!body || typeof body !== "object") { return NextResponse.json({ error: { code: "bad_json", message: "Invalid JSON body" } }, { status: 400 }); } const flow = body.flow || "custom"; const config = body.config || {}; const order = await createOrder(flow, config); return NextResponse.json({ order_id: order.order_id, status: order.status, deposit_ore: order.deposit_ore, total_ore: order.total_ore }, { status: 201 }); } // GET /api/orders — list all orders (queue + financial aggregates). This is // outside middleware.ts's matcher (/api/0pt/:path* only), so auth is enforced // here directly via the same fail-closed Zitadel verifier — see // src/auth/require-route-auth.ts for why the request is re-verified rather // than trusting a forwarded header. export async function GET(req: NextRequest) { const auth = await requireRouteAuth(req); if (auth.response) return auth.response; const orders = await listOrders(); return NextResponse.json(orders); }