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>
30 lines
1.4 KiB
TypeScript
30 lines
1.4 KiB
TypeScript
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 });
|
|
}
|
|
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);
|
|
}
|