Skip to content

Commit

Permalink
insert acteeId for the purged entities
Browse files Browse the repository at this point in the history
  • Loading branch information
sadiqkhoja committed Jan 3, 2025
1 parent 813fc25 commit 5be8b33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions test/integration/other/entities-purging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
});
}));

Expand Down

0 comments on commit 5be8b33

Please sign in to comment.