111 lines
4.1 KiB
TypeScript
111 lines
4.1 KiB
TypeScript
/**
|
|
* src/repositories/in-memory-source-repository.ts
|
|
* ==================================================
|
|
* The original SourceService storage, extracted verbatim behind the
|
|
* SourceRepository seam (src/repositories/source-repository.ts) so
|
|
* PostgresSourceRepository can run the exact same business rules
|
|
* ("already exists", "name is required", "not found", filter semantics)
|
|
* against real Postgres instead. Used outside production when no
|
|
* DATABASE_URL is configured (resolveStoreMode), and directly by tests that
|
|
* want a hermetic, dependency-free SourceService.
|
|
*/
|
|
import type { CreateSourceInput, Source, SourceStatus, SourceType } from "../types/source";
|
|
import type { SourceListFilter, SourceRepository } from "./source-repository";
|
|
|
|
/** Storage shape: nullable columns, exactly mirroring `opt_sources`. */
|
|
interface SourceRecord {
|
|
source_id: string;
|
|
tenant_id: string;
|
|
project_id: string;
|
|
source_type: string;
|
|
name: string;
|
|
adapter_id: string | null;
|
|
access: string | null;
|
|
description: string | null;
|
|
status: string;
|
|
correlation_id: string | null;
|
|
created_at: string;
|
|
updated_at: string | null;
|
|
created_by: string | null;
|
|
owner_id: string | null;
|
|
}
|
|
|
|
export class InMemorySourceRepository implements SourceRepository {
|
|
private sources: Map<string, SourceRecord> = new Map();
|
|
|
|
async create(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 record: SourceRecord = {
|
|
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 ?? null,
|
|
access: input.access ?? null,
|
|
description: input.description ?? null,
|
|
// P060: "pending", not "active". A source with no rights, no provenance
|
|
// and no authorization used to be live the instant it was registered;
|
|
// reaching "active" now goes through
|
|
// SourceRegistryService.activateSource's classification gate.
|
|
status: input.status ?? "pending",
|
|
correlation_id: input.correlation_id ?? null,
|
|
created_at: now,
|
|
updated_at: null,
|
|
created_by: input.created_by ?? null,
|
|
owner_id: input.owner_id ?? null,
|
|
};
|
|
this.sources.set(record.source_id, record);
|
|
return toSource(record);
|
|
}
|
|
|
|
async get(sourceId: string): Promise<Source> {
|
|
const record = this.sources.get(sourceId);
|
|
if (!record) throw new Error(`Source ${sourceId} not found`);
|
|
return toSource(record);
|
|
}
|
|
|
|
async updateStatus(sourceId: string, status: SourceStatus): Promise<Source> {
|
|
const record = this.sources.get(sourceId);
|
|
if (!record) throw new Error(`Source ${sourceId} not found`);
|
|
record.status = status;
|
|
record.updated_at = new Date().toISOString();
|
|
return toSource(record);
|
|
}
|
|
|
|
async list(filter?: SourceListFilter): Promise<Source[]> {
|
|
let results = Array.from(this.sources.values());
|
|
if (filter?.tenant_id) results = results.filter((r) => r.tenant_id === filter.tenant_id);
|
|
if (filter?.project_id) results = results.filter((r) => r.project_id === filter.project_id);
|
|
if (filter?.source_type) results = results.filter((r) => r.source_type === filter.source_type);
|
|
if (filter?.status) results = results.filter((r) => r.status === filter.status);
|
|
return results.map(toSource);
|
|
}
|
|
}
|
|
|
|
function toSource(r: SourceRecord): Source {
|
|
return {
|
|
source_id: r.source_id,
|
|
tenant_id: r.tenant_id,
|
|
project_id: r.project_id,
|
|
source_type: r.source_type as SourceType,
|
|
name: r.name,
|
|
adapter_id: r.adapter_id ?? undefined,
|
|
access: (r.access as Source["access"]) ?? undefined,
|
|
description: r.description ?? undefined,
|
|
status: r.status as SourceStatus,
|
|
correlation_id: r.correlation_id ?? undefined,
|
|
created_at: r.created_at,
|
|
updated_at: r.updated_at ?? undefined,
|
|
created_by: r.created_by ?? undefined,
|
|
owner_id: r.owner_id ?? undefined,
|
|
};
|
|
}
|