Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Log the daily job's account deletion as the system's

Claude083b7f87becc
diff --git a/src/lib/account-purge.dbtest.ts b/src/lib/account-purge.dbtest.ts
index 5c0ad16..93fa11b 100644
--- a/src/lib/account-purge.dbtest.ts
+++ b/src/lib/account-purge.dbtest.ts
@@ -282,3 +282,45 @@ test("with no administrator to take it, an envelope completed during the deletio
);
}
});
+
+test("the daily job's account deletion is logged as the system's, not a person's", async () => {
+ const leaver = await createUser();
+
+ await closeAccount(leaver);
+
+ // The JSON audit lines go to console.info once the change commits.
+ const lines: Record<string, unknown>[] = [];
+ const originalInfo = console.info;
+
+ console.info = (...args: unknown[]) => {
+ if (typeof args[0] === "string") {
+ try {
+ lines.push(JSON.parse(args[0]) as Record<string, unknown>);
+ } catch {
+ // Not a JSON line.
+ }
+ }
+ };
+
+ try {
+ const summary = await runRetentionSweep({ database, storageService });
+
+ assert.ok(!summary.accountPurgeFailures.includes(leaver));
+ } finally {
+ console.info = originalInfo;
+ }
+
+ assert.equal(await accountExists(leaver), false);
+
+ const deletion = lines.find(
+ (line) =>
+ line.trail === "site" &&
+ line.event === "account_deleted" &&
+ line.targetUserId === leaver,
+ );
+
+ assert.ok(deletion, "no audit line for the account deletion");
+ assert.equal(deletion.actorType, "system");
+ assert.equal(deletion.actorUserId, null);
+ assert.equal(deletion.actorEmail, null);
+});
diff --git a/src/lib/retention.ts b/src/lib/retention.ts
index 6d21aa0..715bffd 100644
--- a/src/lib/retention.ts
+++ b/src/lib/retention.ts
@@ -635,6 +635,9 @@ async function deleteAccountRows(
await recordSystemAuditEvent(
{
actorEmail: null,
+ // The daily job deletes it, not a person. The account's own
+ // account_closure_requested event names who asked.
+ actorType: "system",
actorUserId: null,
event: "account_deleted",
metadata: {
diff --git a/src/lib/services/system-audit.ts b/src/lib/services/system-audit.ts
index 33fa381..3f9b1d3 100644
--- a/src/lib/services/system-audit.ts
+++ b/src/lib/services/system-audit.ts
@@ -63,8 +63,17 @@ export type SystemAuditEvent = {
userAgent: string | null;
};
+export type SystemAuditActorType = "system" | "user";
+
export type RecordSystemAuditEventInput = {
actorEmail?: string | null;
+ /**
+ * Who did it, for the JSON audit line only (it is not a column and is
+ * not hashed). "system" is the install's own scheduled jobs, such as the
+ * daily cleanup deleting a closed account. Left out, it is "user": a
+ * person did it, even an unknown one, such as a failed sign-in.
+ */
+ actorType?: SystemAuditActorType;
actorUserId?: string | null;
createdAt?: number;
event: SystemAuditEventType;
@@ -155,7 +164,7 @@ async function insertChained(
afterCommit(database, () => {
logAuditRecord({
...fields,
- actorType: "user",
+ actorType: input.actorType ?? "user",
rowHash,
trail: "site",
});