import { NextRequest, NextResponse } from "next/server"; import { getOrder, setStatus } from "@/lib/orders-store"; // GET /api/orders/{id} export async function GET(_req: NextRequest, ctx: { params: Promise<{ id: string }> }) { const { id } = await ctx.params; const order = await getOrder(id); if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 }); return NextResponse.json(order); } // PATCH /api/orders/{id} — status transition (deposit → in_production → review → accepted ...). // Escrow rule (docs/01): balance never charged before 'accepted'. Stripe wiring follows. export async function PATCH(req: NextRequest, ctx: { params: Promise<{ id: string }> }) { const { id } = await ctx.params; let body: { status?: string }; 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 allowed = ["draft", "quoted", "deposit_pending", "in_production", "review", "revision", "accepted", "delivered"]; if (!body.status || !allowed.includes(body.status)) { return NextResponse.json({ error: { code: "bad_status", message: "Ogiltig status" } }, { status: 400 }); } const order = await setStatus(id, body.status); if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 }); return NextResponse.json(order); }