-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
scripts/20240215_retain-workflow-in-mrf-submissions/retain-workflow-in-mrf-submissions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }, | ||
}) |