Skip to content

Commit

Permalink
Fixing more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Jun 21, 2024
1 parent 9e70f36 commit bfd6a4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/data/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ const extractTrunkVersionFromSubmission = (entity) => {

return parseInt(entity.system.trunkVersion, 10);
}
return null;
};

// TODO: write unit tests of this
const extractBranchIdFromSubmission = (entity) => {
const { branchId } = entity.system;
if (branchId === '')
return null;

if (branchId) {
const matches = _uuidPattern.exec(branchId);
if (matches == null) throw Problem.user.invalidDataTypeOfParameter({ field: 'branchId', expected: 'valid version 4 UUID' });
Expand Down
10 changes: 6 additions & 4 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ ins as (insert into entities (id, "datasetId", "uuid", "createdAt", "creatorId")
select def."entityId", ${dataset.id}, ${partial.uuid}, def."createdAt", ${creatorId} from def
returning entities.*)
select ins.*, def.id as "entityDefId" from ins, def;`)
.then(({ entityDefId, ...entityData }) => // TODO/HACK: reassemble just enough to log audit event
.then(({ entityDefId, ...entityData }) => // TODO/HACK: starting to need more reassembling
new Entity(entityData, {
currentVersion: new Entity.Def({
id: entityDefId,
entityId: entityData.id
entityId: entityData.id,
branchId: partial.def.branchId
})
}));
};
Expand Down Expand Up @@ -407,10 +408,11 @@ const _processSubmissionEvent = (event, parentEvent) => async ({ Audits, Dataset
// TODO: consider changing checkHeldSub query to check for existing version of entity
// so that return value of create/update above doesn't matter as much.
// Also consider actually checking entity uuid, in query.
if (maybeEntity != null && entityData.system.branchId != null) {
if (maybeEntity != null && maybeEntity.aux.currentVersion.branchId != null) {
const { branchId } = maybeEntity.aux.currentVersion;
// baseVersion could be '', meaning its an offline create
const currentBaseVersion = entityData.system.baseVersion === '' ? 0 : parseInt(entityData.system.baseVersion, 10);
const nextSub = await _checkHeldSubmission(maybeOne, entityData.system.branchId, currentBaseVersion + 1);
const nextSub = await _checkHeldSubmission(maybeOne, branchId, currentBaseVersion + 1);
if (nextSub.isDefined()) {
const { submissionId: nextSubmissionId, submissionDefId: nextSubmissionDefId } = nextSub.get();
await Audits.log({ id: event.actorId }, 'submission.reprocess', { acteeId: event.acteeId },
Expand Down
21 changes: 21 additions & 0 deletions test/integration/api/offline-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ const testOfflineEntities = (test) => testService(async (service, container) =>

describe('Offline Entities', () => {
describe('parsing branchId and trunkVersion from submission xml', () => {
it('should ignore trunkVersion and branchId if empty string', testOfflineEntities(async (service, container) => {
const asAlice = await service.login('alice');
const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound);

await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions')
.send(testData.instances.offlineEntity.one
.replace('trunkVersion="1"', `trunkVersion=""`)
)
.set('Content-Type', 'application/xml')
.expect(200);

await exhaust(container);

const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound);
should.not.exist(entity.aux.currentVersion.trunkVersion);
should.not.exist(entity.aux.currentVersion.branchId);
//should.not.exist(entity.aux.currentVersion.branchBaseVersion);
entity.aux.currentVersion.version.should.equal(2);
entity.aux.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' });
}));

it('should parse and save run info from sub creating an entity', testOfflineEntities(async (service, container) => {
const asAlice = await service.login('alice');
const branchId = uuid();
Expand Down

0 comments on commit bfd6a4a

Please sign in to comment.