/** * app/api/0pt/sources/route.ts * ============================== * source.register (POST) + source.query (GET) — the P0 Source Registry * route surface BEFORE_STATE.md §1 found entirely missing * (`grep -rl "SourceRegistryService" app/` returned nothing). Backed by * SourceRegistryService, which now persists to Postgres when DATABASE_URL * is configured (src/repositories/*, sql/002_source_registry.sql). * * Uses the shared singleton from src/services/source-registry.ts, not a * fresh `new SourceRegistryService()` per route module — see that file's * doc comment. */ import { NextRequest, NextResponse } from "next/server"; import { parseBody, registerSourceSchema } from "@/src/validation/0pt-schemas"; import { sourceRegistryService } from "@/src/services/source-registry"; import { siaxErrorResponse } from "@/src/http/siax-error-response"; import { assertTenantMatchesVerified } from "@/src/http/tenant-guard"; import type { SourceStatus, SourceType } from "@/src/types/source"; import type { SourceRights } from "@/src/types/source-registry"; export async function POST(request: NextRequest) { try { const __raw = await request.json(); const __parsed = parseBody(registerSourceSchema, __raw); if (!__parsed.ok) { return NextResponse.json(__parsed.body, { status: __parsed.status }); } const body = __parsed.data; const tenantGuard = assertTenantMatchesVerified(request, body.tenant_id); if (tenantGuard) return tenantGuard; const descriptor = await sourceRegistryService.registerSource(body); return NextResponse.json(descriptor, { status: 201 }); } catch (error: unknown) { return siaxErrorResponse(error); } } export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); const tenantId = searchParams.get("tenant_id"); if (!tenantId) { return NextResponse.json({ error: "tenant_id is required" }, { status: 400 }); } const tenantGuard = assertTenantMatchesVerified(request, tenantId); if (tenantGuard) return tenantGuard; const sources = await sourceRegistryService.querySources({ tenant_id: tenantId, project_id: searchParams.get("project_id") ?? undefined, source_type: (searchParams.get("source_type") as SourceType) ?? undefined, status: (searchParams.get("status") as SourceStatus) ?? undefined, purpose: searchParams.get("purpose") ?? undefined, redistribution: (searchParams.get("redistribution") as SourceRights["redistribution"]) ?? undefined, }); return NextResponse.json(sources); } catch (error: unknown) { return siaxErrorResponse(error); } }