@@ -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
0 commit comments