diff --git a/src/app/modules/submission/__tests__/submission.utils.spec.ts b/src/app/modules/submission/__tests__/submission.utils.spec.ts index a594bfa240..0cdcc90c99 100644 --- a/src/app/modules/submission/__tests__/submission.utils.spec.ts +++ b/src/app/modules/submission/__tests__/submission.utils.spec.ts @@ -2,10 +2,17 @@ import { ObjectId } from 'bson' import { readFileSync } from 'fs' import { cloneDeep, merge } from 'lodash' -import { BasicField, FormResponseMode } from '../../../../../shared/types' +import { + BasicField, + FormResponseMode, + FormWorkflowStepDto, + WorkflowStatus, + WorkflowType, +} from '../../../../../shared/types' import { SingleAnswerFieldResponse } from '../../../../types' import { areAttachmentsMoreThanLimit, + buildMrfMetadata, getInvalidFileExtensions, getResponseModeFilter, mapAttachmentsFromResponses, @@ -66,6 +73,155 @@ const getResponse = (_id: string, answer: string): SingleAnswerFieldResponse => }) as unknown as SingleAnswerFieldResponse describe('submission.utils', () => { + describe('buildMrfMetadata', () => { + const YES_NO_FIELD = { + _id: 'yes_no_field_id', + title: 'Yes or No', + description: '', + required: true, + disabled: false, + fieldType: BasicField.YesNo, + } + const WORKFLOW_STEP_1: FormWorkflowStepDto = { + _id: 'step_1_id', + workflow_type: WorkflowType.Static, + emails: ['example@example.com'], + edit: [], + } + + const WORKFLOW_STEP_2: FormWorkflowStepDto = { + _id: 'step_2_id', + workflow_type: WorkflowType.Static, + emails: ['example@example.com'], + edit: [YES_NO_FIELD._id], + } + const WORKFLOW_APPROVAL_STEP: FormWorkflowStepDto = { + _id: 'approval_step_id', + workflow_type: WorkflowType.Static, + emails: ['example@example.com'], + edit: [YES_NO_FIELD._id], + approval_field: YES_NO_FIELD._id, + } + + it('should build mrf metadata successfully for pending submission without approval step', () => { + const metadata = buildMrfMetadata({ + workflow: [WORKFLOW_STEP_1, WORKFLOW_STEP_2], + workflowStep: 0, + submittedSteps: [ + { + isApproval: false, + submittedAt: '2024-01-01T00:00:00.000Z', + }, + ], + }) + + expect(metadata).toEqual({ + workflowCurrentStepNumber: 1, + workflowNumTotalSteps: 2, + workflowStatus: WorkflowStatus.PENDING, + }) + }) + + it('should build mrf metadata successfully for completed submission without approval step', () => { + const metadata = buildMrfMetadata({ + workflow: [WORKFLOW_STEP_1, WORKFLOW_STEP_2], + workflowStep: 1, + submittedSteps: [ + { + isApproval: false, + submittedAt: '2024-01-01T00:00:00.000Z', + }, + { + isApproval: false, + submittedAt: '2024-01-02T00:00:00.000Z', + }, + ], + }) + + expect(metadata).toEqual({ + workflowCurrentStepNumber: 2, + workflowNumTotalSteps: 2, + workflowStatus: WorkflowStatus.COMPLETED, + }) + }) + + it('should build mrf metadata successfully for pending submission with approval step', () => { + const metadata = buildMrfMetadata({ + workflow: [WORKFLOW_STEP_1, WORKFLOW_APPROVAL_STEP, WORKFLOW_STEP_2], + workflowStep: 1, + submittedSteps: [ + { + isApproval: false, + submittedAt: '2024-01-01T00:00:00.000Z', + }, + { + isApproval: true, + status: WorkflowStatus.APPROVED, + submittedAt: '2024-01-02T00:00:00.000Z', + }, + ], + }) + + expect(metadata).toEqual({ + workflowCurrentStepNumber: 2, + workflowNumTotalSteps: 3, + workflowStatus: WorkflowStatus.PENDING, + }) + }) + + it('should build mrf metadata successfully for approval submission with approval step', () => { + const metadata = buildMrfMetadata({ + workflow: [WORKFLOW_STEP_1, WORKFLOW_APPROVAL_STEP, WORKFLOW_STEP_2], + workflowStep: 2, + submittedSteps: [ + { + isApproval: false, + submittedAt: '2024-01-01T00:00:00.000Z', + }, + { + isApproval: true, + status: WorkflowStatus.APPROVED, + submittedAt: '2024-01-02T00:00:00.000Z', + }, + { + isApproval: false, + submittedAt: '2024-01-03T00:00:00.000Z', + }, + ], + }) + + expect(metadata).toEqual({ + workflowCurrentStepNumber: 3, + workflowNumTotalSteps: 3, + workflowStatus: WorkflowStatus.APPROVED, + }) + }) + + it('should build mrf metadata successfully for rejected submission with approval step', () => { + const metadata = buildMrfMetadata({ + workflow: [WORKFLOW_STEP_1, WORKFLOW_APPROVAL_STEP, WORKFLOW_STEP_2], + workflowStep: 1, + submittedSteps: [ + { + isApproval: false, + submittedAt: '2024-01-01T00:00:00.000Z', + }, + { + isApproval: true, + status: WorkflowStatus.REJECTED, + submittedAt: '2024-01-02T00:00:00.000Z', + }, + ], + }) + + expect(metadata).toEqual({ + workflowCurrentStepNumber: 2, + workflowNumTotalSteps: 3, + workflowStatus: WorkflowStatus.REJECTED, + }) + }) + }) + describe('getResponseModeFilter', () => { const ALL_FIELD_TYPES = Object.values(BasicField).map((fieldType) => ({ fieldType, diff --git a/src/app/modules/submission/multirespondent-submission/__tests__/multirespondent-submission.utils.spec.ts b/src/app/modules/submission/multirespondent-submission/__tests__/multirespondent-submission.utils.spec.ts index e46ccb49b5..01fc7c6c76 100644 --- a/src/app/modules/submission/multirespondent-submission/__tests__/multirespondent-submission.utils.spec.ts +++ b/src/app/modules/submission/multirespondent-submission/__tests__/multirespondent-submission.utils.spec.ts @@ -10,6 +10,7 @@ import { ChildBirthRecordsResponseV3, EmailResponseV3, FieldResponsesV3, + FormFieldDto, FormWorkflowStepDto, LongTextResponseV3, NumberResponseV3, @@ -42,7 +43,7 @@ import { } from '../multirespondent-submission.utils' describe('multirespondent-submission.utils', () => { - const WORKFLOW_STEP_1 = { + const WORKFLOW_STEP_1: FormWorkflowStepDto = { _id: 'step_1_id', workflow_type: WorkflowType.Static, emails: ['example@example.com'], @@ -140,7 +141,7 @@ describe('multirespondent-submission.utils', () => { const result = validateMrfFieldResponses({ formId: mockFormId, visibleFieldIds: mockVisibleFieldIds, - formFields: mockFormFields, + formFields: mockFormFields as FormFieldDto[], responses: mockResponses, }) @@ -174,7 +175,7 @@ describe('multirespondent-submission.utils', () => { validateMrfFieldResponses({ formId: mockFormId, visibleFieldIds: mockVisibleFieldIds, - formFields: mockFormFields, + formFields: mockFormFields as FormFieldDto[], responses: mockResponses, }) @@ -217,7 +218,7 @@ describe('multirespondent-submission.utils', () => { validateMrfFieldResponses({ formId: mockFormId, visibleFieldIds: mockVisibleFieldIds, - formFields: mockFormFields, + formFields: mockFormFields as FormFieldDto[], responses: mockResponses, })