Skip to content

Commit 7323336

Browse files
committed
feat: purge all mrf steps and config on checkstep or delete trigger
1 parent c00626a commit 7323336

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

packages/backend/src/apps/formsg/triggers/new-submission/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,16 @@ const trigger: IRawTrigger = {
117117
await createMrfSteps($, mrfWorkflowData)
118118
} else {
119119
// remove mrf object from parameters and remove mrf steps (if any)
120-
await removeMrfSteps($)
120+
try {
121+
await removeMrfSteps($.flow.id)
122+
} catch (error) {
123+
throw new StepError(
124+
'Error removing MRF steps',
125+
'This should not happen, please contact support.',
126+
$.step.position,
127+
$.app.name,
128+
)
129+
}
121130
}
122131

123132
// if test with mock data is selected OR no past submission exists
Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
import { IGlobalVariable } from '@plumber/types'
1+
import { Transaction } from 'objection'
22

3-
import StepError from '@/errors/step'
43
import Step from '@/models/step'
54

6-
export async function removeMrfSteps($: IGlobalVariable) {
7-
if (!$.flow?.id || !$.step?.id) {
8-
throw new StepError(
9-
'Missing flow or step',
10-
'This should not happen, please contact support.',
11-
$.step.position,
12-
$.app.name,
13-
)
14-
}
15-
16-
await Step.transaction(async (trx) => {
5+
export async function removeMrfSteps(flowId: string, trx?: Transaction) {
6+
const executeQueries = async (trx: Transaction) => {
177
// delete all mrf action steps
188
await Step.query(trx)
19-
.where('flow_id', $.flow.id)
9+
.where('flow_id', flowId)
2010
.where('type', 'action')
2111
.where('key', 'mrfSubmission')
2212
.delete()
2313

2414
// reset trigger step parameters
2515
await Step.query(trx)
26-
.where('flow_id', $.flow.id)
16+
.where('flow_id', flowId)
2717
.where('type', 'trigger')
2818
.where('key', 'newSubmission')
2919
.patch({
3020
parameters: {},
3121
})
32-
})
22+
23+
// Delete all steps in the reject branch
24+
await Step.query(trx)
25+
.where('flow_id', flowId)
26+
.where('type', 'action')
27+
.andWhereRaw(`steps.config->'approval'->>'branch' = ?`, ['reject'])
28+
.delete()
29+
.debug()
30+
31+
await Step.query(trx)
32+
.where('flow_id', flowId)
33+
.where('type', 'action')
34+
.patch({
35+
// remove approval config from all action steps
36+
config: Step.knex().raw(`(steps.config::jsonb - 'approval')::jsonb`),
37+
})
38+
39+
await Step.resetStepOrdering(flowId, trx)
40+
}
41+
42+
if (trx) {
43+
await executeQueries(trx)
44+
} else {
45+
await Step.transaction(executeQueries)
46+
}
3347
}

packages/backend/src/graphql/mutations/delete-step.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { raw } from 'objection'
22

3+
import { removeMrfSteps } from '@/apps/formsg/triggers/new-submission/remove-mrf-steps'
34
import { hasStepReference } from '@/helpers/check-step-parameters'
45
import Step from '@/models/step'
56

@@ -47,6 +48,10 @@ const deleteStep: MutationResolvers['deleteStep'] = async (
4748
if (steps.length === 1 && steps[0].type === 'trigger') {
4849
const deletedStepId = steps[0].id
4950

51+
if (steps[0].appKey === 'formsg' && steps[0].key === 'newSubmission') {
52+
await removeMrfSteps(flow.id, trx)
53+
}
54+
5055
// check for steps whose parameters reference the deletedStepId
5156
const allSteps = await flow
5257
.$relatedQuery('steps', trx)

packages/backend/src/models/step.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ class Step extends Base {
199199
})
200200
}
201201

202+
// Reset step ordering in case of mass deletion or reordering
203+
static async resetStepOrdering(flowId: string, trx?: Transaction) {
204+
const allSteps = await Step.query(trx)
205+
.where('flow_id', flowId)
206+
.where('type', 'action')
207+
.orderBy('position', 'asc')
208+
209+
for (let i = 0; i < allSteps.length; i++) {
210+
const currStep = allSteps[i]
211+
if (currStep.position !== i + 2) {
212+
await Step.query(trx)
213+
.findById(currStep.id)
214+
.patch({
215+
// actions start from 2 (after the trigger step)
216+
position: i + 2,
217+
})
218+
}
219+
}
220+
}
221+
202222
static async beforeUpdate(args: StaticHookArguments<Step>): Promise<void> {
203223
await super.beforeUpdate(args)
204224

0 commit comments

Comments
 (0)