Skip to content

Commit

Permalink
fixes #858: Submission frame should be reinstantiated (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadiqkhoja authored May 19, 2023
1 parent 46ac811 commit d455911
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/model/query/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ order by path asc, value asc`)

// Helper function to assign submission.currentVersionSubmitter to submission.currentVersion.submitter
// Current there is no way to create such complex object using `extender` and `unjoiner` framework functions
const assignCurrentVersionSubmitter = (x) => x.withAux('currentVersion', x.aux.currentVersion.withAux('submitter', x.aux.currentVersionSubmitter));
//
// New instance of Submission is created to remove all aux objects from it (except submitter).
// If those aux's are kept then properties of aux are merged at the root level cb#858
const assignCurrentVersionSubmitter = (x) => (new Submission(x, { submitter: x.aux.submitter })).withAux('currentVersion', x.aux.currentVersion.withAux('submitter', x.aux.currentVersionSubmitter));

const _get = extender(Submission, Submission.Def.into('currentVersion'))(Actor.into('submitter'), Actor.alias('current_version_actors', 'currentVersionSubmitter'))((fields, extend, options, projectId, xmlFormId, draft) => sql`
select ${fields} from
Expand Down
42 changes: 42 additions & 0 deletions test/integration/api/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3183,6 +3183,48 @@ one,h,/data/h,2000-01-01T00:06,2000-01-01T00:07,-5,-6,,ee,ff
body.submitter.displayName.should.equal('Alice');
})))));

// cb#858
it('should not mince object properties', testService(async (service, container) => {
const asAlice = await service.login('alice');
const asBob = await service.login('bob');

await asAlice.post('/v1/projects/1/forms/simple/submissions')
.send(testData.instances.simple.one)
.set('Content-Type', 'text/xml')
.expect(200);

await asBob.put('/v1/projects/1/forms/simple/submissions/one')
.set('Content-Type', 'text/xml')
.send(withSimpleIds('one', 'two'))
.expect(200);

await container.run(sql`UPDATE actors SET "createdAt" = '2020-01-01' WHERE "displayName" = 'Alice'`);
await container.run(sql`UPDATE actors SET "createdAt" = '2021-01-01' WHERE "displayName" = 'Bob'`);

await container.run(sql`UPDATE submissions SET "createdAt" = '2022-01-01', "updatedAt" = '2023-01-01'`);

await container.run(sql`UPDATE submission_defs SET "createdAt" = '2022-01-01' WHERE "instanceId" = 'one'`);
await container.run(sql`UPDATE submission_defs SET "createdAt" = '2023-01-01' WHERE "instanceId" = 'two'`);

await asAlice.get('/v1/projects/1/forms/simple/submissions/one')
.set('X-Extended-Metadata', 'true')
.expect(200)
.then(({ body }) => {
body.should.be.an.ExtendedSubmission();

body.createdAt.should.match(/2022/);
body.updatedAt.should.match(/2023/);

body.submitter.displayName.should.equal('Alice');
body.submitter.createdAt.should.match(/2020/);

body.currentVersion.createdAt.should.match(/2023/);

body.currentVersion.submitter.displayName.should.equal('Bob');
body.currentVersion.submitter.createdAt.should.match(/2021/);
});
}));

it('should redirect to the version if the referenced instanceID is out of date', testService((service) =>
service.login('alice', (asAlice) =>
asAlice.post('/v1/projects/1/submission')
Expand Down

0 comments on commit d455911

Please sign in to comment.