lib/db (env-guarded postgres-js), lib/orders-store (createOrder/getOrder/listOrders/setStatus, DB-or-memory), /api/orders (POST create+estimate, GET list), /api/orders/[id] (GET, PATCH status with escrow-safe state machine). Activates fully on DATABASE_URL/POSTGRES_URL. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
23 lines
926 B
TypeScript
23 lines
926 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { createOrder, listOrders, type OrderConfig } from "@/lib/orders-store";
|
|
|
|
// POST /api/orders (docs/04) — create a draft order from a configurator config.
|
|
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 });
|
|
}
|
|
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 (org scoping added with auth).
|
|
export async function GET() {
|
|
const orders = await listOrders();
|
|
return NextResponse.json(orders);
|
|
}
|