Skip to content

Commit

Permalink
feat(mrf): retain workflow through a submission lifetime (#7087)
Browse files Browse the repository at this point in the history
* feat: retain workflow through a submission lifetime

* test: fix submission model tests with workflow object

* fix: add db migration script

* chore: update package-lock for virus scanner
  • Loading branch information
justynoh authored Feb 21, 2024
1 parent 956620a commit 51ce234
Show file tree
Hide file tree
Showing 10 changed files with 1,219 additions and 14 deletions.
5 changes: 4 additions & 1 deletion frontend/src/features/public-form/PublicFormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,13 @@ export const PublicFormProvider = ({
/* enabled= */ !submissionData,
)

// Replace form fields and logic with the previous version for MRF consistency.
// Replace form fields, logic, and workflow with the previous version for MRF consistency.
if (data && encryptedPreviousSubmission) {
data.form.form_fields = encryptedPreviousSubmission.form_fields
data.form.form_logics = encryptedPreviousSubmission.form_logics
if (data.form.responseMode === FormResponseMode.Multirespondent) {
data.form.workflow = encryptedPreviousSubmission.workflow
}
}

const isLoading = isFormLoading || isSubmissionLoading
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* 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.aggregate([
{
$match: {
submissionType: 'multirespondentSubmission',
workflow: { $exists: false },
},
},
{
$lookup: {
from: 'forms',
localField: 'form',
foreignField: '_id',
as: 'forms',
},
},
{
$addFields: {
formWorkflow: { $first: '$forms.workflow' },
},
},
{
$match: {
formWorkflow: { $exists: true },
},
},
{
$count: 'count',
},
])

// 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.aggregate([
{
$match: {
submissionType: 'multirespondentSubmission',
workflow: { $exists: true },
},
},
{
$lookup: {
from: 'forms',
localField: 'form',
foreignField: '_id',
as: 'forms',
},
},
{
$addFields: {
formWorkflow: { $first: '$forms.workflow' },
},
},
{
$match: {
formWorkflow: { $exists: true },
},
},
{
$count: 'count',
},
])
Loading

0 comments on commit 51ce234

Please sign in to comment.