Skip to content

Commit 6da876b

Browse files
committed
chore: edit validate approval config query
1 parent 9a17c6a commit 6da876b

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

packages/backend/src/apps/formsg/common/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ export interface ParsedMrfWorkflow {
5252
}
5353

5454
export const stepApprovalConfigSchema = z.object({
55-
branch: z.enum(['approve', 'reject']),
55+
branch: z.literal('reject'),
5656
stepId: z.string().uuid(),
5757
})

packages/backend/src/helpers/validate-approval-config.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function validateApprovalConfig(
5050
}
5151
}
5252
logger.error(
53-
'Invalid approval config: previous step has no approval config and is not an mrf approval step',
53+
'Invalid approval config: previous step has no approval config and is not an mrf approval step. This step should not have an approval config.',
5454
)
5555
return {
5656
isApprovalConfigValid: false,
@@ -83,12 +83,19 @@ export async function validateApprovalConfig(
8383
* Case 4: Previous step is an approval step
8484
*/
8585
if (isPreviousStepMrfApprovalStep) {
86+
// If previous step is an approval step, but this step has no approval config, it is part of the approve flow
87+
if (!config?.approval) {
88+
return {
89+
isApprovalConfigValid: true,
90+
newStepPosition: prevStep.position + 1,
91+
}
92+
}
8693
// validate approval config
8794
const { success } = stepApprovalConfigSchema.safeParse(config?.approval)
8895
// validate approval config step id is the same as prev step id
8996
if (!success || config.approval.stepId !== prevStep.id) {
9097
logger.error(
91-
'Invalid approval config (after approval step): invalid approval config or new step id is not the same as approval step id',
98+
'Invalid approval config: invalid approval config or new step id is not the same as approval step id',
9299
{
93100
approvalConfig: config?.approval,
94101
},
@@ -97,16 +104,7 @@ export async function validateApprovalConfig(
97104
isApprovalConfigValid: false,
98105
}
99106
}
100-
/**
101-
* If approval branch, simply add 1 to the prev step position
102-
*/
103-
if (config.approval.branch === 'approve') {
104-
return {
105-
isApprovalConfigValid: true,
106-
newStepPosition: prevStep.position + 1,
107-
}
108-
}
109-
// If reject branch, find the end of the approval branch and add one to that step
107+
// For reject branch, find the end of the approval branch and add one to that step
110108
// first find the next mrf step (if any)
111109
const nextMrfStep = await Step.query()
112110
.where('flow_id', prevStep.flowId)
@@ -115,23 +113,23 @@ export async function validateApprovalConfig(
115113
.andWhere('position', '>', prevStep.position)
116114
.orderBy('position', 'asc')
117115
.first()
118-
// then find the last approval step in the curr branch (if any)
119-
const lastApprovalStepQuery = Step.query()
116+
// then find the step in the approve branch (if any)
117+
const lastStepOfApproveFlowQuery = Step.query()
120118
.where('flow_id', prevStep.flowId)
121119
.andWhere('position', '>', prevStep.position)
122-
.andWhereRaw(`config->'approval'->>'branch' = ?`, ['approve'])
120+
123121
.orderBy('position', 'desc')
124122
.first()
125123

126124
if (nextMrfStep) {
127-
lastApprovalStepQuery.andWhere('position', '<', nextMrfStep.position)
125+
lastStepOfApproveFlowQuery.andWhere('position', '<', nextMrfStep.position)
128126
}
129-
const lastApprovalStep = await lastApprovalStepQuery.first()
127+
const lastStepOfapproveFlow = await lastStepOfApproveFlowQuery.first()
130128
// If last approval step exists, return the position after that
131-
if (lastApprovalStep) {
129+
if (lastStepOfapproveFlow) {
132130
return {
133131
isApprovalConfigValid: true,
134-
newStepPosition: lastApprovalStep.position + 1,
132+
newStepPosition: lastStepOfapproveFlow.position + 1,
135133
}
136134
} else {
137135
// If no last approval step, return the position after the previous step

packages/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export interface IExecution {
168168
export type IStepApprovalBranch = 'approve' | 'reject'
169169

170170
export interface IStepApprovalConfig {
171-
branch: IStepApprovalBranch
171+
branch: 'reject'
172172
stepId: string
173173
}
174174

0 commit comments

Comments
 (0)