fix(recovery): restore source removed by production documentation merge
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/activate/route.ts
|
||||
* ==================================================
|
||||
* P060 activation gate over HTTP. POST activates a source ONLY when it is
|
||||
* classified (owner + unexpired authorization + rights + provenance);
|
||||
* otherwise it returns 403 AUTHORIZATION_DENIED naming every missing fact.
|
||||
* GET returns the classification without changing anything, so an operator
|
||||
* can see what is missing before trying.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { activateSourceSchema, parseBody } 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";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const tenantId = new URL(request.url).searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const guard = assertTenantMatchesVerified(request, tenantId);
|
||||
if (guard) return guard;
|
||||
return NextResponse.json(await sourceRegistryService.classifySource(sourceId, tenantId));
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const parsed = parseBody(activateSourceSchema, await request.json());
|
||||
if (!parsed.ok) return NextResponse.json(parsed.body, { status: parsed.status });
|
||||
const guard = assertTenantMatchesVerified(request, parsed.data.tenant_id);
|
||||
if (guard) return guard;
|
||||
const source = await sourceRegistryService.activateSource(
|
||||
sourceId,
|
||||
parsed.data.tenant_id,
|
||||
parsed.data.actor_id,
|
||||
);
|
||||
return NextResponse.json(source);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/authorization/route.ts
|
||||
* ======================================================
|
||||
* P060 "authorization": the recorded decision that 0pt may connect to this
|
||||
* source at all — separate from rights (what the DATA may be used for). PUT
|
||||
* is an upsert; a service identity as `granted_by` is rejected (400), because
|
||||
* a source may not authorize itself.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, setSourceAuthorizationSchema } 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";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const tenantId = new URL(request.url).searchParams.get("tenant_id");
|
||||
if (!tenantId) return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
const guard = assertTenantMatchesVerified(request, tenantId);
|
||||
if (guard) return guard;
|
||||
const auth = await sourceRegistryService.getAuthorization(sourceId, tenantId);
|
||||
if (!auth) {
|
||||
return NextResponse.json({ error: `source ${sourceId} has no authorization record` }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(auth);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const parsed = parseBody(setSourceAuthorizationSchema, await request.json());
|
||||
if (!parsed.ok) return NextResponse.json(parsed.body, { status: parsed.status });
|
||||
const guard = assertTenantMatchesVerified(request, parsed.data.tenant_id);
|
||||
if (guard) return guard;
|
||||
return NextResponse.json(
|
||||
await sourceRegistryService.setAuthorization({ ...parsed.data, source_id: sourceId }),
|
||||
);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/freshness/route.ts
|
||||
* =====================================================
|
||||
* Freshness view derived from provenance's freshness_sla_hours + the last
|
||||
* successful observation (recorded via POST .../health with status "up",
|
||||
* or SourceRegistryService.recordSuccess directly). Read-only — there is no
|
||||
* separate write path here by design; freshness is a derived signal, not
|
||||
* its own record.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
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 freshness = await sourceRegistryService.getFreshness(sourceId, tenantId);
|
||||
return NextResponse.json(freshness);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/health/route.ts
|
||||
* ==================================================
|
||||
* source.health — record a health observation (POST) and read the
|
||||
* aggregate view (GET): status, last check, consecutive failures. Bounded
|
||||
* history (latest 100 observations) — this is an operational signal, not
|
||||
* an audit log; AUD0 owns history.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, recordSourceHealthCheckSchema } 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";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
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 health = await sourceRegistryService.getHealth(sourceId, tenantId);
|
||||
return NextResponse.json(health);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(recordSourceHealthCheckSchema, __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 health = await sourceRegistryService.recordHealthCheck({
|
||||
source_id: sourceId,
|
||||
tenant_id: body.tenant_id,
|
||||
status: body.status,
|
||||
checked_at: body.checked_at ?? new Date().toISOString(),
|
||||
latency_ms: body.latency_ms,
|
||||
detail: body.detail,
|
||||
});
|
||||
return NextResponse.json(health, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/provenance/route.ts
|
||||
* ======================================================
|
||||
* Per-SOURCE provenance/method metadata (Master-DoD §10) — HOW a source is
|
||||
* obtained (official_api | bulk_download | authorized_web | licensed_feed |
|
||||
* manual_upload), not per-field bridge_provenance. See BEFORE_STATE.md §3
|
||||
* for that distinction: this route does not, and cannot, close the
|
||||
* bridge_provenance gap — it only surfaces what SourceRegistryService
|
||||
* already models at the source level.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, setSourceProvenanceSchema } 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";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
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 provenance = await sourceRegistryService.getProvenance(sourceId, tenantId);
|
||||
if (!provenance) {
|
||||
return NextResponse.json({ error: `source ${sourceId} has no provenance record` }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(provenance);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(setSourceProvenanceSchema, __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 provenance = await sourceRegistryService.setProvenance({
|
||||
...body,
|
||||
source_id: sourceId,
|
||||
updated_at: new Date().toISOString(),
|
||||
});
|
||||
return NextResponse.json(provenance);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/query/route.ts
|
||||
* ================================================
|
||||
* 0pt.query_source — bounded synchronous query execution for a registered
|
||||
* Source. Additive: does not modify sibling ../route.ts or ../../route.ts.
|
||||
*
|
||||
* UPDATED (Source Registry Postgres-persistence pass): now imports the
|
||||
* shared `sourceService` singleton from src/services/source-registry.ts
|
||||
* instead of constructing its own SourceService, closing the "not even a
|
||||
* shared process-wide singleton" gap BEFORE_STATE.md §1 flagged (a source
|
||||
* registered via POST /api/0pt/sources used to be invisible here). With
|
||||
* DATABASE_URL configured this is doubly fixed — state lives in Postgres
|
||||
* (sql/002_source_registry.sql), genuinely shared across processes, not
|
||||
* just within one. Details: docs/P0_QUERY_SOURCE_FLOW.md.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, sourceQuerySchema } from "@/src/validation/0pt-schemas";
|
||||
import { sourceService } from "@/src/services/source-registry";
|
||||
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
|
||||
import {
|
||||
SourceQueryService,
|
||||
SourceQueryTimeoutError,
|
||||
SourceQueryUnsupportedError,
|
||||
} from "@/src/services/source-query-service";
|
||||
|
||||
const queryService = new SourceQueryService({ sourceService });
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(sourceQuerySchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const body = __parsed.data;
|
||||
|
||||
if (typeof body.query !== "string" || body.query.trim().length === 0) {
|
||||
return NextResponse.json({ error: "query (string) is required" }, { status: 400 });
|
||||
}
|
||||
if (typeof body.tenant_id !== "string" || typeof body.project_id !== "string") {
|
||||
return NextResponse.json({ error: "tenant_id and project_id are required" }, { status: 400 });
|
||||
}
|
||||
if (!body.connection_config || typeof body.connection_config !== "object") {
|
||||
return NextResponse.json({ error: "connection_config is required" }, { status: 400 });
|
||||
}
|
||||
const tenantGuard = assertTenantMatchesVerified(request, body.tenant_id);
|
||||
if (tenantGuard) return tenantGuard;
|
||||
|
||||
const result = await queryService.querySource({
|
||||
source_id: sourceId,
|
||||
query: body.query,
|
||||
params: body.params,
|
||||
tenant_id: body.tenant_id,
|
||||
project_id: body.project_id,
|
||||
actor_id: body.actor_id,
|
||||
correlation_id: body.correlation_id,
|
||||
max_rows: body.max_rows,
|
||||
timeout_ms: body.timeout_ms,
|
||||
connection_config: body.connection_config,
|
||||
});
|
||||
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
if (error instanceof SourceQueryTimeoutError) {
|
||||
return NextResponse.json({ error: message }, { status: 504 });
|
||||
}
|
||||
if (error instanceof SourceQueryUnsupportedError) {
|
||||
return NextResponse.json({ error: message }, { status: 422 });
|
||||
}
|
||||
const status = /not found/i.test(message) ? 404 : 400;
|
||||
return NextResponse.json({ error: message }, { status });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/rights/route.ts
|
||||
* ==================================================
|
||||
* Rights / license / purpose metadata (Master-DoD §10). PUT is an upsert
|
||||
* (SourceRegistryService.setRights), matching the fail-closed purpose gate:
|
||||
* no rights record at all denies every purpose until one is set here.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, setSourceRightsSchema } 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";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
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 rights = await sourceRegistryService.getRights(sourceId, tenantId);
|
||||
if (!rights) {
|
||||
return NextResponse.json({ error: `source ${sourceId} has no rights record` }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(rights);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(setSourceRightsSchema, __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 rights = await sourceRegistryService.setRights({ ...body, source_id: sourceId });
|
||||
return NextResponse.json(rights);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/route.ts
|
||||
* ==========================================
|
||||
* source.describe — full SourceDescriptor (base + rights + provenance +
|
||||
* health + freshness + rate policy). Cross-tenant probes get 404 NOT_FOUND
|
||||
* (no existence oracle, §50) via SourceRegistryService.describeSource's
|
||||
* assertSourceTenant gate.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
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 descriptor = await sourceRegistryService.describeSource(sourceId, tenantId);
|
||||
return NextResponse.json(descriptor);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/schedule/route.ts
|
||||
* =================================================
|
||||
* P060 "schedule": ingestion mode + cadence, recorded on the source. 0pt does
|
||||
* NOT run cron itself — ACT0 owns scheduling; this row is the declaration a
|
||||
* scheduler reads, and saying otherwise would be a claim this repo cannot back.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, setSourceScheduleSchema } 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";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const tenantId = new URL(request.url).searchParams.get("tenant_id");
|
||||
if (!tenantId) return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
const guard = assertTenantMatchesVerified(request, tenantId);
|
||||
if (guard) return guard;
|
||||
const schedule = await sourceRegistryService.getSchedule(sourceId, tenantId);
|
||||
if (!schedule) {
|
||||
return NextResponse.json({ error: `source ${sourceId} has no schedule record` }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(schedule);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const parsed = parseBody(setSourceScheduleSchema, await request.json());
|
||||
if (!parsed.ok) return NextResponse.json(parsed.body, { status: parsed.status });
|
||||
const guard = assertTenantMatchesVerified(request, parsed.data.tenant_id);
|
||||
if (guard) return guard;
|
||||
return NextResponse.json(
|
||||
await sourceRegistryService.setSchedule({
|
||||
...parsed.data,
|
||||
source_id: sourceId,
|
||||
updated_at: new Date().toISOString(),
|
||||
}),
|
||||
);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { sourceRatePolicyService as service } from "@/src/services/source-registry";
|
||||
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
|
||||
|
||||
// P014: this lookup used to accept only `sourceRatePolicyId`, with NO tenant
|
||||
// check anywhere in the request path — any authenticated caller could read
|
||||
// another tenant's rate-limit/crawl-budget/compliance-notes record by
|
||||
// guessing or enumerating IDs. Now requires `?tenant_id=` (matching every
|
||||
// sibling route under app/api/0pt/sources/*) and both checks it against the
|
||||
// verified token (assertTenantMatchesVerified) and threads it into the
|
||||
// service so a foreign policy_id 404s instead of leaking (no existence
|
||||
// oracle) — see getSourceRatePolicy's doc comment.
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ sourceRatePolicyId: string }> },
|
||||
) {
|
||||
try {
|
||||
const { sourceRatePolicyId } = await params;
|
||||
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 policy = await service.getSourceRatePolicy(sourceRatePolicyId, tenantId);
|
||||
return NextResponse.json(policy);
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return NextResponse.json({ error: message }, { status: 404 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, createSourceRatePolicySchema } from "@/src/validation/0pt-schemas";
|
||||
import { sourceRatePolicyService as service } from "@/src/services/source-registry";
|
||||
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(createSourceRatePolicySchema, __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 policy = await service.createSourceRatePolicy(body);
|
||||
return NextResponse.json(policy, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return NextResponse.json({ error: message }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id") ?? undefined;
|
||||
const tenantGuard = assertTenantMatchesVerified(request, tenantId);
|
||||
if (tenantGuard) return tenantGuard;
|
||||
const policies = await service.listSourceRatePolicies({
|
||||
tenant_id: tenantId,
|
||||
project_id: searchParams.get("project_id") ?? undefined,
|
||||
source_id: searchParams.get("source_id") ?? undefined,
|
||||
});
|
||||
return NextResponse.json(policies);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user