Skip to content

Commit

Permalink
Auto-link form that creates and consumes dataset on publish (#1056)
Browse files Browse the repository at this point in the history
* Auto-link dataset to form that creates and consumes the dataset on publish

* Changes in response to code review

* Final code review test change
  • Loading branch information
ktuite authored Dec 7, 2023
1 parent c258056 commit a0cb606
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/model/query/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ createVersion.audit.withResult = true;

// TODO: we need to make more explicit what .def actually represents throughout.
// for now, enforce an extra check here just in case.
const publish = (form) => async ({ Forms, Datasets }) => {
const publish = (form) => async ({ Forms, Datasets, FormAttachments }) => {
if (form.draftDefId !== form.def.id) throw Problem.internal.unknown();

// Try to push the form to Enketo if it hasn't been pushed already. If doing
Expand All @@ -329,15 +329,27 @@ const publish = (form) => async ({ Forms, Datasets }) => {
: {};

const publishedAt = (new Date()).toISOString();
return Promise.all([
await Promise.all([
Forms._update(form, { currentDefId: form.draftDefId, draftDefId: null, ...enketoIds }),
Forms._updateDef(form.def, { draftToken: null, enketoId: null, publishedAt }),
Datasets.publishIfExists(form.def.id, publishedAt)
])
.catch(Problem.translate(
Problem.user.uniquenessViolation,
() => Problem.user.versionUniquenessViolation({ xmlFormId: form.xmlFormId, version: form.def.version })
));

const ds = await Datasets.publishIfExists(form.def.id, publishedAt);
// this check is for issue c#554. we will hopefully come back to this flow and improve it later.
// ds contains a list of objects about what happened with the dataset
// like if the dataset was published and which properties were published.
// if it's empty, there is no dataset to work with.
if (ds.length > 0) {
const dataset = await Datasets.getById(ds[0].id).then(o => o.get());
const attachment = await FormAttachments.getByFormDefIdAndName(form.def.id, `${dataset.name}.csv`);
if (attachment.isDefined() && attachment.get().blobId == null && attachment.get().datasetId == null) {
await FormAttachments.update(form, attachment.get(), null, dataset.id);
}
}
};
publish.audit = (form) => (log) => log('form.update.publish', form,
{ oldDefId: form.currentDefId, newDefId: form.draftDefId });
Expand Down
Loading

0 comments on commit a0cb606

Please sign in to comment.