Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request Enketo IDs during request when form is created or published #989

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/model/query/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ const fromXls = (stream, contentType, formIdFallback, ignoreWarnings) => ({ Blob
.then(([ partial, xlsBlobId ]) => partial.withAux('xls', { xlsBlobId, itemsets }));
});


////////////////////////////////////////////////////////////////////////////////
// PUSHING TO ENKETO

const timeboundEnketo = (request, bound) =>
(bound != null ? timebound(request, bound).catch(() => ({})) : request);

// Accepts either a Form or an object with a top-level draftToken property. Also
// accepts an optional bound on the amount of time for the request to Enketo to
// complete (in seconds). If a bound is specified, and the request to Enketo
// times out or results in an error, then a nullish value is returned.
const pushDraftToEnketo = ({ projectId, xmlFormId, def, draftToken = def?.draftToken }, bound = undefined) => async ({ enketo, env }) => {
const encodedFormId = encodeURIComponent(xmlFormId);
const path = `${env.domain}/v1/test/${draftToken}/projects/${projectId}/forms/${encodedFormId}/draft`;
return (await timeboundEnketo(enketo.create(path, xmlFormId), bound)).enketoId;
};


////////////////////////////////////////////////////////////////////////////////
// CREATING NEW FORMS

Expand Down Expand Up @@ -120,7 +138,7 @@ createNew.audit.withResult = true;
// Inserts a new form def into the database for createVersion() below, setting
// fields on the def according to whether the def will be the current def or the
// draft def.
const _createNewDef = (partial, form, publish, data) => async ({ one, enketo, env }) => {
const _createNewDef = (partial, form, publish, data) => async ({ one, Forms }) => {
const insertWith = (moreData) => one(insert(partial.def.with({
formId: form.id,
xlsBlobId: partial.xls.xlsBlobId,
Expand All @@ -135,14 +153,12 @@ const _createNewDef = (partial, form, publish, data) => async ({ one, enketo, en
// generate a draft token and enketoId.
if (form.def.id == null || form.def.id !== form.draftDefId) {
const draftToken = generateToken();

// Try to push the draft to Enketo. If doing so fails or is too slow, then
// the worker will try again later.
const encodedId = encodeURIComponent(form.xmlFormId);
const path = `/v1/test/${draftToken}/projects/${form.projectId}/forms/${encodedId}/draft`;
const request = enketo.create(`${env.domain}${path}`, form.xmlFormId);
const enketoId = await timebound(request, 0.5).catch(() => null);

const enketoId = await Forms.pushDraftToEnketo(
matthew-white marked this conversation as resolved.
Show resolved Hide resolved
{ projectId: form.projectId, xmlFormId: form.xmlFormId, draftToken },
0.5
);
return insertWith({ draftToken, enketoId });
}

Expand Down Expand Up @@ -712,7 +728,7 @@ const _newSchema = () => ({ one }) =>
.then((s) => s.id);

module.exports = {
fromXls, _createNew, createNew, _createNewDef, createVersion,
fromXls, pushDraftToEnketo, _createNew, createNew, _createNewDef, createVersion,
publish, clearDraft,
_update, update, _updateDef, del, restore, purge,
clearUnneededDrafts,
Expand Down
5 changes: 2 additions & 3 deletions lib/worker/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const { Actor, Form } = require('../model/frames');

const pushDraftToEnketo = ({ Forms, enketo, env }, event) =>
const pushDraftToEnketo = ({ Forms }, event) =>
Forms.getByActeeIdForUpdate(event.acteeId, undefined, Form.DraftVersion)
.then((maybeForm) => maybeForm.map((form) => {
// if there was no draft or this form isn't the draft anymore just bail.
Expand All @@ -23,8 +23,7 @@ const pushDraftToEnketo = ({ Forms, enketo, env }, event) =>
// and wrong. still want to log a fail but bail early.
if (form.def.draftToken == null) throw new Error('Could not find a draft token!');

const path = `${env.domain}/v1/test/${form.def.draftToken}/projects/${form.projectId}/forms/${encodeURIComponent(form.xmlFormId)}/draft`;
return enketo.create(path, form.xmlFormId)
return Forms.pushDraftToEnketo(form)
.then((enketoId) => Forms._updateDef(form.def, new Form.Def({ enketoId })));
}).orNull());

Expand Down