113 lines
3.3 KiB
TypeScript
113 lines
3.3 KiB
TypeScript
/**
|
|
* Phase 10 Task #68 — DSGVO deletion-verifier worker.
|
|
*
|
|
* Runs ~1 hour after the deletion cascade. For every USER_FK_MAP entry
|
|
* with strategy=`hard-delete`, queries the table for any straggler row
|
|
* still pointing at the deleted user. Tables in the `pseudonymize` and
|
|
* `retain-tombstone` buckets are skipped — these intentionally keep
|
|
* rows.
|
|
*
|
|
* A clean run emits an AuditEvent with outcome=success and metadata
|
|
* `{ tablesChecked, straggler: 0 }`. A run with any leftover rows emits
|
|
* an AuditEvent with outcome=failure and the per-table breakdown so the
|
|
* operator can escalate the incident.
|
|
*/
|
|
|
|
import { USER_FK_MAP, type UserFkMapEntry } from "@toolctrl/db/dsgvo/user-fk-map";
|
|
|
|
export type DsgvoVerifyJobInput = {
|
|
userId: string;
|
|
tenantId: string | null;
|
|
requestId: string;
|
|
};
|
|
|
|
export type DsgvoVerifyTableResult = {
|
|
model: string;
|
|
column: string;
|
|
remainingRows: number;
|
|
};
|
|
|
|
export type DsgvoVerifyResult = {
|
|
userId: string;
|
|
requestId: string;
|
|
ok: boolean;
|
|
tablesChecked: number;
|
|
failures: DsgvoVerifyTableResult[];
|
|
};
|
|
|
|
export type DsgvoVerifyClient = {
|
|
countRemaining: (entry: UserFkMapEntry, userId: string) => Promise<number>;
|
|
};
|
|
|
|
export type DsgvoVerifyDependencies = {
|
|
client: DsgvoVerifyClient;
|
|
writeAuditEvent: (input: {
|
|
tenantId: string | null;
|
|
actorId: string;
|
|
actorType: string;
|
|
action: string;
|
|
targetType: string;
|
|
targetId: string;
|
|
regulatoryScope: string[];
|
|
dataSensitivity: "pii";
|
|
outcome: "success" | "failure";
|
|
metadata: Record<string, unknown>;
|
|
}) => Promise<void>;
|
|
raiseEscalation?: (input: { userId: string; failures: DsgvoVerifyTableResult[] }) => Promise<void>;
|
|
logger: { info: (fields: Record<string, unknown>, msg: string) => void; warn?: (fields: Record<string, unknown>, msg: string) => void };
|
|
};
|
|
|
|
export async function runDsgvoVerify(
|
|
deps: DsgvoVerifyDependencies,
|
|
job: DsgvoVerifyJobInput,
|
|
): Promise<DsgvoVerifyResult> {
|
|
const hardDeleteEntries = USER_FK_MAP.filter((e) => e.strategy === "hard-delete");
|
|
const failures: DsgvoVerifyTableResult[] = [];
|
|
|
|
for (const entry of hardDeleteEntries) {
|
|
const remaining = await deps.client.countRemaining(entry, job.userId);
|
|
if (remaining > 0) {
|
|
failures.push({ model: entry.model, column: entry.column, remainingRows: remaining });
|
|
}
|
|
}
|
|
|
|
const ok = failures.length === 0;
|
|
|
|
await deps.writeAuditEvent({
|
|
tenantId: job.tenantId,
|
|
actorId: "system",
|
|
actorType: "system",
|
|
action: "VERIFY_USER_DELETION",
|
|
targetType: "User",
|
|
targetId: job.userId,
|
|
regulatoryScope: ["gdpr"],
|
|
dataSensitivity: "pii",
|
|
outcome: ok ? "success" : "failure",
|
|
metadata: {
|
|
requestId: job.requestId,
|
|
tablesChecked: hardDeleteEntries.length,
|
|
stragglerTables: failures,
|
|
},
|
|
});
|
|
|
|
if (!ok) {
|
|
deps.logger.warn?.(
|
|
{ userId: job.userId, requestId: job.requestId, failureCount: failures.length },
|
|
"dsgvo-verify.stragglers-found",
|
|
);
|
|
if (deps.raiseEscalation) {
|
|
await deps.raiseEscalation({ userId: job.userId, failures });
|
|
}
|
|
} else {
|
|
deps.logger.info({ userId: job.userId, requestId: job.requestId }, "dsgvo-verify.clean");
|
|
}
|
|
|
|
return {
|
|
userId: job.userId,
|
|
requestId: job.requestId,
|
|
ok,
|
|
tablesChecked: hardDeleteEntries.length,
|
|
failures,
|
|
};
|
|
}
|