89 lines
3.4 KiB
TypeScript
89 lines
3.4 KiB
TypeScript
/**
|
|
* `dsgvo delete --user <id> [--tenant <id>] [--immediate] [--reason <text>]`
|
|
*
|
|
* Schedule a user for deletion. Default behaviour mirrors the
|
|
* self-service flow: flips User.pendingDeletion=true with
|
|
* scheduledDeletionAt = now + GRACE days (default 30), and the
|
|
* grace-period cron picks it up later.
|
|
*
|
|
* With `--immediate` the scheduledDeletionAt is set to *now* so the
|
|
* very next cron pass dispatches the cascade. We deliberately do not
|
|
* synchronously call runDsgvoDeletion from the CLI — that would
|
|
* bypass the worker's audit + per-table-strategy invariants.
|
|
*
|
|
* `--reason` is required when --immediate to satisfy the same
|
|
* audit-evidence bar as the admin UI override.
|
|
*/
|
|
import { defineCommand } from "citty";
|
|
import consola from "consola";
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
const DSGVO_GRACE_PERIOD_DAYS = Number(process.env.DSGVO_GRACE_PERIOD_DAYS ?? "30") || 30;
|
|
|
|
export const dsgvoDeleteCommand = defineCommand({
|
|
meta: { name: "delete", description: "Schedule a user for deletion (grace-period by default)." },
|
|
args: {
|
|
user: { type: "string", required: true, description: "Target User.id." },
|
|
tenant: { type: "string", required: false, description: "Tenant id stamped on the audit row." },
|
|
actor: { type: "string", required: false, description: "Operator id stamped on the audit row." },
|
|
immediate: { type: "boolean", required: false, description: "Skip grace; the next cron pass deletes the user." },
|
|
reason: { type: "string", required: false, description: "Operator reason (required with --immediate)." },
|
|
},
|
|
async run({ args }) {
|
|
const userId = String(args.user);
|
|
const tenantId = args.tenant ? String(args.tenant) : null;
|
|
const actorId = args.actor ? String(args.actor) : "cli-operator";
|
|
const immediate = Boolean(args.immediate);
|
|
const reason = args.reason ? String(args.reason) : null;
|
|
if (immediate && (!reason || reason.length < 10)) {
|
|
consola.error("--reason is required (>=10 chars) when --immediate is used.");
|
|
process.exitCode = 2;
|
|
return;
|
|
}
|
|
const now = new Date();
|
|
const scheduledDeletionAt = immediate
|
|
? now
|
|
: new Date(now.getTime() + DSGVO_GRACE_PERIOD_DAYS * 24 * 60 * 60 * 1000);
|
|
const requestId = randomUUID();
|
|
|
|
const [{ getDbClient }, { createAuditEvent }] = await Promise.all([
|
|
import("@toolctrl/db/client.server"),
|
|
import("@toolctrl/db/audit-events.server"),
|
|
]);
|
|
const prisma = getDbClient();
|
|
await prisma.user.update({
|
|
where: { id: userId },
|
|
data: {
|
|
pendingDeletion: true,
|
|
deletionRequestedAt: now,
|
|
scheduledDeletionAt,
|
|
deletionRequestId: requestId,
|
|
},
|
|
});
|
|
await createAuditEvent({
|
|
tenantId,
|
|
actorId,
|
|
actorType: "admin",
|
|
action: "REQUEST_USER_DELETION",
|
|
targetType: "User",
|
|
targetId: userId,
|
|
regulatoryScope: ["gdpr"],
|
|
dataSensitivity: "pii",
|
|
outcome: "success",
|
|
metadata: {
|
|
requestId,
|
|
intent: "delete",
|
|
source: "cli",
|
|
immediate,
|
|
reason,
|
|
scheduledDeletionAt: scheduledDeletionAt.toISOString(),
|
|
},
|
|
});
|
|
consola.success(
|
|
immediate
|
|
? `User ${userId} marked for immediate deletion (next cron pass). requestId=${requestId}`
|
|
: `User ${userId} scheduled for deletion on ${scheduledDeletionAt.toISOString()}. requestId=${requestId}`,
|
|
);
|
|
},
|
|
});
|