Skip to content

Commit

Permalink
worker wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Oct 5, 2024
1 parent cef069c commit 7f530c2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/model/frames/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// API. This is partly for legacy reasons: Forms and FormsDef did not used to

const { Frame, table, readable, writable, into, embedded, fieldTypes } = require('../frame');
const { injectPublicKey, addVersionSuffix } = require('../../data/schema');
const { injectPublicKey, addVersionSuffix, updateEntityForm } = require('../../data/schema');
const { Key } = require('./key');
const { md5sum, shasum, sha256sum, digestWith, generateVersionSuffix } = require('../../util/crypto');
const { resolve } = require('../../util/promise');
Expand Down Expand Up @@ -76,6 +76,10 @@ class Form extends Frame.define(
.then((partial) => partial.with({ key: Option.of(key) }));
}

withUpgradeEntityVersion() {
return updateEntityForm(this.xml, '2023.1.0', '2024.1.0', '_upgrade').then(Form.fromXml);
}

_enketoIdForApi() {
if (this.def == null) return null;
if (this.def.id === this.draftDefId) return this.def.enketoId;
Expand Down
9 changes: 8 additions & 1 deletion lib/worker/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@ const create = pushDraftToEnketo;
const updateDraftSet = pushDraftToEnketo;
const updatePublish = pushFormToEnketo;

module.exports = { create, updateDraftSet, updatePublish };
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);
};

module.exports = { create, updateDraftSet, updatePublish, updateEntitiesVersion };

2 changes: 2 additions & 0 deletions lib/worker/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const jobs = {
'upgrade.process.form.draft': [ require('./form').updateDraftSet ],
'upgrade.process.form': [ require('./form').updatePublish ],

'upgrade.process.form.entities_version': [ require('./form').updateEntitiesVersion ],

'dataset.update': [ require('./dataset').createEntitiesFromPendingSubmissions ]
};

Expand Down
55 changes: 55 additions & 0 deletions test/integration/other/form-entities-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const appRoot = require('app-root-path');
const { testService } = require('../setup');
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"?>
<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>`);
});
}));
});

0 comments on commit 7f530c2

Please sign in to comment.