/** * `dsgvo reactivate --user ` — cancel a pending deletion. Clears * User.pendingDeletion + scheduledDeletionAt + deletionRequestedAt + * deletionRequestId and writes a CANCEL_USER_DELETION audit row. * * Fails (exit 1) if the user is not currently pendingDeletion=true. */ import { defineCommand } from "citty"; import consola from "consola"; import { randomUUID } from "node:crypto"; export const dsgvoReactivateCommand = defineCommand({ meta: { name: "reactivate", description: "Cancel a pending user deletion." }, 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." }, }, 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 requestId = randomUUID(); const [{ getDbClient }, { createAuditEvent }] = await Promise.all([ import("@hivegrid/db/client.server"), import("@hivegrid/db/audit-events.server"), ]); const prisma = getDbClient(); const existing = await prisma.user.findUnique({ where: { id: userId }, select: { pendingDeletion: true }, }); if (!existing) { consola.error(`User ${userId} not found.`); process.exitCode = 1; return; } if (!existing.pendingDeletion) { consola.warn(`User ${userId} is not pendingDeletion; nothing to do.`); process.exitCode = 1; return; } await prisma.user.update({ where: { id: userId }, data: { pendingDeletion: false, deletionRequestedAt: null, scheduledDeletionAt: null, deletionRequestId: null, }, }); await createAuditEvent({ tenantId, actorId, actorType: "admin", action: "CANCEL_USER_DELETION", targetType: "User", targetId: userId, regulatoryScope: ["gdpr"], dataSensitivity: "pii", outcome: "success", metadata: { requestId, intent: "reactivate", source: "cli" }, }); consola.success(`User ${userId} reactivated. requestId=${requestId}`); }, });