-
Notifications
You must be signed in to change notification settings - Fork 8
[MRF-11] PLU-520: (backend) create step approval config #1287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pregnantboy
wants to merge
3
commits into
mrf/refactor-prevstep-id
Choose a base branch
from
mrf/create-step-approval-config-backend
base: mrf/refactor-prevstep-id
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/backend/src/helpers/validate-approval-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { IStep, IStepConfig } from '@plumber/types' | ||
|
|
||
| import get from 'lodash.get' | ||
|
|
||
| import { stepApprovalConfigSchema } from '@/apps/formsg/common/types' | ||
| import Step from '@/models/step' | ||
|
|
||
| import logger from './logger' | ||
|
|
||
| /** | ||
| * TODO: write unit test for this function | ||
| */ | ||
| export async function validateApprovalConfig( | ||
| config: IStepConfig, | ||
| prevStep: IStep, | ||
| ): Promise< | ||
| | { | ||
| isApprovalConfigValid: true | ||
| newStepPosition: number | ||
| } | ||
| | { | ||
| isApprovalConfigValid: false | ||
| } | ||
| > { | ||
| /** | ||
| * Case 1: new step is a trigger step, return early | ||
| */ | ||
| if (!prevStep) { | ||
| return { | ||
| isApprovalConfigValid: true, | ||
| newStepPosition: 1, | ||
| } | ||
| } | ||
|
|
||
| const isPreviousStepMrfApprovalStep = | ||
| prevStep.appKey === 'formsg' && | ||
| prevStep.key === 'mrfSubmission' && | ||
| // if no approval field, it's just a normal mrf step | ||
| !!get(prevStep.parameters, 'mrf.approvalField') | ||
|
|
||
| /** | ||
| * Case 2: Prev step has no approval config and is not an mrf approval step, | ||
| * current step should have no approval config either | ||
| */ | ||
| if (!prevStep.config.approval && !isPreviousStepMrfApprovalStep) { | ||
| if (!config?.approval) { | ||
| return { | ||
| isApprovalConfigValid: true, | ||
| newStepPosition: prevStep.position + 1, | ||
| } | ||
| } | ||
| logger.error( | ||
| 'Invalid approval config: previous step has no approval config and is not an mrf approval step. This step should not have an approval config.', | ||
| ) | ||
| return { | ||
| isApprovalConfigValid: false, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Case 3: Prev step is part of an approval branch, check that | ||
| * the new step has the same approval config | ||
| */ | ||
| if (prevStep.config.approval) { | ||
| const isSameApprovalConfig = | ||
| prevStep.config.approval?.branch === config?.approval?.branch && | ||
| prevStep.config.approval?.stepId === config?.approval?.stepId | ||
| if (isSameApprovalConfig) { | ||
| return { | ||
| isApprovalConfigValid: true, | ||
| newStepPosition: prevStep.position + 1, | ||
| } | ||
| } | ||
| logger.error( | ||
| 'Invalid approval config: new step approval config is not the same as previous step', | ||
| ) | ||
| return { | ||
| isApprovalConfigValid: false, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Case 4: Previous step is an approval step | ||
| */ | ||
| if (isPreviousStepMrfApprovalStep) { | ||
| // If previous step is an approval step, but this step has no approval config, it is part of the approve flow | ||
| if (!config?.approval) { | ||
| return { | ||
| isApprovalConfigValid: true, | ||
| newStepPosition: prevStep.position + 1, | ||
| } | ||
| } | ||
| // validate approval config | ||
| const { success } = stepApprovalConfigSchema.safeParse(config?.approval) | ||
| // validate approval config step id is the same as prev step id | ||
| if (!success || config.approval.stepId !== prevStep.id) { | ||
| logger.error( | ||
| 'Invalid approval config: invalid approval config or new step id is not the same as approval step id', | ||
| { | ||
| approvalConfig: config?.approval, | ||
| }, | ||
| ) | ||
| return { | ||
| isApprovalConfigValid: false, | ||
| } | ||
| } | ||
| // For reject branch, find the end of the approval branch and add one to that step | ||
| // first find the next mrf step (if any) | ||
| const nextMrfStep = await Step.query() | ||
| .where('flow_id', prevStep.flowId) | ||
| .andWhere('type', 'action') | ||
| .andWhere('key', 'mrfSubmission') | ||
| .andWhere('position', '>', prevStep.position) | ||
| .orderBy('position', 'asc') | ||
| .first() | ||
| // then find the step in the approve branch (if any) | ||
| const lastStepOfApproveFlowQuery = Step.query() | ||
| .where('flow_id', prevStep.flowId) | ||
| .andWhere('position', '>', prevStep.position) | ||
|
|
||
| .orderBy('position', 'desc') | ||
| .first() | ||
|
|
||
| if (nextMrfStep) { | ||
| lastStepOfApproveFlowQuery.andWhere('position', '<', nextMrfStep.position) | ||
| } | ||
| const lastStepOfapproveFlow = await lastStepOfApproveFlowQuery.first() | ||
| // If last approval step exists, return the position after that | ||
| if (lastStepOfapproveFlow) { | ||
| return { | ||
| isApprovalConfigValid: true, | ||
| newStepPosition: lastStepOfapproveFlow.position + 1, | ||
| } | ||
| } else { | ||
| // If no last approval step, return the position after the previous step | ||
| return { | ||
| isApprovalConfigValid: true, | ||
| newStepPosition: prevStep.position + 1, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Catch all branch, something seems fishy | ||
| */ | ||
| return { | ||
| isApprovalConfigValid: false, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clarification: do we need to add the
templateConfig?don't think we add this in any of the createStep mutations