Skip to content

Commit a5ab6a4

Browse files
committed
chore: edit validate approval config query
1 parent f571401 commit a5ab6a4

File tree

5 files changed

+25
-37
lines changed

5 files changed

+25
-37
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/graphql/mutations/create-step.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IStepConfig } from '@plumber/types'
2+
13
import { raw } from 'objection'
24

35
import { BadUserInputError } from '@/errors/graphql-errors'
@@ -39,15 +41,6 @@ const createStep: MutationResolvers['createStep'] = async (
3941
throw new BadUserInputError('Action can only be created by system')
4042
}
4143
}
42-
if (input.connection?.id) {
43-
// if connectionId is specified, verify that the connection exists and belongs to the user
44-
const connection = await context.currentUser
45-
.$relatedQuery('connections')
46-
.findOne({ id: input.connection.id })
47-
if (!connection) {
48-
throw new BadUserInputError('Connection not found')
49-
}
50-
}
5144

5245
return await Step.transaction(async (trx) => {
5346
await trx.raw('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;')
@@ -89,7 +82,7 @@ const createStep: MutationResolvers['createStep'] = async (
8982
.throwIfNotFound()
9083

9184
const validationResult = await validateApprovalConfig(
92-
input.config,
85+
input.config as IStepConfig,
9386
previousStep,
9487
)
9588
if (!validationResult.isApprovalConfigValid) {
@@ -110,7 +103,7 @@ const createStep: MutationResolvers['createStep'] = async (
110103
position: validationResult.newStepPosition,
111104
parameters: input.parameters,
112105
connectionId: input.connection?.id,
113-
config: input.config,
106+
config: input.config as IStepConfig,
114107
})
115108

116109
// NOTE: add flow connection to the flow_connections table

packages/backend/src/graphql/schema.graphql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,10 @@ input CreateStepInput {
590590
config: StepConfigInput
591591
}
592592

593-
enum StepApprovalBranch {
594-
approve
595-
reject
596-
}
593+
597594

598595
input StepApprovalConfigInput {
599-
branch: StepApprovalBranch!
596+
branch: String!
600597
stepId: String!
601598
}
602599

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
@@ -169,7 +169,7 @@ export interface IExecution {
169169
export type IStepApprovalBranch = 'approve' | 'reject'
170170

171171
export interface IStepApprovalConfig {
172-
branch: IStepApprovalBranch
172+
branch: 'reject'
173173
stepId: string
174174
}
175175

0 commit comments

Comments
 (0)