Skip to content

Commit 5c253f2

Browse files
committed
fix(objectql): credit a recycle-bin entry to the human a system delete was made for
deleted_by reads the authorized subject first, then the attributed user of a system-authorized write, the precedence the audit ledger uses. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4czFGLnXKQd6cQjv3Pr
1 parent 0a8d24a commit 5c253f2

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎packages/objectql/src/recycle-bin/capture.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ describe('a by-id delete writes ONE recycle-bin entry (ADR-0148 D1)', () => {
275275
expect(entry.deleted_by).toBeUndefined();
276276
});
277277

278+
it('credits the human a system-authorized delete was made for, as the audit ledger does', async () => {
279+
const { engine, driver } = await makeEngine();
280+
driver.seed('loner', { id: 'l1', name: 'x' });
281+
await engine.delete('loner', { where: { id: 'l1' }, context: { isSystem: true, attributedUserId: 'u_bob' } } as any);
282+
expect(entriesOf(driver)[0].deleted_by).toBe('u_bob');
283+
});
284+
278285
it('leaves the secret column out of the snapshot (D4)', async () => {
279286
const { engine, driver } = await makeEngine();
280287
seedAccountTree(driver);

‎packages/objectql/src/recycle-bin/capture.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ export class RecycleBinCapture {
224224
return undefined;
225225
}
226226
const deletedAt = new Date();
227-
const deletedBy = this.context?.userId;
227+
const deletedBy = this.deletedBy();
228228
const organizationId = this.organizationOf(rows[0].values);
229229
const clearedReferences: RecycleBinClearedReference[] = [...(this.set?.clearedReferences ?? [])];
230230
const entry: RecycleBinEntry = RecycleBinEntrySchema.parse({
231231
object_name: this.object,
232232
record_id: this.id,
233233
record_count: rows.length,
234234
snapshot: { rows, clearedReferences },
235-
...(typeof deletedBy === 'string' && deletedBy !== '' ? { deleted_by: deletedBy } : {}),
235+
...(deletedBy !== undefined ? { deleted_by: deletedBy } : {}),
236236
deleted_at: deletedAt.toISOString(),
237237
retention_days: this.retentionDays,
238238
purge_after: recycleBinPurgeAfter(deletedAt, this.retentionDays),
@@ -272,6 +272,21 @@ export class RecycleBinCapture {
272272
};
273273
}
274274

275+
/**
276+
* Who deleted the rows, as the audit ledger credits a write: the subject the
277+
* delete was authorized as, else the human credited for a delete the system
278+
* authorized on their behalf (`attributedUserId`, a better-auth-originated
279+
* write). `undefined` for the system's own deletes. It is the person whose
280+
* entries `allowRestoreOwnDeleted` lets restore.
281+
*/
282+
private deletedBy(): string | undefined {
283+
const context = this.context as { userId?: unknown; attributedUserId?: unknown } | undefined;
284+
for (const candidate of [context?.userId, context?.attributedUserId]) {
285+
if (typeof candidate === 'string' && candidate !== '') return candidate;
286+
}
287+
return undefined;
288+
}
289+
275290
/**
276291
* The organization that owns the entry: the root row's own, read off its
277292
* tenant column, else the deleting context's. An entry belongs to the

0 commit comments

Comments
 (0)