fix(recovery): restore source removed by production documentation merge

This commit is contained in:
2026-11-10 03:42:10 +01:00
parent 585bdd4b44
commit 3d2fc6f913
957 changed files with 115754 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
import { NextRequest, NextResponse } from "next/server";
import { getOwnedOrder, setOwnedStatusIfCurrent } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mapping";
// 0pt#53: durable-store failures arrive as SiaxError(DEPENDENCY_UNAVAILABLE, 503);
// map them instead of letting App Router render an opaque 500.
async function readOwnedOrder(id: string, tenantId: string) {
try {
return await getOwnedOrder(id, tenantId);
} catch (err: unknown) {
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
}
// GET /api/orders/{id}
export async function GET(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
const { id } = await ctx.params;
const order = await readOwnedOrder(id, auth.owner.tenantId);
if (order instanceof NextResponse) return order;
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 auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
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 });
}
// Paid states are owned exclusively by the signed, matching Stripe webhook.
if (body.status === "in_production" || body.status === "delivered") {
return NextResponse.json({ error: { code: "payment_event_required" } }, { status: 403 });
}
const allowed = ["quoted", "deposit_pending", "review", "revision", "accepted"];
if (!body.status || !allowed.includes(body.status)) {
return NextResponse.json({ error: { code: "bad_status", message: "Ogiltig status" } }, { status: 400 });
}
const order = await readOwnedOrder(id, auth.owner.tenantId);
if (order instanceof NextResponse) return order;
if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
const previous: Record<string, string[]> = {
quoted: ["draft"],
deposit_pending: ["draft", "quoted"],
review: ["in_production"],
revision: ["review"],
accepted: ["review", "revision"],
};
if (!previous[body.status]?.includes(order.status)) {
return NextResponse.json({ error: { code: "invalid_transition" } }, { status: 409 });
}
let updated;
try {
updated = await setOwnedStatusIfCurrent(id, auth.owner.tenantId, order.status, body.status);
} catch (err: unknown) {
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
if (!updated) return NextResponse.json({ error: { code: "stale_transition" } }, { status: 409 });
return NextResponse.json(updated);
}
+48
View File
@@ -0,0 +1,48 @@
import { NextRequest, NextResponse } from "next/server";
import { listOwnedOrders } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mapping";
/**
* GET /api/orders/export (våg 3.4, DoD §56) — CSV-export of the authenticated
* owner's orders. Same fail-closed auth as GET/POST /api/orders (this path is
* outside middleware.ts's /api/0pt matcher, so the Zitadel verifier runs here).
*
* RFC 4180: CRLF line endings, fields containing comma, quote or newline are
* quoted with doubled quotes, header row always present.
*
* Locale note: deposit_ore / total_ore are exported as RAW ÖRE — öre is the
* canonical unit in the order contract; formatting (SEK, thousand separators)
* happens in the UI layer, never in the export.
*
* Cap note (DoD §56): NO row truncation/limiting — an export must be complete.
*/
function csvEscape(value: string): string {
if (/[",\r\n]/.test(value)) return `"${value.replace(/"/g, '""')}"`;
return value;
}
export async function GET(req: NextRequest) {
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
try {
const orders = await listOwnedOrders(auth.owner.tenantId);
const header = ["order_id", "flow", "status", "deposit_ore", "total_ore"];
const rows = orders.map((o) =>
[o.order_id, o.flow, o.status, String(o.deposit_ore), String(o.total_ore)].map(csvEscape).join(","),
);
const csv = [header.join(","), ...rows].join("\r\n") + "\r\n";
const date = new Date().toISOString().slice(0, 10);
return new NextResponse(csv, {
status: 200,
headers: {
"Content-Type": "text/csv; charset=utf-8",
"Content-Disposition": `attachment; filename="orders-${date}.csv"`,
},
});
} catch (err: unknown) {
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
}
+270
View File
@@ -0,0 +1,270 @@
import { NextRequest, NextResponse } from "next/server";
import { createOrder, type OrderConfig, type OrderOwner } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mapping";
import { createAud0Client } from "@/src/clients/aud0-client";
import { validateRows, defineMapping, type RowError } from "@siax/contracts/import-export";
import { serviceById, type Flow } from "@/lib/services";
import { IMPORT_DISPATCH_POLICY, evaluateImportDispatch } from "@/lib/import-dispatch-policy";
import { ENTITY_TYPES, FREQUENCIES, type EntityType, type Frequency } from "@/lib/data";
// C8: CSV-import of orders — the symmetric counterpart of GET /api/orders/export
// (våg 3.4). The server is the ONLY authority: the raw CSV is parsed here
// (RFC 4180, no eval), re-validated here via the frozen @siax/contracts kernel
// (validateRows) plus host field rules, and executed through the EXISTING order
// creation logic (createOrder) so imported rows are real tenant-owned orders.
// Client-side wizard parsing is presentation entropy, never trusted.
//
// Fail-closed properties:
// - No principal (requireOrderOwner) → 401, no parse, no writes.
// - Unknown header column → 400 (never silently dropped).
// - More than MAX_IMPORT_ROWS data rows → 413 (cap, never truncate silently).
// - execute() re-validates and imports only rows that pass; failing rows are
// reported per row (row/field/code) and never created. Each order insert is
// individually atomic via createOrder (per-row transactional semantics);
// a failing row can never corrupt a passing one.
// - Audit: every successful execute emits an AUD0 event via 0pt's sole producer
// boundary (durable outbox when DATABASE_URL is configured). Emission failure
// is logged, never blocks the import (the outbox pattern owns delivery).
// DES1GN ADR D3 (§31): taken härstammar från ImportDispatchPolicy — ett objekt,
// en sanning. Inline-taket är oförändrat (500); rader > policy.maxRows (5000)
// är N0D-dispatch-signalen (descriptor-gate: svarar 413 + hint tills N0D-workern
// är verifierad — aldrig tyst trunkering, aldrig tyst dispatch).
export const MAX_IMPORT_ROWS = IMPORT_DISPATCH_POLICY.maxInlineRows;
const MAX_CSV_CHARS = IMPORT_DISPATCH_POLICY.maxBytes;
const IMPORT_COLUMNS = ["flow", "service_type", "type", "volume", "frequency", "markets"] as const;
const FLOWS: readonly Flow[] = ["catalog", "custom", "subscription"];
const MAX_VOLUME = 1_000_000;
interface ImportRowError extends RowError {
message: string;
}
interface ParsedCsv {
rows: Record<string, string>[];
headerError?: string;
}
/** Minimal RFC 4180 parser: quoted fields, doubled quotes, CRLF or LF. No eval. */
export function parseCsv(text: string): ParsedCsv {
const rows: string[][] = [];
let field = "";
let row: string[] = [];
let inQuotes = false;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (inQuotes) {
if (ch === '"') {
if (text[i + 1] === '"') {
field += '"';
i++;
} else {
inQuotes = false;
}
} else {
field += ch;
}
} else if (ch === '"') {
inQuotes = true;
} else if (ch === ",") {
row.push(field);
field = "";
} else if (ch === "\n") {
row.push(field);
field = "";
rows.push(row);
row = [];
} else if (ch === "\r") {
// consumed by the following \n (or ignored for bare CR)
} else {
field += ch;
}
}
if (field.length > 0 || row.length > 0) {
row.push(field);
rows.push(row);
}
const nonEmpty = rows.filter((r) => r.length > 1 || (r[0] ?? "").trim() !== "");
if (nonEmpty.length === 0) return { rows: [] };
const header = nonEmpty[0].map((h) => h.trim().toLowerCase());
const unknown = header.filter((h) => h !== "" && !(IMPORT_COLUMNS as readonly string[]).includes(h));
if (unknown.length > 0) {
return { rows: [], headerError: `Okänd kolumn: ${unknown.join(", ")} (tillåtna: ${IMPORT_COLUMNS.join(", ")})` };
}
return {
rows: nonEmpty.slice(1).map((cells) => {
const obj: Record<string, string> = {};
header.forEach((h, idx) => {
if (h) obj[h] = (cells[idx] ?? "").trim();
});
return obj;
}),
};
}
/** Host field rules on top of the kernel's required/type checks. */
function hostValidate(rows: Record<string, string>[]): ImportRowError[] {
const errors: ImportRowError[] = [];
rows.forEach((r, idx) => {
const rowNo = idx + 1;
const flow = (r.flow ?? "").trim() as Flow;
if (flow && !FLOWS.includes(flow)) {
errors.push({ row: rowNo, field: "flow", code: "bad_type", message: `Ogiltigt flow "${r.flow}" (tillåtna: ${FLOWS.join(", ")})` });
}
const st = (r.service_type ?? "").trim();
if (st && !serviceById(st)) {
errors.push({ row: rowNo, field: "service_type", code: "bad_type", message: `Okänd service_type "${st}"` });
}
const type = (r.type ?? "").trim();
if (type && !ENTITY_TYPES.some((t) => t.id === type)) {
errors.push({ row: rowNo, field: "type", code: "bad_type", message: `Ogiltig typ "${type}" (tillåtna: ${ENTITY_TYPES.map((t) => t.id).join(", ")})` });
}
const volume = (r.volume ?? "").trim();
if (volume === "") {
// optional — order logic applies flow defaults
} else if (!/^\d+$/.test(volume) || Number(volume) < 1 || Number(volume) > MAX_VOLUME) {
errors.push({ row: rowNo, field: "volume", code: "bad_type", message: `volume måste vara ett positivt heltal ≤ ${MAX_VOLUME}` });
}
const freq = (r.frequency ?? "").trim();
if (freq && !FREQUENCIES.includes(freq as Frequency)) {
errors.push({ row: rowNo, field: "frequency", code: "bad_type", message: `Ogiltig frequency "${freq}" (tillåtna: ${FREQUENCIES.join(", ")})` });
}
});
return errors;
}
function buildConfig(r: Record<string, string>): OrderConfig {
const config: OrderConfig = {};
if ((r.service_type ?? "").trim()) config.service_type = r.service_type.trim();
if ((r.type ?? "").trim()) config.type = r.type.trim() as EntityType;
if ((r.volume ?? "").trim()) config.volume = Number(r.volume.trim());
if ((r.frequency ?? "").trim()) config.frequency = r.frequency.trim() as Frequency;
if ((r.markets ?? "").trim()) {
config.markets = r.markets.split(";").map((m) => m.trim()).filter(Boolean);
}
return config;
}
export interface ImportReport {
totalRows: number;
validRows: number;
errorCount: number;
errors: ImportRowError[];
}
/** Server-authoritative validation: kernel required/type checks + host rules. */
export function validateImport(csv: string): { report: ImportReport; rows: Record<string, string>[]; headerError?: string } {
const { rows, headerError } = parseCsv(csv);
if (headerError) return { report: { totalRows: 0, validRows: 0, errorCount: 1, errors: [{ row: 0, field: "header", code: "format", message: headerError }] }, rows: [], headerError };
const mapping = defineMapping({ columns: IMPORT_COLUMNS.map((c) => ({ source: c, target: c, required: c === "flow" })) });
const kernel = validateRows(rows, mapping);
const host = hostValidate(rows);
const kernelErrors: ImportRowError[] = kernel.errors.map((e) => ({ ...e, message: e.message ?? `Valideringsfel (${e.code})` }));
const errors = [...kernelErrors, ...host];
return {
report: {
totalRows: rows.length,
validRows: rows.length - new Set(errors.map((e) => e.row)).size,
errorCount: errors.length,
errors,
},
rows,
};
}
export async function POST(req: NextRequest) {
const auth = await requireOrderOwner(req);
if (auth.response) return auth.response;
const owner: OrderOwner = auth.owner;
let body: { mode?: string; csv?: string };
try {
body = await req.json();
} catch {
return NextResponse.json({ error: { code: "bad_json", message: "Invalid JSON" } }, { status: 400 });
}
const mode = body.mode;
const csv = body.csv;
if (!mode || !["validate", "dry_run", "execute"].includes(mode)) {
return NextResponse.json({ error: { code: "bad_mode", message: "mode måste vara validate | dry_run | execute" } }, { status: 400 });
}
if (typeof csv !== "string" || csv.trim() === "") {
return NextResponse.json({ error: { code: "bad_csv", message: "csv saknas eller är tom" } }, { status: 400 });
}
const csvBytes = Buffer.byteLength(csv, "utf8");
if (evaluateImportDispatch({ format: "csv", bytes: csvBytes, rows: 0 }).verdict === "byte_cap_exceeded") {
return NextResponse.json({ error: { code: "csv_too_large", message: `CSV större än ${MAX_CSV_CHARS} bytes` } }, { status: 413 });
}
try {
const { report, rows, headerError } = validateImport(csv);
if (headerError) {
return NextResponse.json({ error: { code: "bad_header", message: headerError }, report }, { status: 400 });
}
const dispatchVerdict = evaluateImportDispatch({ format: "csv", bytes: csvBytes, rows: rows.length });
if (dispatchVerdict.verdict === "dispatch_n0d") {
// DES1GN D3-signalen: den här volymen ÄR ett N0D-jobb (0pt.import_execute_job).
// Descriptor-gaten är stängd (N0D-worker UNVERIFIED) -> fail-closed 413 med hint.
return NextResponse.json(
{
error: {
code: "n0d_dispatch_required",
message: `Import med ${rows.length} rader ska köras som N0D-jobb (0pt.import_execute_job); dispatch är ännu inte aktiverad`,
hint: { capability_id: "0pt.import_execute", job_type: "0pt.import_execute_job", status: "UNVERIFIED" },
},
},
{ status: 413 },
);
}
if (dispatchVerdict.verdict === "row_cap_exceeded") {
return NextResponse.json({ error: { code: "row_cap_exceeded", message: `Max ${MAX_IMPORT_ROWS} rader per import` } }, { status: 413 });
}
if (mode === "validate" || mode === "dry_run") {
// No writes — dry_run is exactly validate plus an explicit rehearsal count.
return NextResponse.json({ mode, ...report, would_import: report.validRows });
}
// mode === "execute": server re-validates and imports ONLY passing rows.
const failedRows = new Set(report.errors.map((e) => e.row));
const orderIds: string[] = [];
let imported = 0;
for (let idx = 0; idx < rows.length; idx++) {
if (failedRows.has(idx + 1)) continue;
const r = rows[idx];
try {
const { order } = await createOrder((r.flow ?? "").trim(), buildConfig(r), undefined, owner);
orderIds.push(order.order_id);
imported++;
} catch (err: unknown) {
report.errors.push({ row: idx + 1, field: "row", code: "format", message: `Skapning misslyckades: ${err instanceof Error ? err.message : "okänt fel"}` });
}
}
// AUD0 audit — 0pt's sole producer boundary; durable outbox under DATABASE_URL.
try {
const aud0 = createAud0Client();
await aud0.ingestEvent({
event_id: (globalThis.crypto?.randomUUID?.() ?? `evt-${Date.now()}`),
event_type: "0pt.orders.import",
tenant_id: owner.tenantId,
project_id: "0pt-orders",
actor_id: owner.subject,
app_id: "0pt",
capability_id: "orders.import",
resource_type: "order",
resource_id: orderIds[0] ?? "none",
risk_level: "L2",
payload: { imported, total_rows: report.totalRows, error_count: report.errors.length, order_ids: orderIds },
created_at: new Date().toISOString(),
});
} catch (auditErr: unknown) {
console.error("[0pt-orders-import] AUD0 emission failed (import not rolled back):", auditErr);
}
return NextResponse.json({ mode, imported, totalRows: report.totalRows, validRows: report.validRows, errorCount: report.errors.length, errors: report.errors.slice(0, 100), order_ids: orderIds });
} catch (err: unknown) {
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
}
+68
View File
@@ -0,0 +1,68 @@
import { NextRequest, NextResponse } from "next/server";
import { createOrder, listOwnedOrders, IdempotencyKeyConflictError, type OrderConfig } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
import { resolveAdvanceAction, buildOrderInboxItem } from "@/lib/order-flow";
import { isSiaxError, siaxErrorResponseForLegacy } from "../siax-error-mapping";
// POST /api/orders (docs/04) — create a draft order from a configurator config.
// 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")
// so a retried submission (double-click, dropped response, proxy replay)
// returns the ORIGINAL order (200) instead of creating a duplicate (was:
// no idempotency mechanism at all -- every retry created a new row, P014
// 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();
} 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 idempotencyKey = req.headers.get("idempotency-key") ?? undefined;
try {
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 },
);
} catch (err: unknown) {
if (err instanceof IdempotencyKeyConflictError) {
return NextResponse.json({ error: { code: "idempotency_key_conflict", message: err.message } }, { status: 409 });
}
// 0pt#53: getSqlForDurableStore throws SiaxError(DEPENDENCY_UNAVAILABLE, 503) when
// production has no DATABASE_URL — surface it as documented, not opaque 500.
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
}
// 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 requireOrderOwner(req);
if (auth.response) return auth.response;
try {
const orders = await listOwnedOrders(auth.owner.tenantId);
// ADR-0001: servern resolvér advance-aktionen per order — klienten
// renderar bara (ActionGate). Policy ägs här, inte i UI-lagret.
const withActions = orders.map((o) => ({ ...o, advance_action: resolveAdvanceAction(o.status), inbox_item: buildOrderInboxItem(o) }));
return NextResponse.json(withActions);
} catch (err: unknown) {
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
}