/** * src/services/source-service.ts * =============================== * Owns 0pt.source.schema.v1 — the per-tenant registry of upstream data * sources (databases, crawl domains, APIs, file drops). This is the registry * `SourceQueryService.querySource()` (src/services/source-query-service.ts) * and `SourceRatePolicyService` (src/services/source-rate-policy-service.ts) * both look a `source_id` up against — see docs/P0_QUERY_SOURCE_FLOW.md. * * Persistence: storage now lives behind SourceRepository * (src/repositories/source-repository.ts) — real Postgres (`opt_sources`, * sql/002_source_registry.sql) when DATABASE_URL is configured, an * in-memory Map otherwise (non-production only; resolveStoreMode fails hard * in production with no URL). This service still owns the business rules * (validation, tenant-agnostic CRUD orchestration) and the AUD0 emission — * only the storage moved. * * Callers that need a shared view across route modules should use the * shared singleton in src/services/source-registry.ts rather than * constructing a fresh `new SourceService()` per module — see that file's * doc comment for the "each route owned its own registry" history this * fixes. */ import { createAud0Client, type Aud0Client } from "../clients/aud0-client"; import { createSourceRepository, type SourceRepository, type SourceListFilter, } from "../repositories/source-repository"; import type { CreateSourceInput, Source, SourceStatus, SourceType } from "../types/source"; export interface SourceServiceOptions { aud0BaseUrl?: string; /** Explicit DATABASE_URL override — mainly for tests pointing at a specific Postgres instance. Falls back to resolveStoreMode's own env lookup when omitted. */ databaseUrl?: string; /** Inject a repository directly (tests) — bypasses resolveStoreMode entirely. */ repository?: SourceRepository; } export class SourceService { private readonly repo: SourceRepository; private readonly aud0: Aud0Client; constructor(opts?: SourceServiceOptions) { this.aud0 = createAud0Client(opts?.aud0BaseUrl); this.repo = opts?.repository ?? createSourceRepository(opts?.databaseUrl); } async createSource(input: CreateSourceInput): Promise { const source = await this.repo.create(input); await this.aud0.ingestEvent({ event_id: `evt_0pt_source_created_${source.source_id}`, event_type: "0pt.source_created", tenant_id: source.tenant_id, project_id: source.project_id, actor_id: source.created_by ?? "system", app_id: "0pt", capability_id: "0pt.create_source", resource_type: "source", resource_id: source.source_id, risk_level: "L2", payload: { source_type: source.source_type, name: source.name, adapter_id: source.adapter_id, access: source.access, }, created_at: source.created_at, correlation_id: source.correlation_id, }); return source; } async getSource(sourceId: string): Promise { return this.repo.get(sourceId); } /** * P060: the ONLY status transition path. Callers do not get a generic * update method, because status is what the activation gate protects — * SourceRegistryService.activateSource is the only caller that moves a * source to "active", and it runs the classification check first. */ async updateStatus(sourceId: string, status: SourceStatus, actorId?: string): Promise { const source = await this.repo.updateStatus(sourceId, status); await this.aud0.ingestEvent({ event_id: `evt_0pt_source_status_${source.source_id}_${Date.now()}`, event_type: "0pt.source_status_changed", tenant_id: source.tenant_id, project_id: source.project_id, actor_id: actorId ?? source.owner_id ?? "system", app_id: "0pt", capability_id: "0pt.update_source_status", resource_type: "source", resource_id: source.source_id, risk_level: "L2", payload: { status: source.status, name: source.name }, created_at: source.updated_at ?? new Date().toISOString(), correlation_id: source.correlation_id, }); return source; } async listSources(filter?: { tenant_id?: string; project_id?: string; source_type?: SourceType; status?: SourceStatus; }): Promise { return this.repo.list(filter as SourceListFilter | undefined); } }