Skip to content

Commit

Permalink
include purged entities in the response
Browse files Browse the repository at this point in the history
  • Loading branch information
sadiqkhoja committed Jan 6, 2025
1 parent dcf05cc commit fbc56f0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,17 @@ const idFilter = (options) => {
const _getAllEntitiesState = (datasetId, options) => sql`
SELECT uuid, "deletedAt" IS NOT NULL as deleted
FROM entities
WHERE "datasetId" = ${datasetId} AND ${idFilter(options)}
-- union with purged
WHERE "datasetId" = ${datasetId}
AND ${idFilter(options)}
UNION
SELECT uuid, deleted FROM (
SELECT jsonb_array_elements_text(details -> 'entityUuids') AS uuid, TRUE as deleted
FROM audits
JOIN datasets ON datasets."acteeId" = audits."acteeId"
WHERE action = 'entities.purge'
AND datasets.id = ${datasetId}
) purged
WHERE ${idFilter(options)}
-- union with not approved
`;

Expand Down
70 changes: 66 additions & 4 deletions test/integration/api/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ describe('Entities API', () => {
}));
});

// OpenRosa endpoint
describe('GET /datasets/:name/integrity', () => {
it('should return notfound if the dataset does not exist', testEntities(async (service) => {
const asAlice = await service.login('alice');
Expand Down Expand Up @@ -333,10 +334,71 @@ describe('Entities API', () => {
const result = await xml2js.parseStringPromise(text, { explicitArray: false });
result.data.entities.entity.length.should.be.eql(2);
const [first, second] = result.data.entities.entity;
first.$.id.should.be.eql('12345678-1234-4123-8234-123456789abc');
first.deleted.should.be.eql('true');
second.$.id.should.be.eql('12345678-1234-4123-8234-123456789aaa');
second.deleted.should.be.eql('false');
first.$.id.should.be.eql('12345678-1234-4123-8234-123456789aaa');
first.deleted.should.be.eql('false');
second.$.id.should.be.eql('12345678-1234-4123-8234-123456789abc');
second.deleted.should.be.eql('true');
});
}));

it('should return purged entities as well', testEntities(async (service, { Entities }) => {
const asAlice = await service.login('alice');

await asAlice.delete('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc')
.expect(200);

await Entities.purge(true);

await asAlice.get(`/v1/projects/1/datasets/people/integrity`)
.set('X-OpenRosa-Version', '1.0')
.expect(200)
.then(async ({ text }) => {
const result = await xml2js.parseStringPromise(text, { explicitArray: false });
result.data.entities.entity.length.should.be.eql(2);
const [first, second] = result.data.entities.entity;
first.$.id.should.be.eql('12345678-1234-4123-8234-123456789aaa');
first.deleted.should.be.eql('false');
second.$.id.should.be.eql('12345678-1234-4123-8234-123456789abc');
second.deleted.should.be.eql('true');
});
}));

it('should return only queried entities', testEntities(async (service) => {
const asAlice = await service.login('alice');

await asAlice.delete('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc')
.expect(200);

await asAlice.get(`/v1/projects/1/datasets/people/integrity?id=12345678-1234-4123-8234-123456789abc`)
.set('X-OpenRosa-Version', '1.0')
.expect(200)
.then(async ({ text }) => {
const result = await xml2js.parseStringPromise(text, { explicitArray: false });
const { entity } = result.data.entities;
entity.$.id.should.be.eql('12345678-1234-4123-8234-123456789abc');
entity.deleted.should.be.eql('true');
});
}));

it('should return only queried purged entities', testEntities(async (service, { Entities }) => {
const asAlice = await service.login('alice');

await asAlice.delete('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc')
.expect(200);

await asAlice.delete('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789aaa')
.expect(200);

await Entities.purge(true);

await asAlice.get(`/v1/projects/1/datasets/people/integrity?id=12345678-1234-4123-8234-123456789abc`)
.set('X-OpenRosa-Version', '1.0')
.expect(200)
.then(async ({ text }) => {
const result = await xml2js.parseStringPromise(text, { explicitArray: false });
const { entity } = result.data.entities;
entity.$.id.should.be.eql('12345678-1234-4123-8234-123456789abc');
entity.deleted.should.be.eql('true');
});
}));
});
Expand Down

0 comments on commit fbc56f0

Please sign in to comment.