diff --git a/lib/worker/form.js b/lib/worker/form.js
index 6afc23d27..a995f6305 100644
--- a/lib/worker/form.js
+++ b/lib/worker/form.js
@@ -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 };
diff --git a/test/integration/other/form-entities-version.js b/test/integration/other/form-entities-version.js
index 292d1a9b6..babb146c3 100644
--- a/test/integration/other/form-entities-version.js
+++ b/test/integration/other/form-entities-version.js
@@ -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(`
+ 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(`
@@ -50,6 +52,59 @@ describe('Update / migrate entities-version within form', () => {
`);
- });
- }));
+ });
+ }));
+
+ 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(`
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+`);
+ });
+ }));
+
+ // should test that xlsx attachment gets carried along
+ });
});