Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+190
@@ -0,0 +1,190 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type DsgvoCliMocks = {
|
||||
db: {
|
||||
user: {
|
||||
findUnique: ReturnType<typeof vi.fn>;
|
||||
update: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
};
|
||||
createAuditEvent: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
function getDsgvoCliMocks(): DsgvoCliMocks {
|
||||
const global = globalThis as typeof globalThis & { __dsgvoCliMocks__?: DsgvoCliMocks };
|
||||
global.__dsgvoCliMocks__ ??= {
|
||||
db: {
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
},
|
||||
createAuditEvent: vi.fn(),
|
||||
};
|
||||
return global.__dsgvoCliMocks__;
|
||||
}
|
||||
|
||||
vi.mock("@toolctrl/db/client.server", () => ({
|
||||
getDbClient: () => getDsgvoCliMocks().db,
|
||||
}));
|
||||
vi.mock("@toolctrl/db/audit-events.server", () => ({
|
||||
createAuditEvent: getDsgvoCliMocks().createAuditEvent,
|
||||
}));
|
||||
vi.mock("consola", () => ({
|
||||
default: {
|
||||
error: vi.fn(),
|
||||
success: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import consola from "consola";
|
||||
import { dsgvoDeleteCommand } from "./delete.js";
|
||||
import { dsgvoReactivateCommand } from "./reactivate.js";
|
||||
|
||||
const mocks = getDsgvoCliMocks();
|
||||
const consoleMock = vi.mocked(consola);
|
||||
const now = new Date("2026-05-25T12:00:00.000Z");
|
||||
|
||||
describe("dsgvo CLI commands", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(now);
|
||||
vi.clearAllMocks();
|
||||
process.exitCode = undefined;
|
||||
mocks.db.user.update.mockResolvedValue({ id: "user_1" });
|
||||
mocks.createAuditEvent.mockResolvedValue({ id: "audit_1" });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
process.exitCode = undefined;
|
||||
});
|
||||
|
||||
it("rejects immediate deletion without a sufficiently descriptive reason before DB writes", async () => {
|
||||
await dsgvoDeleteCommand.run?.({
|
||||
args: { user: "user_1", immediate: true, reason: "short" },
|
||||
} as never);
|
||||
|
||||
expect(consoleMock.error).toHaveBeenCalledWith("--reason is required (>=10 chars) when --immediate is used.");
|
||||
expect(process.exitCode).toBe(2);
|
||||
expect(mocks.db.user.update).not.toHaveBeenCalled();
|
||||
expect(mocks.createAuditEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("schedules deletion with grace period, tenant, actor, and audit metadata", async () => {
|
||||
await dsgvoDeleteCommand.run?.({
|
||||
args: { user: "user_1", tenant: "tenant_1", actor: "admin_1" },
|
||||
} as never);
|
||||
|
||||
const scheduledDeletionAt = new Date("2026-06-24T12:00:00.000Z");
|
||||
expect(mocks.db.user.update).toHaveBeenCalledWith({
|
||||
where: { id: "user_1" },
|
||||
data: {
|
||||
pendingDeletion: true,
|
||||
deletionRequestedAt: now,
|
||||
scheduledDeletionAt,
|
||||
deletionRequestId: expect.any(String),
|
||||
},
|
||||
});
|
||||
const requestId = mocks.db.user.update.mock.calls[0]?.[0].data.deletionRequestId;
|
||||
expect(mocks.createAuditEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tenantId: "tenant_1",
|
||||
actorId: "admin_1",
|
||||
action: "REQUEST_USER_DELETION",
|
||||
targetId: "user_1",
|
||||
metadata: expect.objectContaining({
|
||||
requestId,
|
||||
immediate: false,
|
||||
reason: null,
|
||||
scheduledDeletionAt: scheduledDeletionAt.toISOString(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith(
|
||||
`User user_1 scheduled for deletion on ${scheduledDeletionAt.toISOString()}. requestId=${requestId}`,
|
||||
);
|
||||
});
|
||||
|
||||
it("marks immediate deletion using default actor and nullable tenant audit fields", async () => {
|
||||
await dsgvoDeleteCommand.run?.({
|
||||
args: { user: "user_1", immediate: true, reason: "legal erasure request" },
|
||||
} as never);
|
||||
|
||||
expect(mocks.db.user.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
scheduledDeletionAt: now,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(mocks.createAuditEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tenantId: null,
|
||||
actorId: "cli-operator",
|
||||
metadata: expect.objectContaining({
|
||||
immediate: true,
|
||||
reason: "legal erasure request",
|
||||
scheduledDeletionAt: now.toISOString(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/^User user_1 marked for immediate deletion \(next cron pass\)\. requestId=.+$/u),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports missing or non-pending users during reactivation without writing updates", async () => {
|
||||
mocks.db.user.findUnique.mockResolvedValueOnce(null);
|
||||
|
||||
await dsgvoReactivateCommand.run?.({ args: { user: "missing" } } as never);
|
||||
|
||||
expect(consoleMock.error).toHaveBeenCalledWith("User missing not found.");
|
||||
expect(process.exitCode).toBe(1);
|
||||
expect(mocks.db.user.update).not.toHaveBeenCalled();
|
||||
|
||||
process.exitCode = undefined;
|
||||
mocks.db.user.findUnique.mockResolvedValueOnce({ pendingDeletion: false });
|
||||
|
||||
await dsgvoReactivateCommand.run?.({ args: { user: "user_1" } } as never);
|
||||
|
||||
expect(consoleMock.warn).toHaveBeenCalledWith("User user_1 is not pendingDeletion; nothing to do.");
|
||||
expect(process.exitCode).toBe(1);
|
||||
expect(mocks.db.user.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reactivates a pending user and records cancellation audit metadata", async () => {
|
||||
mocks.db.user.findUnique.mockResolvedValueOnce({ pendingDeletion: true });
|
||||
|
||||
await dsgvoReactivateCommand.run?.({
|
||||
args: { user: "user_1", tenant: "tenant_1", actor: "admin_1" },
|
||||
} as never);
|
||||
|
||||
expect(mocks.db.user.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: "user_1" },
|
||||
select: { pendingDeletion: true },
|
||||
});
|
||||
expect(mocks.db.user.update).toHaveBeenCalledWith({
|
||||
where: { id: "user_1" },
|
||||
data: {
|
||||
pendingDeletion: false,
|
||||
deletionRequestedAt: null,
|
||||
scheduledDeletionAt: null,
|
||||
deletionRequestId: null,
|
||||
},
|
||||
});
|
||||
expect(mocks.createAuditEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tenantId: "tenant_1",
|
||||
actorId: "admin_1",
|
||||
action: "CANCEL_USER_DELETION",
|
||||
targetId: "user_1",
|
||||
metadata: { requestId: expect.any(String), intent: "reactivate", source: "cli" },
|
||||
}),
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/^User user_1 reactivated\. requestId=.+$/u),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* `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}`,
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* `dsgvo export --user <userId> [--tenant <tenantId>]` — write a
|
||||
* REQUEST_PERSONAL_DATA_EXPORT AuditEvent for the user, mirroring the
|
||||
* self-service path. The worker picks up the request out-of-band and
|
||||
* emails the user the signed download URL. The CLI command is the
|
||||
* operator-equivalent escape hatch for tickets the user could not
|
||||
* complete via the web UI.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
export const dsgvoExportCommand = defineCommand({
|
||||
meta: { name: "export", description: "Trigger a personal-data export for a user." },
|
||||
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 { createAuditEvent } = await import("@toolctrl/db/audit-events.server");
|
||||
await createAuditEvent({
|
||||
tenantId,
|
||||
actorId,
|
||||
actorType: "admin",
|
||||
action: "REQUEST_PERSONAL_DATA_EXPORT",
|
||||
targetType: "User",
|
||||
targetId: userId,
|
||||
regulatoryScope: ["gdpr"],
|
||||
dataSensitivity: "pii",
|
||||
outcome: "success",
|
||||
metadata: { requestId, intent: "export", source: "cli" },
|
||||
});
|
||||
consola.success(`Personal-data export queued for user=${userId} requestId=${requestId}`);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* `dsgvo` — operator commands for the DSGVO/GDPR bulk-operations toolkit
|
||||
* (Phase 10 #68).
|
||||
*
|
||||
* Subcommands:
|
||||
* export Trigger a personal-data export for a user (Art. 15 / 20).
|
||||
* delete Schedule a user for deletion (Art. 17). Respects the
|
||||
* configured grace period unless --immediate is passed.
|
||||
* verify Run the cross-table verifier-pass for a user (post-cascade
|
||||
* sanity check that no straggler rows remain).
|
||||
* reactivate Cancel a pending deletion before the grace window elapses.
|
||||
*
|
||||
* All commands are thin shims around the existing workers /
|
||||
* audit-event-driven dispatch path — the CLI just records the request
|
||||
* and prints the resulting job/audit ids. The actual cascade always
|
||||
* runs in the worker so the audit + per-table-strategy invariants stay
|
||||
* in one code path.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import { dsgvoExportCommand } from "./export.js";
|
||||
import { dsgvoDeleteCommand } from "./delete.js";
|
||||
import { dsgvoVerifyCommand } from "./verify.js";
|
||||
import { dsgvoReactivateCommand } from "./reactivate.js";
|
||||
|
||||
export const dsgvoCommand = defineCommand({
|
||||
meta: {
|
||||
name: "dsgvo",
|
||||
description: "DSGVO/GDPR operator helpers (export, delete, verify, reactivate).",
|
||||
},
|
||||
subCommands: {
|
||||
export: dsgvoExportCommand,
|
||||
delete: dsgvoDeleteCommand,
|
||||
verify: dsgvoVerifyCommand,
|
||||
reactivate: dsgvoReactivateCommand,
|
||||
},
|
||||
});
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* `dsgvo reactivate --user <id>` — 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("@toolctrl/db/client.server"),
|
||||
import("@toolctrl/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}`);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* `dsgvo verify --user <id>` — run the cross-table verifier-pass for a
|
||||
* user after a deletion-cascade, printing any straggler rows that still
|
||||
* reference the user. Useful when an operator wants a quick "are we
|
||||
* sure they're gone?" check without waiting for the scheduled verifier
|
||||
* worker run (~1h after the cascade).
|
||||
*
|
||||
* The actual scan logic lives in the worker (apps/worker/src/jobs/
|
||||
* dsgvo-verify.ts). The CLI just records the request as an AuditEvent
|
||||
* with action=VERIFY_USER_DELETION so the worker can pick it up.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
export const dsgvoVerifyCommand = defineCommand({
|
||||
meta: { name: "verify", description: "Schedule a verifier-pass for a deleted user." },
|
||||
args: {
|
||||
user: { type: "string", required: true, description: "Target User.id (already deleted/pseudonymized)." },
|
||||
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 { createAuditEvent } = await import("@toolctrl/db/audit-events.server");
|
||||
await createAuditEvent({
|
||||
tenantId,
|
||||
actorId,
|
||||
actorType: "admin",
|
||||
action: "VERIFY_USER_DELETION",
|
||||
targetType: "User",
|
||||
targetId: userId,
|
||||
regulatoryScope: ["gdpr"],
|
||||
dataSensitivity: "pii",
|
||||
outcome: "success",
|
||||
metadata: { requestId, intent: "verify", source: "cli" },
|
||||
});
|
||||
consola.success(`Verifier-pass queued for user=${userId} requestId=${requestId}`);
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user