From 5be8b33f96e0b3da6a550b931ee6c672504cb7ba Mon Sep 17 00:00:00 2001 From: Sadiq Khoja Date: Fri, 3 Jan 2025 12:52:54 -0500 Subject: [PATCH] insert acteeId for the purged entities --- lib/model/query/entities.js | 5 +++-- test/integration/other/entities-purging.js | 25 ++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/model/query/entities.js b/lib/model/query/entities.js index ae6b2cc46..b6b69b64b 100644 --- a/lib/model/query/entities.js +++ b/lib/model/query/entities.js @@ -933,11 +933,12 @@ const purge = (force = false, projectId = null, datasetName = null, entityUuid = WHERE (audits.details->'entity'->>'uuid') = entities.uuid AND ${_trashedFilter(force, projectId, datasetName, entityUuid)} ), purge_audit AS ( - INSERT INTO audits ("action", "loggedAt", "processed", "details") - SELECT 'entities.purge', clock_timestamp(), clock_timestamp(), jsonb_build_object('entitiesDeleted', COUNT(*), 'entityUuids', ARRAY_AGG(uuid)) + INSERT INTO audits ("action", "acteeId", "loggedAt", "processed", "details") + SELECT 'entities.purge', datasets."acteeId", clock_timestamp(), clock_timestamp(), jsonb_build_object('entitiesDeleted', COUNT(*), 'entityUuids', ARRAY_AGG(uuid)) FROM entities JOIN datasets ON entities."datasetId" = datasets.id WHERE ${_trashedFilter(force, projectId, datasetName, entityUuid)} + GROUP BY datasets."acteeId" HAVING count(*) > 0 ), delete_entity_sources AS ( DELETE FROM entity_def_sources diff --git a/test/integration/other/entities-purging.js b/test/integration/other/entities-purging.js index 57b247ddb..61e30c843 100644 --- a/test/integration/other/entities-purging.js +++ b/test/integration/other/entities-purging.js @@ -222,9 +222,9 @@ describe('query module entities purge', () => { })); }); - describe('deep cleanup of all submission artifacts', () => { + describe('deep cleanup of all entity artifacts', () => { - it('should purge all versions of a deleted submission', testService(async (service, { Entities, all }) => { + it('should purge all versions of a deleted entity', testService(async (service, { Entities, all }) => { const asAlice = await service.login('alice'); await createDataset(asAlice, 1, 'people'); @@ -411,19 +411,30 @@ describe('query module entities purge', () => { }); describe('entity.purge audit event', () => { - it('should log a purge event in the audit log when purging submissions', testService(async (service, { Entities }) => { + it('should log a purge event in the audit log when purging entities', testService(async (service, { Entities, oneFirst }) => { const asAlice = await service.login('alice'); - const uuids = await createDeletedEntities(asAlice, 2); + const peopleUuids = await createDeletedEntities(asAlice, 2); + const treesUuids = await createDeletedEntities(asAlice, 2, { datasetName: 'trees' }); + + const peopleActeeId = await oneFirst(sql`SELECT "acteeId" FROM datasets WHERE name = 'people'`); + const treesActeeId = await oneFirst(sql`SELECT "acteeId" FROM datasets WHERE name = 'trees'`); // Purge entities await Entities.purge(true); await asAlice.get('/v1/audits') .then(({ body }) => { - body.filter((a) => a.action === 'entities.purge').length.should.equal(1); - body[0].details.entitiesDeleted.should.eql(2); - body[0].details.entityUuids.should.containDeep(uuids); + const purgeLogs = body.filter((a) => a.action === 'entities.purge'); + purgeLogs.length.should.equal(2); + + const [ peoplePurgeAudit ] = purgeLogs.filter(a => a.acteeId === peopleActeeId); + peoplePurgeAudit.details.entitiesDeleted.should.eql(2); + peoplePurgeAudit.details.entityUuids.should.containDeep(peopleUuids); + + const [ treesPurgeAudit ] = purgeLogs.filter(a => a.acteeId === treesActeeId); + treesPurgeAudit.details.entitiesDeleted.should.eql(2); + treesPurgeAudit.details.entityUuids.should.containDeep(treesUuids); }); }));