Files
0pt/src/services/source-service.ts
T
SIAX Foundation Bot 844220d9f5
CI (SIAX Cloud) / security (push) Failing after 28s
CI (SIAX Cloud) / quality (push) Failing after 38s
feat(foundation): Wave 3 integrity/persistence/build fixes
2026-07-25 13:54:36 +02:00

86 lines
3.2 KiB
TypeScript

/**
* src/services/source-service.ts
* ===============================
* Owns 0pt.source.schema.v1 — the registry of upstream data sources
* (databases, crawl domains, APIs, file drops). In-memory reference
* implementation (same convention as the other 0pt services in this repo:
* each API route file constructs its own instance; a real deployment backs
* this with `opt_sources` — sql/002_p0_dataset_quality.sql).
*
* Consumed by:
* - source-query-service.ts (getSource -> is this source queryable?)
* - source-rate-policy-service.ts (getSource -> does the source exist?)
*
* Registering a source emits a best-effort AUD0 `0pt.source_registered`
* event; with no AUD0_URL configured that emission is a no-op (see
* aud0-client.ts), so this stays hermetic in tests and local dev.
*/
import { createAud0Client, type Aud0Client } from "../clients/aud0-client";
import type { CreateSourceInput, Source } from "../types/source";
export class SourceService {
private sources: Map<string, Source> = new Map();
private aud0: Aud0Client;
constructor(aud0BaseUrl?: string) {
this.aud0 = createAud0Client(aud0BaseUrl);
}
async createSource(input: CreateSourceInput): Promise<Source> {
if (this.sources.has(input.source_id)) {
throw new Error(`Source ${input.source_id} already exists`);
}
if (!input.name) {
throw new Error("name is required");
}
const now = new Date().toISOString();
const source: Source = {
source_id: input.source_id,
tenant_id: input.tenant_id,
project_id: input.project_id,
source_type: input.source_type,
name: input.name,
adapter_id: input.adapter_id,
description: input.description,
status: input.status ?? "active",
correlation_id: input.correlation_id,
created_at: now,
created_by: input.created_by,
};
this.sources.set(source.source_id, source);
await this.aud0.ingestEvent({
event_id: `evt_0pt_source_registered_${source.source_id}`,
event_type: "0pt.source_registered",
tenant_id: source.tenant_id,
project_id: source.project_id,
actor_id: source.created_by ?? "system",
app_id: "0pt",
capability_id: "0pt.register_source",
resource_type: "source",
resource_id: source.source_id,
risk_level: "L2",
payload: { source_type: source.source_type, adapter_id: source.adapter_id ?? null, name: source.name },
created_at: now,
correlation_id: source.correlation_id,
});
return source;
}
async getSource(sourceId: string): Promise<Source> {
const source = this.sources.get(sourceId);
if (!source) throw new Error(`Source ${sourceId} not found`);
return source;
}
async listSources(filter?: { tenant_id?: string; project_id?: string; source_type?: Source["source_type"] }): Promise<Source[]> {
let results = Array.from(this.sources.values());
if (filter?.tenant_id) results = results.filter((s) => s.tenant_id === filter.tenant_id);
if (filter?.project_id) results = results.filter((s) => s.project_id === filter.project_id);
if (filter?.source_type) results = results.filter((s) => s.source_type === filter.source_type);
return results;
}
}