Skip to content

Commit

Permalink
working with published and draft forms separately
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Oct 8, 2024
1 parent 929d8bf commit e0a1670
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 30 deletions.
15 changes: 12 additions & 3 deletions lib/worker/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ const updatePublish = pushFormToEnketo;

const updateEntitiesVersion = async ({ Forms }, event) => {
const { projectId, xmlFormId } = await Forms.getByActeeIdForUpdate(event.acteeId).then(o => o.get());
const form = await Forms.getByProjectAndXmlFormId(projectId, xmlFormId, true).then(o => o.get());
const partial = await form.withUpgradeEntityVersion();
return Forms.createVersion(partial, form, true, true);
const publishedVersion = await Forms.getByProjectAndXmlFormId(projectId, xmlFormId, true, Form.PublishedVersion).then(o => o.orNull());
if (publishedVersion && publishedVersion.currentDefId != null) {
const partial = await publishedVersion.withUpgradeEntityVersion();
return Forms.createVersion(partial, publishedVersion, true, true);
}

const draftVersion = await Forms.getByProjectAndXmlFormId(projectId, xmlFormId, true, Form.DraftVersion).then(o => o.orNull());
if (draftVersion && draftVersion.draftDefId != null) {
const partial = await draftVersion.withUpgradeEntityVersion();
// only update xml. maybe it should update the version as well? but this is all happening in place.
await Forms._updateDef(draftVersion.def, { xml: partial.xml });
}
};

module.exports = { create, updateDraftSet, updatePublish, updateEntitiesVersion };
Expand Down
109 changes: 82 additions & 27 deletions test/integration/other/form-entities-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ const testData = require('../../data/xml');
const { exhaust } = require(appRoot + '/lib/worker/worker');

describe('Update / migrate entities-version within form', () => {
it('should upgrade a 2023.1.0 update form', testService(async (service, container) => {
const { Forms, Audits } = container;
const asAlice = await service.login('alice');

// Publish a form that will set up the dataset with properties
await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.updateEntity)
.set('Content-Type', 'application/xml')
.expect(200);

const { acteeId } = await Forms.getByProjectAndXmlFormId(1, 'updateEntity').then(o => o.get());
await Audits.log(null, 'upgrade.process.form.entities_version', { acteeId });

// Run form upgrade
await exhaust(container);

await asAlice.get('/v1/projects/1/forms/updateEntity/versions')
.then(({ body }) => {
body.length.should.equal(2);
body[0].version.should.equal('1.0_upgrade');
});

await asAlice.get('/v1/projects/1/forms/updateEntity.xml')
.then(({ text }) => {
text.should.equal(`<?xml version="1.0"?>
describe('upgrading a 2023.1.0 update form', () => {
it('should upgrade a form with only a published version', testService(async (service, container) => {
const { Forms, Audits } = container;
const asAlice = await service.login('alice');

// Publish a form
await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.updateEntity)
.set('Content-Type', 'application/xml')
.expect(200);

const { acteeId } = await Forms.getByProjectAndXmlFormId(1, 'updateEntity').then(o => o.get());
await Audits.log(null, 'upgrade.process.form.entities_version', { acteeId });

// Run form upgrade
await exhaust(container);

await asAlice.get('/v1/projects/1/forms/updateEntity/versions')
.then(({ body }) => {
body.length.should.equal(2);
body[0].version.should.equal('1.0_upgrade');
body[1].version.should.equal('1.0');
});

await asAlice.get('/v1/projects/1/forms/updateEntity.xml')
.then(({ text }) => {
text.should.equal(`<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms">
<h:head>
<model entities:entities-version="2024.1.0">
Expand All @@ -50,6 +52,59 @@ describe('Update / migrate entities-version within form', () => {
</model>
</h:head>
</h:html>`);
});
}));
});
}));

it('should upgrade a form with only a draft version', testService(async (service, container) => {
const { Forms, Audits } = container;
const asAlice = await service.login('alice');

// Upload a form but dont publish, leaving it as a draft
await asAlice.post('/v1/projects/1/forms')
.send(testData.forms.updateEntity)
.set('Content-Type', 'application/xml')
.expect(200);


const { acteeId } = await Forms.getByProjectAndXmlFormId(1, 'updateEntity').then(o => o.get());
await Audits.log(null, 'upgrade.process.form.entities_version', { acteeId });

// Run form upgrade
await exhaust(container);

// The version on the draft hasn't changed because it has been updated in place
await asAlice.get('/v1/projects/1/forms/updateEntity/draft')
.then(({ body }) => {
body.version.should.equal('1.0');
});

// The XML is updated
await asAlice.get('/v1/projects/1/forms/updateEntity/draft.xml')
.then(({ text }) => {
text.should.equal(`<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms">
<h:head>
<model entities:entities-version="2024.1.0">
<instance>
<data id="updateEntity" orx:version="1.0_upgrade">
<name/>
<age/>
<hometown/>
<meta>
<entity dataset="people" id="" update="" baseVersion="" trunkVersion="" branchId="">
<label/>
</entity>
</meta>
</data>
</instance>
<bind nodeset="/data/name" type="string" entities:saveto="first_name"/>
<bind nodeset="/data/age" type="int" entities:saveto="age"/>
</model>
</h:head>
</h:html>`);
});
}));

// should test that xlsx attachment gets carried along
});
});

0 comments on commit e0a1670

Please sign in to comment.