Skip to content

Commit

Permalink
Dont force process a sub for a deleted entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Aug 23, 2024
1 parent 23715cf commit 9724f46
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,21 @@ const _updateEntity = (dataset, entityData, submissionId, submissionDef, submiss

// Figure out the intended baseVersion
// If this is an offline update with a branchId, the baseVersion value is local to that offline context.
const baseEntityDef = await Entities._computeBaseVersion(event.id, dataset, clientEntity, submissionDef, forceOutOfOrderProcessing);
let baseEntityDef;

// Try computing base version.
// But if there is a 404.8 not found error, double-check if the entity never existed or was deleted.
try {
baseEntityDef = await Entities._computeBaseVersion(event.id, dataset, clientEntity, submissionDef, forceOutOfOrderProcessing);
} catch (err) {
if (err.problemCode === 404.8) {
// Look up deleted entity by passing deleted as option argData
const deletedEntity = await Entities.getById(dataset.id, clientEntity.uuid, new QueryOptions({ argData: { deleted: 'true' } }));
if (deletedEntity.isDefined())
throw Problem.user.entityDeleted({ entityUuid: clientEntity.uuid });
}
throw err;
}

// If baseEntityVersion is null, we held a submission and will stop processing now.
if (baseEntityDef == null)
Expand Down
3 changes: 3 additions & 0 deletions lib/util/problem.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ const problems = {
// entity base version specified in submission does not exist
entityVersionNotFound: problem(404.9, ({ baseVersion, entityUuid, datasetName }) => `Base version (${baseVersion}) does not exist for entity UUID (${entityUuid}) in dataset (${datasetName}).`),

// entity has been deleted
entityDeleted: problem(404.11, ({ entityUuid }) => `The entity with UUID (${entityUuid}) has been deleted.`),

// { allowed: [ acceptable formats ], got }
unacceptableFormat: problem(406.1, ({ allowed }) => `Requested format not acceptable; this resource allows: ${allowed.join()}.`),

Expand Down
37 changes: 36 additions & 1 deletion test/integration/api/offline-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,42 @@ describe('Offline Entities', () => {
});
}));

// TODO: check deleted entity
it('should not process a submission for an entity that has been soft-deleted', testOfflineEntities(async (service, container) => {
const asAlice = await service.login('alice');
const branchId = uuid();

// Send the first submission, which will be held in the backlog because the base version is high
await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions')
.send(testData.instances.offlineEntity.one
.replace('branchId=""', `branchId="${branchId}"`)
.replace('baseVersion="1"', 'baseVersion="2"')
)
.set('Content-Type', 'application/xml')
.expect(200);

await exhaust(container);

let backlogCount = await container.oneFirst(sql`select count(*) from entity_submission_backlog`);
backlogCount.should.equal(1);

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

// Process the backlog (count will be 1 but update should not be applied to entity)
const processedCount = await container.Entities.processBacklog(true);
processedCount.should.equal(1);

// Check that the backlog count is now 0
backlogCount = await container.oneFirst(sql`select count(*) from entity_submission_backlog`);
backlogCount.should.equal(0);

// Check for an entity error on the submission
await asAlice.get('/v1/projects/1/forms/offlineEntity/submissions/one/audits')
.expect(200)
.then(({ body }) => {
body[0].details.errorMessage.should.eql('The entity with UUID (12345678-1234-4123-8234-123456789abc) has been deleted.');
});
}));
});
});
});

0 comments on commit 9724f46

Please sign in to comment.