feat(payments): bind orders to verified tenant and buyer
CI (SIAX Cloud) / integration (push) Has been skipped
masterplan-lock / check (push) Successful in 4s
CI (SIAX Cloud) / security (push) Successful in 14s
masterplan-lock / check (pull_request) Successful in 4s
CI (SIAX Cloud) / security (pull_request) Successful in 14s
Security Scan (SIAX Cloud) / security (push) Successful in 30s
CI (SIAX Cloud) / quality (push) Successful in 35s
CI (SIAX Cloud) / integration (pull_request) Successful in 25s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 54s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m31s

This commit is contained in:
2026-09-15 19:53:58 +02:00
parent f976f36530
commit a1557b2a7d
19 changed files with 326 additions and 76 deletions
+7 -7
View File
@@ -1,13 +1,13 @@
import { NextRequest, NextResponse } from "next/server";
import { getOrder, setStatusIfCurrent } from "@/lib/orders-store";
import { requireRouteAuth } from "@/src/auth/require-route-auth";
import { getOwnedOrder, setOwnedStatusIfCurrent } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
// GET /api/orders/{id}
export async function GET(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
const auth = await requireRouteAuth(req);
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
const { id } = await ctx.params;
const order = await getOrder(id);
const order = await getOwnedOrder(id, auth.owner.tenantId);
if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
return NextResponse.json(order);
}
@@ -15,7 +15,7 @@ export async function GET(req: NextRequest, ctx: { params: Promise<{ id: string
// 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 auth = await requireRouteAuth(req);
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
const { id } = await ctx.params;
let body: { status?: string };
@@ -35,7 +35,7 @@ export async function PATCH(req: NextRequest, ctx: { params: Promise<{ id: strin
if (!body.status || !allowed.includes(body.status)) {
return NextResponse.json({ error: { code: "bad_status", message: "Ogiltig status" } }, { status: 400 });
}
const order = await getOrder(id);
const order = await getOwnedOrder(id, auth.owner.tenantId);
if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
const previous: Record<string, string[]> = {
quoted: ["draft"],
@@ -47,7 +47,7 @@ export async function PATCH(req: NextRequest, ctx: { params: Promise<{ id: strin
if (!previous[body.status]?.includes(order.status)) {
return NextResponse.json({ error: { code: "invalid_transition" } }, { status: 409 });
}
const updated = await setStatusIfCurrent(id, order.status, body.status);
const updated = await setOwnedStatusIfCurrent(id, auth.owner.tenantId, order.status, body.status);
if (!updated) return NextResponse.json({ error: { code: "stale_transition" } }, { status: 409 });
return NextResponse.json(updated);
}
+9 -8
View File
@@ -1,11 +1,10 @@
import { NextRequest, NextResponse } from "next/server";
import { createOrder, listOrders, IdempotencyKeyConflictError, type OrderConfig } from "@/lib/orders-store";
import { requireRouteAuth } from "@/src/auth/require-route-auth";
import { createOrder, listOwnedOrders, IdempotencyKeyConflictError, type OrderConfig } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
// 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.
// A payable order is created only for a verified Zitadel tenant/buyer.
// Anonymous prospects may obtain estimates but cannot mint payment orders.
//
// P014: honors an optional `Idempotency-Key` header (the convention this
// skill's own docs already name -- "Idempotency-Key on order/payment POSTs")
@@ -15,6 +14,8 @@ import { requireRouteAuth } from "@/src/auth/require-route-auth";
// recon "Duplicate delivery idempotent: NOT MET"). Omitting the header keeps
// the pre-P014 behavior exactly: every call creates a fresh order.
export async function POST(req: NextRequest) {
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
let body: { flow?: string; config?: OrderConfig };
try {
body = await req.json();
@@ -28,7 +29,7 @@ export async function POST(req: NextRequest) {
const config = body.config || {};
const idempotencyKey = req.headers.get("idempotency-key") ?? undefined;
try {
const { order, replayed } = await createOrder(flow, config, idempotencyKey);
const { order, replayed } = await createOrder(flow, config, idempotencyKey, auth.owner);
return NextResponse.json(
{ order_id: order.order_id, status: order.status, deposit_ore: order.deposit_ore, total_ore: order.total_ore },
{ status: replayed ? 200 : 201 },
@@ -47,8 +48,8 @@ export async function POST(req: NextRequest) {
// 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);
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
const orders = await listOrders();
const orders = await listOwnedOrders(auth.owner.tenantId);
return NextResponse.json(orders);
}