From ca2d0cef0a0f698dd06476e2355e923877ef00b6 Mon Sep 17 00:00:00 2001 From: scottheng96 Date: Wed, 18 Dec 2024 08:41:01 +0800 Subject: [PATCH] added splitting of address fields into single responses --- .../features/public-form/PublicFormService.ts | 6 ++-- .../public-form/utils/createSubmission.ts | 2 -- .../public-form/utils/inputTransformation.ts | 2 +- shared/types/response.ts | 8 ++++- .../email-submission/email-submission.util.ts | 32 +++++-------------- .../modules/submission/submission.types.ts | 1 - .../modules/submission/submission.utils.ts | 30 +++++++++++++++++ 7 files changed, 50 insertions(+), 31 deletions(-) diff --git a/frontend/src/features/public-form/PublicFormService.ts b/frontend/src/features/public-form/PublicFormService.ts index 0225c72e20..b963fa075f 100644 --- a/frontend/src/features/public-form/PublicFormService.ts +++ b/frontend/src/features/public-form/PublicFormService.ts @@ -186,7 +186,8 @@ export const submitEmailModeForm = async ({ formInputs: filteredInputs, responseMetadata, }) - + console.log('here i am') + console.log(formData.get('body')) return ApiService.post( `${PUBLIC_FORMS_ENDPOINT}/${formId}/submissions/email`, formData, @@ -284,7 +285,8 @@ export const submitStorageModeForm = async ({ }, fieldIdToQuarantineKeyMap, ) - + console.log('here i am') + console.log(formData.get('body')) return ApiService.post( `${PUBLIC_FORMS_ENDPOINT}/${formId}/submissions/storage`, formData, diff --git a/frontend/src/features/public-form/utils/createSubmission.ts b/frontend/src/features/public-form/utils/createSubmission.ts index d290d99279..9ac6c893d5 100644 --- a/frontend/src/features/public-form/utils/createSubmission.ts +++ b/frontend/src/features/public-form/utils/createSubmission.ts @@ -175,8 +175,6 @@ export const createClearSubmissionWithVirusScanningFormData = ( ...formDataArgsRest, }), ) - console.log('here i am') - console.log(formData.get('body')) if (!isEmpty(attachments)) { forOwn(attachments, (attachment, fieldId) => { if (attachment) { diff --git a/frontend/src/features/public-form/utils/inputTransformation.ts b/frontend/src/features/public-form/utils/inputTransformation.ts index b4f7633b9e..6e81da1f4d 100644 --- a/frontend/src/features/public-form/utils/inputTransformation.ts +++ b/frontend/src/features/public-form/utils/inputTransformation.ts @@ -233,7 +233,7 @@ const transformToAddressOutput = ( const answerArray: string[][] = [] if (input !== undefined) { Object.entries(input.addressSubFields).map(([key, value]) => - answerArray.push([`${key}: ${value}`]), + answerArray.push([`${key}:${value}`]), ) } diff --git a/shared/types/response.ts b/shared/types/response.ts index aec923f614..55126a77ff 100644 --- a/shared/types/response.ts +++ b/shared/types/response.ts @@ -1,6 +1,6 @@ import type { Opaque } from 'type-fest' import { z } from 'zod' -import { AddressAttributes, BasicField, MyInfoAttribute } from './field' +import { BasicField, MyInfoAttribute } from './field' const ResponseBase = z.object({ myInfo: z.never().optional(), @@ -139,6 +139,11 @@ export const AddressResponse = ResponseBase.extend({ }) export type AddressResponse = z.infer +export const AddressSubFieldResponse = SingleAnswerResponse.extend({ + fieldType: z.literal(BasicField.Address), +}) +export type AddressSubFieldResponse = z.infer + export const ChildBirthRecordsResponse = ResponseBase.merge( MyInfoResponseBase, ).extend({ @@ -184,3 +189,4 @@ export type FieldResponse = | ChildBirthRecordsResponse | SingleChildSubRecordResponse | AddressResponse + | AddressSubFieldResponse diff --git a/src/app/modules/submission/email-submission/email-submission.util.ts b/src/app/modules/submission/email-submission/email-submission.util.ts index 59223f72f2..cdbf0fd89f 100644 --- a/src/app/modules/submission/email-submission/email-submission.util.ts +++ b/src/app/modules/submission/email-submission/email-submission.util.ts @@ -74,12 +74,15 @@ import { ValidateFieldError, } from '../submission.errors' import { - ProcessedAddressResponse, ProcessedCheckboxResponse, ProcessedFieldResponse, ProcessedTableResponse, } from '../submission.types' -import { getAnswersForChild, getMyInfoPrefix } from '../submission.utils' +import { + getAnswersForAddress, + getAnswersForChild, + getMyInfoPrefix, +} from '../submission.utils' import { ATTACHMENT_PREFIX, @@ -193,26 +196,6 @@ export const getAnswerForCheckbox = ( } } -/** - * Creates a response for address, with its answer formatted from the answerArray - * @param response - * @param response.answerArray is of type AddressAttributes - * @returns the response with formatted answer - */ -export const getAnswerForAddress = ( - response: ProcessedAddressResponse, -): ResponseFormattedForEmail => { - return { - _id: response._id, - fieldType: response.fieldType, - question: response.question, - myInfo: response.myInfo, - isVisible: response.isVisible, - isUserVerified: response.isUserVerified, - answer: JSON.stringify(response.answerArray), - } -} - /** * Formats the response for sending to the submitter (autoReplyData), * the table that is sent to the admin (formData), @@ -465,8 +448,9 @@ const createFormattedDataForOneField = ( getFormattedFunction(childField, hashedFields), ) } else if (isProcessedAddressResponse(response)) { - const address = getAnswerForAddress(response) - return [getFormattedFunction(address, hashedFields)] + return getAnswersForAddress(response).map((subField) => + getFormattedFunction(subField, hashedFields), + ) } else { return [getFormattedFunction(response, hashedFields)] } diff --git a/src/app/modules/submission/submission.types.ts b/src/app/modules/submission/submission.types.ts index 7cbb47e4f6..79e75bf4c0 100644 --- a/src/app/modules/submission/submission.types.ts +++ b/src/app/modules/submission/submission.types.ts @@ -2,7 +2,6 @@ import { StatusCodes } from 'http-status-codes' import type { Opaque } from 'type-fest' import { - AddressAttributes, AddressResponse, BasicField, CheckboxResponse, diff --git a/src/app/modules/submission/submission.utils.ts b/src/app/modules/submission/submission.utils.ts index 495fc5f91d..50f993adc6 100644 --- a/src/app/modules/submission/submission.utils.ts +++ b/src/app/modules/submission/submission.utils.ts @@ -140,6 +140,7 @@ import { } from './submission.errors' import { FilteredResponse, + ProcessedAddressResponse, ProcessedChildrenResponse, ProcessedFieldResponse, ProcessedSingleAnswerResponse, @@ -781,6 +782,35 @@ export const getAnswersForChild = ( }) } +/** + * Creates a response for address, with its answer formatted from the answerArray + * @param response + * @param response.answerArray is of type AddressAttributes + * @returns the response with formatted answer + */ +export const getAnswersForAddress = ( + response: ProcessedAddressResponse, +): ProcessedSingleAnswerResponse[] => { + const subFields = response.answerArray + if (!subFields) { + return [] + } + // subField = ["postalCode:650161"] + return subFields.flatMap((subField) => { + const subFieldName = subField[0].split(':')[0].trim() + const subFieldValue = subField[0].split(':')[1].trim() + return { + _id: response._id + subFieldName, // check this + fieldType: response.fieldType, + question: response.question + subFieldName, + myInfo: response.myInfo, + isVisible: response.isVisible, + isUserVerified: response.isUserVerified, + answer: subFieldValue, + } + }) +} + /** * Generates a hash to mask the original submitterId. * @param id