CI (SIAX Cloud) / integration (push) Has been skipped
masterplan-lock / check (push) Successful in 5s
Security Scan (SIAX Cloud) / security (push) Successful in 15s
CI (SIAX Cloud) / security (push) Successful in 17s
masterplan-lock / check (pull_request) Successful in 4s
CI (SIAX Cloud) / security (pull_request) Successful in 15s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 13s
CI (SIAX Cloud) / quality (push) Successful in 51s
CI (SIAX Cloud) / integration (pull_request) Successful in 30s
CI (SIAX Cloud) / quality (pull_request) Successful in 37s
119 lines
4.5 KiB
TypeScript
119 lines
4.5 KiB
TypeScript
/**
|
|
* `connectors/inventory-report.ts` — HSP-081: discovery av befintlig
|
|
* kundstack. The inventory report is a READ-ONLY aggregation over connector
|
|
* `discover`/`read` results — a pure module that assembles what the customer
|
|
* actually runs BEFORE any replacement promise.
|
|
*
|
|
* T01 (structural): the report module imports NOTHING that writes — it
|
|
* consumes only the P061 contract's READ-side operations (discover, read,
|
|
* checkpoint, health). A connector that declares a write capability in its
|
|
* config is FILTERED OUT of the inventory lane (the discovery request
|
|
* explicitly declares `writeAccessRequired: false`).
|
|
* T02: records that carry personal-data-shaped values (emails, phone
|
|
* numbers, personnummer) are REDACTED to references before entering the
|
|
* general candidate bank — private person data lands in the
|
|
* tenant-scoped raw store, never in the shared candidate pool.
|
|
* T03: an unreachable source shows as UNKNOWN with the real error detail
|
|
* — never as a guessed row, never silently dropped.
|
|
*/
|
|
|
|
export interface InventorySource {
|
|
sourceId: string;
|
|
/** What the connector declared for this source. */
|
|
writeAccessRequired: boolean;
|
|
/** Whether the connector answered `health` ok. */
|
|
reachable: boolean;
|
|
/** Real error detail when unreachable (T03). */
|
|
unreachableError?: string;
|
|
/** Discovered partitions with their record shapes. */
|
|
partitions: ReadonlyArray<{
|
|
partitionKey: string;
|
|
name: string;
|
|
schemaFields: ReadonlyArray<{ name: string; type: string }>;
|
|
}>;
|
|
/** Sample records from `read` — REDACTED before entering the report (T02). */
|
|
sampleRecords: ReadonlyArray<Record<string, unknown>>;
|
|
}
|
|
|
|
export interface InventoryEntry {
|
|
sourceId: string;
|
|
state: 'inventoried' | 'unknown';
|
|
/** Real error detail for unreachable sources (T03: unknown, never guessed). */
|
|
unknownReason?: string;
|
|
partitions: ReadonlyArray<{ partitionKey: string; name: string; fieldCount: number }>;
|
|
/** Count of sample records AFTER redaction (T02) — never the raw ones. */
|
|
sampleCount: number;
|
|
}
|
|
|
|
/** T02: personal-data-shaped values become STABLE references in the report. */
|
|
const PERSONAL_SHAPE =
|
|
/([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}|(?:\+46|0)[\s-]?\d{2,3}(?:[\s-]?\d{2,4}){2,3}|\d{6}-?\d{4})/g;
|
|
|
|
export function redactPersonalData(record: Record<string, unknown>): Record<string, unknown> {
|
|
const out: Record<string, unknown> = {};
|
|
for (const [key, value] of Object.entries(record)) {
|
|
if (typeof value === 'string') {
|
|
out[key] = value.replace(PERSONAL_SHAPE, '[REDACTED-REF]');
|
|
} else if (value !== null && typeof value === 'object') {
|
|
out[key] = redactPersonalData(value as Record<string, unknown>);
|
|
} else {
|
|
out[key] = value;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Assemble the inventory report. Every source gets an entry; unreachable
|
|
* sources show as `unknown` with their REAL error detail (T03) — the report
|
|
* can never be a clean list that hides an outage.
|
|
*/
|
|
export function buildInventoryReport(sources: ReadonlyArray<InventorySource>): InventoryEntry[] {
|
|
const report: InventoryEntry[] = [];
|
|
for (const src of sources) {
|
|
// T01: a source whose connector requires WRITE access is excluded from
|
|
// the inventory lane — discovery is read-only by contract.
|
|
if (src.writeAccessRequired) continue;
|
|
|
|
if (!src.reachable) {
|
|
report.push({
|
|
sourceId: src.sourceId,
|
|
state: 'unknown',
|
|
unknownReason: src.unreachableError ?? 'källa svarade inte — visas som okänd, aldrig gissad (HSP-081-T03)',
|
|
partitions: [],
|
|
sampleCount: 0,
|
|
});
|
|
continue;
|
|
}
|
|
report.push({
|
|
sourceId: src.sourceId,
|
|
state: 'inventoried',
|
|
partitions: src.partitions.map((p) => ({
|
|
partitionKey: p.partitionKey,
|
|
name: p.name,
|
|
fieldCount: p.schemaFields.length,
|
|
})),
|
|
sampleCount: src.sampleRecords.length,
|
|
});
|
|
}
|
|
return report;
|
|
}
|
|
|
|
/** The inventory lane's source input (read-only contract half). */
|
|
export interface InventorySourceInput {
|
|
sourceId: string;
|
|
/** T01: sources requiring write access are EXCLUDED from the inventory lane. */
|
|
writeAccessRequired: boolean;
|
|
reachable: boolean;
|
|
unreachableError?: string;
|
|
partitions: ReadonlyArray<{
|
|
partitionKey: string;
|
|
name: string;
|
|
schemaFields: ReadonlyArray<{ name: string; type: string }>;
|
|
}>;
|
|
sampleRecords: ReadonlyArray<Record<string, unknown>>;
|
|
}
|
|
|
|
// Re-export for the report-builder test's redaction assertions.
|
|
export { redactPersonalData as redactForCandidateBank };
|