From 4e3f017b31792df1fea3b8b2dfe8a598ac97f569 Mon Sep 17 00:00:00 2001 From: Justyn Oh Date: Thu, 15 Feb 2024 14:53:15 +0800 Subject: [PATCH] fix: add db migration script --- .../mrf-retain-workflow-in-submissions.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 scripts/20240215_mrf_retain_workflow_in_submissions/mrf-retain-workflow-in-submissions.js diff --git a/scripts/20240215_mrf_retain_workflow_in_submissions/mrf-retain-workflow-in-submissions.js b/scripts/20240215_mrf_retain_workflow_in_submissions/mrf-retain-workflow-in-submissions.js new file mode 100644 index 0000000000..9e88e38515 --- /dev/null +++ b/scripts/20240215_mrf_retain_workflow_in_submissions/mrf-retain-workflow-in-submissions.js @@ -0,0 +1,49 @@ +/* eslint-disable */ + +// Creates a new workflow key in all existing mrf submissions, populated with +// the form's existing workflow. From this point on, workflows should be captured +// when the submission is initially created (i.e. the workflow is started), + +// BEFORE +// COUNT existing number of MRF submissions with no workflow key +db.submissions.countDocuments({ + submissionType: 'multirespondentSubmission', + workflow: { $exists: false }, +}) + +// UPDATE +db.submissions.aggregate([ + { + $match: { + submissionType: 'multirespondentSubmission', + }, + }, + { + $lookup: { + from: 'forms', + localField: 'form', + foreignField: '_id', + as: 'forms', + }, + }, + { + $addFields: { + workflow: { $first: '$forms.workflow' }, + }, + }, + { + $merge: { + into: 'submissions', + on: '_id', + whenNotMatched: 'discard', + }, + }, +]) + +// AFTER +// Count number of MRF submissions with workflow key +// Expect this to match the COUNT in BEFORE +db.submissions.countDocuments({ + submissionType: 'multirespondentSubmission', + workflow: { $exists: true }, +})