From 1415c932db55b6df5def26046d3743bfb0687c29 Mon Sep 17 00:00:00 2001 From: Matthew White Date: Mon, 11 Dec 2023 22:31:54 -0500 Subject: [PATCH] Exclude deleted entities from extended metadata about entity list --- lib/model/query/datasets.js | 1 + test/integration/api/datasets.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/model/query/datasets.js b/lib/model/query/datasets.js index 85ef64ce1..3f74ba13c 100644 --- a/lib/model/query/datasets.js +++ b/lib/model/query/datasets.js @@ -195,6 +195,7 @@ const _get = extender(Dataset)(Dataset.Extended)((fields, extend, options, publi LEFT JOIN ( SELECT "datasetId", COUNT(1) "entities", MAX(COALESCE("updatedAt", "createdAt")) "lastEntity", COUNT(1) FILTER (WHERE conflict IS NOT NULL) "conflicts" FROM entities e + WHERE "deletedAt" IS NULL GROUP BY "datasetId" ) stats on stats."datasetId" = datasets.id`} ${(actorId == null) ? sql`` : sql` diff --git a/test/integration/api/datasets.js b/test/integration/api/datasets.js index 7b89a8bca..b4a423fb9 100644 --- a/test/integration/api/datasets.js +++ b/test/integration/api/datasets.js @@ -218,6 +218,38 @@ describe('datasets and entities', () => { }); })); + it('should exclude deleted entities', testService(async (service) => { + const asAlice = await service.login('alice'); + await asAlice.post('/v1/projects/1/forms?publish=true') + .send(testData.forms.simpleEntity) + .set('Content-Type', 'application/xml') + .expect(200); + await asAlice.post('/v1/projects/1/datasets/people/entities') + .send({ + uuid: '12345678-1234-4123-8234-123456789abc', + label: 'Johnny Doe' + }) + .expect(200); + await asAlice.get('/v1/projects/1/datasets') + .set('X-Extended-Metadata', 'true') + .expect(200) + .then(({ body }) => { + body.length.should.equal(1); + body[0].entities.should.equal(1); + body[0].lastEntity.should.be.an.isoDate(); + }); + await asAlice.delete('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200); + await asAlice.get('/v1/projects/1/datasets') + .set('X-Extended-Metadata', 'true') + .expect(200) + .then(({ body }) => { + body.length.should.equal(1); + body[0].entities.should.equal(0); + should.not.exist(body[0].lastEntity); + }); + })); + it('should return the correct count for multiple dataset', testService(async (service, container) => { const asAlice = await service.login('alice');