Skip to content

Commit

Permalink
feat: process child subfields
Browse files Browse the repository at this point in the history
  • Loading branch information
wanlingt committed Nov 9, 2023
1 parent 0474fac commit 44bfb6f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import { StatusCodes } from 'http-status-codes'
import moment from 'moment-timezone'

import {
EncryptFormFieldResponse,
ParsedClearFormFieldResponse,
} from 'src/types/api'

import { MYINFO_ATTRIBUTE_MAP } from '../../../../../shared/constants/field/myinfo'
import {
BasicField,
FormAuthType,
FormPaymentsField,
MyInfoAttribute,
PaymentFieldsDto,
PaymentType,
StorageModeSubmissionContentDto,
Expand All @@ -12,6 +20,7 @@ import {
SubmissionType,
} from '../../../../../shared/types'
import { calculatePrice } from '../../../../../shared/utils/paymentProductPrice'
import { isProcessedChildResponse } from '../../../../app/utils/field-validation/field-validation.guards'
import {
IEncryptedSubmissionSchema,
IPopulatedEncryptedForm,
Expand Down Expand Up @@ -50,6 +59,7 @@ import {
PrivateFormError,
} from '../../form/form.errors'
import { MyInfoKey } from '../../myinfo/myinfo.types'
import { getMyInfoChildHashKey } from '../../myinfo/myinfo.util'
import { PaymentNotFoundError } from '../../payments/payments.errors'
import {
SgidInvalidJwtError,
Expand All @@ -74,7 +84,11 @@ import {
SubmissionNotFoundError,
ValidateFieldError,
} from '../submission.errors'
import { ProcessedFieldResponse } from '../submission.types'
import {
ProcessedChildrenResponse,
ProcessedFieldResponse,
ProcessedSingleAnswerResponse,
} from '../submission.types'

import {
AttachmentSizeLimitExceededError,
Expand Down Expand Up @@ -380,6 +394,55 @@ const getMyInfoPrefix = (
: ''
}

export const getAnswersForChild = (
response: ProcessedChildrenResponse,
): ProcessedSingleAnswerResponse[] =>
// {
// _id: string
// question: string
// answer: string
// fieldType: any
// isVisible?: boolean
// myInfo?: {
// attr: MyInfoAttribute
// }
// }[]
{
const subFields = response.childSubFieldsArray
const qnChildIdx = response.childIdx ?? 0
if (!subFields) {
return []
}
return response.answerArray.flatMap((arr, childIdx) => {
// First array element is always child name
const childName = arr[0]
return arr.map((answer, idx) => {
const subfield = subFields[idx]
return {
_id: getMyInfoChildHashKey(
response._id,
subFields[idx],
childIdx,
childName,
),
// qnChildIdx represents the index of the MyInfo field
// childIdx represents the index of the child in this MyInfo field
// as there might be >1 child for each MyInfo child field if "Add another child" is used
question: `Child ${qnChildIdx + childIdx + 1} ${
MYINFO_ATTRIBUTE_MAP[subfield].description
}`,
answer,
fieldType: BasicField.ShortText,
// fieldType: MYINFO_ATTRIBUTE_MAP[subfield].fieldType,
isVisible: response.isVisible,
myInfo: {
attr: subFields[idx] as unknown as MyInfoAttribute,
},
}
})
})
}

export class SubmissionStorageObj {
parsedResponses: ProcessedFieldResponse[]
hashedFields: Set<MyInfoKey>
Expand All @@ -399,11 +462,26 @@ export class SubmissionStorageObj {
* Getter function to return formData which is used to send responses to admin
*/
get formData() {
return this.parsedResponses.flatMap((response) => {
// Obtain prefix for question based on whether it is verified by MyInfo.
const myInfoPrefix = getMyInfoPrefix(response, this.hashedFields)
response.question = `${myInfoPrefix}${response.question}`
return response
})
console.log('hashes: ', this.hashedFields)
const responses:
| ProcessedFieldResponse[]
| ParsedClearFormFieldResponse[]
| EncryptFormFieldResponse[] = this.parsedResponses.flatMap(
(response) => {
if (isProcessedChildResponse(response)) {
return getAnswersForChild(response).map((childField) => {
const myInfoPrefix = getMyInfoPrefix(childField, this.hashedFields)
childField.question = `${myInfoPrefix}${childField.question}`
return childField
})
} else {
// Obtain prefix for question based on whether it is verified by MyInfo.
const myInfoPrefix = getMyInfoPrefix(response, this.hashedFields)
response.question = `${myInfoPrefix}${response.question}`
return response
}
},
)
return responses
}
}
12 changes: 12 additions & 0 deletions src/app/modules/submission/submission.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
BasicField,
CheckboxResponse,
ChildBirthRecordsResponse,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
MyInfoAttribute,
MyInfoChildAttributes,
TableResponse,
} from '../../../../shared/types'
Expand Down Expand Up @@ -81,3 +83,13 @@ export type ProcessedFieldResponse =
| ProcessedTableResponse
| ProcessedAttachmentResponse
| ProcessedChildrenResponse
// | ({
// _id: string
// question: string
// answer: string
// fieldType: any
// isVisible?: boolean
// myInfo?: {
// attr: MyInfoAttribute
// }
// } & ProcessedResponse)

0 comments on commit 44bfb6f

Please sign in to comment.