148 lines
11 KiB
TypeScript
148 lines
11 KiB
TypeScript
/**
|
|
* Phase 10 Task #68 — DSGVO bulk-ops user-FK map.
|
|
*
|
|
* Enumerates every Prisma model that holds a foreign key (or a logical
|
|
* reference) to `User.id`. Each entry declares an explicit cascade
|
|
* strategy used by the deletion pipeline (`dsgvo-deletion.ts`) and the
|
|
* export pipeline (`dsgvo-export.ts`).
|
|
*
|
|
* Strategies:
|
|
*
|
|
* • `hard-delete` — delete rows when the user is deleted. Used
|
|
* for join tables, ephemeral data and rows
|
|
* with no retention requirement.
|
|
*
|
|
* • `pseudonymize` — keep the row but null/replace the user FK
|
|
* with the deterministic pseudonym. Used for
|
|
* retention-bound rows such as AuditEvents,
|
|
* help-desk tickets and historical analytics
|
|
* where regulatory or business intent
|
|
* requires the record to survive deletion.
|
|
*
|
|
* • `retain-tombstone` — keep the row but set the FK to NULL
|
|
* (existing schema-level `onDelete: SetNull`
|
|
* handles the column update). Used when the
|
|
* row outlives the user but no audit reference
|
|
* is required.
|
|
*
|
|
* The companion test (`user-fk-map.test.ts`) asserts that the grep over
|
|
* `prisma/schema/*.prisma` finds exactly the relations enumerated here.
|
|
* If a future migration adds a new User-FK without updating this map,
|
|
* the test fails and the cascade silently leaking PII is blocked at
|
|
* CI time.
|
|
*
|
|
* BARE-SCALAR user references (R-P6C, 2026-06-29): columns like
|
|
* `createdByUserId` / `zustaendigerUserId` (crm.prisma) or
|
|
* `createdByUserId` / `assignedToUserId` (tasks.prisma, hr.prisma) are
|
|
* intentionally NOT enumerated here. They carry no DB-level `@relation`
|
|
* to `User`, so there is no FK to cascade and the companion test's grep
|
|
* (which matches `@relation ... references: [id]` to `User`) does not see
|
|
* them — by design, repo-wide. They are metadata references; after user
|
|
* deletion they become dangling cuids (no PII payload). Project-wide
|
|
* pseudonymization of bare-scalar user references is a separate
|
|
* cross-cutting hardening topic (not CRM-specific) — to be addressed when
|
|
* the DSGVO deletion flow goes live, consistently across all modules.
|
|
*/
|
|
|
|
export type DsgvoCascadeStrategy = "hard-delete" | "pseudonymize" | "retain-tombstone";
|
|
|
|
export interface UserFkMapEntry {
|
|
/** Prisma model name (matches `model X { ... }`). */
|
|
model: string;
|
|
/** Foreign-key column on `model` that references `User.id`. */
|
|
column: string;
|
|
/** Cascade strategy applied by the deletion worker. */
|
|
strategy: DsgvoCascadeStrategy;
|
|
/** Schema source file (debug/audit aid). */
|
|
source: string;
|
|
/** Whether this table is included in the personal-data export bundle. */
|
|
includeInExport: boolean;
|
|
}
|
|
|
|
/**
|
|
* Sorted alphabetically by `model` + `column` so diffs against future
|
|
* additions stay reviewable.
|
|
*/
|
|
export const USER_FK_MAP: ReadonlyArray<UserFkMapEntry> = Object.freeze([
|
|
// P19 admin tenant notes. Schema declares `author User? @relation(..., onDelete: SetNull)`
|
|
// → `retain-tombstone` (note survives, FK nulled). `includeInExport: false`: an operator
|
|
// note *about a tenant* is an internal admin record, not the author's personal data, so it
|
|
// stays out of the author's export bundle. TODO(VR-P31): confirm DSGVO intent (retain-tombstone sign-off).
|
|
{ model: "AdminTenantNote", column: "authorUserId", strategy: "retain-tombstone", source: "admin.prisma", includeInExport: false },
|
|
{ model: "AdminUser", column: "userId", strategy: "hard-delete", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "AnalyticsUniqueVisitor", column: "userId", strategy: "pseudonymize", source: "analytics.prisma", includeInExport: false },
|
|
{ model: "ApiKey", column: "createdByUserId", strategy: "hard-delete", source: "core.prisma", includeInExport: true },
|
|
// R-P5: Apps-Engine uses bare String scalars for createdByUserId (AD-21, like
|
|
// tasks/files/hr) — no User @relation, so no USER_FK_MAP entry (retain by default).
|
|
// AuditEvent has no FK relation in the schema (actorId is a free
|
|
// string column) — we list it here as a *logical* reference. The
|
|
// pseudonymization strategy is mandatory: SOC2-CC6.1 requires the
|
|
// audit trail to survive user deletion, only its PII fields are
|
|
// rewritten.
|
|
{ model: "AuditEvent", column: "actorId", strategy: "pseudonymize", source: "core.prisma", includeInExport: true },
|
|
{ model: "Credit", column: "userId", strategy: "pseudonymize", source: "subscriptions.prisma", includeInExport: true },
|
|
{ model: "DeviceToken", column: "userId", strategy: "hard-delete", source: "notifications.prisma", includeInExport: false },
|
|
{ model: "EmailRead", column: "userId", strategy: "hard-delete", source: "email.prisma", includeInExport: true },
|
|
{ model: "EntityView", column: "createdByUserId", strategy: "hard-delete", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "EntityView", column: "userId", strategy: "hard-delete", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "Event", column: "userId", strategy: "pseudonymize", source: "core.prisma", includeInExport: true },
|
|
{ model: "Feedback", column: "userId", strategy: "pseudonymize", source: "helpDesk.prisma", includeInExport: true },
|
|
{ model: "FormulaLog", column: "userId", strategy: "pseudonymize", source: "entityBuilder.prisma", includeInExport: false },
|
|
{ model: "Group", column: "createdByUserId", strategy: "retain-tombstone", source: "permissions.prisma", includeInExport: true },
|
|
{ model: "GroupUser", column: "userId", strategy: "hard-delete", source: "permissions.prisma", includeInExport: true },
|
|
{ model: "ImpersonationAudit", column: "actorUserId", strategy: "pseudonymize", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "ImpersonationAudit", column: "targetUserId", strategy: "pseudonymize", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "KnowledgeBaseArticle", column: "createdByUserId", strategy: "pseudonymize", source: "knowledgeBase.prisma", includeInExport: true },
|
|
{ model: "Log", column: "userId", strategy: "pseudonymize", source: "core.prisma", includeInExport: true },
|
|
{ model: "MagicLinkToken", column: "userId", strategy: "hard-delete", source: "accounts.prisma", includeInExport: false },
|
|
{ model: "MetricLog", column: "userId", strategy: "pseudonymize", source: "core.prisma", includeInExport: false },
|
|
{ model: "NotificationDelivery", column: "userId", strategy: "pseudonymize", source: "notifications.prisma", includeInExport: true },
|
|
{ model: "OnboardingSession", column: "userId", strategy: "hard-delete", source: "onboarding.prisma", includeInExport: true },
|
|
{ model: "Portal", column: "createdByUserId", strategy: "retain-tombstone", source: "portals.prisma", includeInExport: true },
|
|
{ model: "Row", column: "createdByUserId", strategy: "pseudonymize", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "RowComment", column: "createdByUserId", strategy: "pseudonymize", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "RowCommentReaction", column: "createdByUserId", strategy: "hard-delete", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "RowPermission", column: "userId", strategy: "hard-delete", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "RowTask", column: "assignedToUserId", strategy: "retain-tombstone", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "RowTask", column: "completedByUserId", strategy: "retain-tombstone", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "RowTask", column: "createdByUserId", strategy: "pseudonymize", source: "entityBuilder.prisma", includeInExport: true },
|
|
{ model: "Site", column: "createdByUserId", strategy: "retain-tombstone", source: "sites.prisma", includeInExport: true },
|
|
// Help-desk staff/author FKs (schema `onDelete: SetNull`) → retain-tombstone; the ticket
|
|
// *owner* (`userId`) stays pseudonymize per the help-desk-retention note above.
|
|
// TODO(VR-P31): confirm DSGVO intent for assignee/message-author rows.
|
|
{ model: "SupportTicket", column: "assignedToUserId", strategy: "retain-tombstone", source: "helpDesk.prisma", includeInExport: true },
|
|
{ model: "SupportTicket", column: "userId", strategy: "pseudonymize", source: "helpDesk.prisma", includeInExport: true },
|
|
{ model: "SupportTicketMessage", column: "authorUserId", strategy: "retain-tombstone", source: "admin.prisma", includeInExport: true },
|
|
{ model: "TenantIpAddress", column: "userId", strategy: "hard-delete", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "TenantUser", column: "userId", strategy: "hard-delete", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "TenantUserInvitation", column: "acceptedByUserId", strategy: "retain-tombstone", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "TenantUserInvitation", column: "createdUserId", strategy: "retain-tombstone", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "TenantUserInvitation", column: "fromUserId", strategy: "retain-tombstone", source: "accounts.prisma", includeInExport: true },
|
|
{ model: "TenantUserInvitation", column: "revokedByUserId", strategy: "retain-tombstone", source: "accounts.prisma", includeInExport: true },
|
|
// Auth session/device artifacts (R-P2b). They already declare
|
|
// `onDelete: Cascade`, so hard-delete is the matching DSGVO strategy; not user
|
|
// content, so excluded from export.
|
|
{ model: "TrustedDevice", column: "userId", strategy: "hard-delete", source: "auth.prisma", includeInExport: false },
|
|
{ model: "UserNotificationPreference", column: "userId", strategy: "hard-delete", source: "notifications.prisma", includeInExport: true },
|
|
{ model: "UserRole", column: "userId", strategy: "hard-delete", source: "permissions.prisma", includeInExport: true },
|
|
{ model: "UserSession", column: "userId", strategy: "hard-delete", source: "auth.prisma", includeInExport: false },
|
|
{ model: "Workflow", column: "createdByUserId", strategy: "retain-tombstone", source: "workflows.prisma", includeInExport: true },
|
|
{ model: "WorkflowCredential", column: "userId", strategy: "retain-tombstone", source: "workflows.prisma", includeInExport: true },
|
|
{ model: "WorkflowExecution", column: "createdByUserId", strategy: "pseudonymize", source: "workflows.prisma", includeInExport: false },
|
|
{ model: "WorkflowVariable", column: "userId", strategy: "retain-tombstone", source: "workflows.prisma", includeInExport: true },
|
|
]);
|
|
|
|
export const USER_FK_MAP_BY_STRATEGY = Object.freeze({
|
|
hardDelete: USER_FK_MAP.filter((e) => e.strategy === "hard-delete"),
|
|
pseudonymize: USER_FK_MAP.filter((e) => e.strategy === "pseudonymize"),
|
|
retainTombstone: USER_FK_MAP.filter((e) => e.strategy === "retain-tombstone"),
|
|
});
|
|
|
|
export function getUserFkEntriesForExport(): readonly UserFkMapEntry[] {
|
|
return USER_FK_MAP.filter((entry) => entry.includeInExport);
|
|
}
|
|
|
|
export function findUserFkEntry(model: string, column: string): UserFkMapEntry | undefined {
|
|
return USER_FK_MAP.find((entry) => entry.model === model && entry.column === column);
|
|
}
|