From dd32a67efc2fd71855e84acaa3a040d28b4b07a9 Mon Sep 17 00:00:00 2001 From: scottheng96 Date: Wed, 11 Dec 2024 11:17:51 +0800 Subject: [PATCH] checkpoint for BE validation --- .../public-form/PublicFormProvider.tsx | 1 - .../features/public-form/PublicFormService.ts | 1 - .../FormFields/PublicFormSubmitButton.tsx | 2 ++ .../public-form/utils/createSubmission.ts | 29 ++++++++++++------- .../public-form/utils/inputTransformation.ts | 3 +- shared/types/response.ts | 12 ++++---- .../validators/addressValidator.ts | 6 ++-- 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/frontend/src/features/public-form/PublicFormProvider.tsx b/frontend/src/features/public-form/PublicFormProvider.tsx index 79bd1f6b77..6bfccf80fc 100644 --- a/frontend/src/features/public-form/PublicFormProvider.tsx +++ b/frontend/src/features/public-form/PublicFormProvider.tsx @@ -135,7 +135,6 @@ const transformFormInputCountryRegionToUpperCase = .filter((field) => field.fieldType === BasicField.CountryRegion) .map((field) => field._id), ) - return Object.keys(formInputs).reduce( (newFormInputs: typeof formInputs, fieldId) => { const currentInput = formInputs[fieldId] diff --git a/frontend/src/features/public-form/PublicFormService.ts b/frontend/src/features/public-form/PublicFormService.ts index 50aeaa73fa..0225c72e20 100644 --- a/frontend/src/features/public-form/PublicFormService.ts +++ b/frontend/src/features/public-form/PublicFormService.ts @@ -272,7 +272,6 @@ export const submitStorageModeForm = async ({ formInputs, formLogics, }) - const formData = createClearSubmissionWithVirusScanningFormData( { formFields, diff --git a/frontend/src/features/public-form/components/FormFields/PublicFormSubmitButton.tsx b/frontend/src/features/public-form/components/FormFields/PublicFormSubmitButton.tsx index aa58bd8ce8..1c3b53820f 100644 --- a/frontend/src/features/public-form/components/FormFields/PublicFormSubmitButton.tsx +++ b/frontend/src/features/public-form/components/FormFields/PublicFormSubmitButton.tsx @@ -45,6 +45,8 @@ export const PublicFormSubmitButton = ({ const isMobile = useIsMobile() const { isSubmitting } = useFormState() const formInputs = useWatch({}) as FormFieldValues + console.log(formFields) + console.log(formInputs) const { formId, isPaymentEnabled, diff --git a/frontend/src/features/public-form/utils/createSubmission.ts b/frontend/src/features/public-form/utils/createSubmission.ts index 110caa9ac7..1e0f721821 100644 --- a/frontend/src/features/public-form/utils/createSubmission.ts +++ b/frontend/src/features/public-form/utils/createSubmission.ts @@ -270,7 +270,6 @@ const createResponsesV3 = ( case BasicField.Uen: case BasicField.Date: case BasicField.CountryRegion: - case BasicField.Address: // for now treating as a postalCode single response case BasicField.YesNo: { const input = formInputs[ff._id] as | FormFieldValue @@ -282,16 +281,24 @@ const createResponsesV3 = ( } as FieldResponseV3 break } - // case BasicField.Address: - // const input = formInputs[ff._id] as - // | FormFieldValue - // | undefined - // if (!input) break - // returnedInputs[ff._id] = { - // fieldType: ff.fieldType, - // answer: input, - // } as FieldResponseV3 - // break + case BasicField.Address: { + const input = formInputs[ff._id] as + | FormFieldValue + | undefined + if (!input) break + returnedInputs[ff._id] = { + fieldType: ff.fieldType, + answer: { + postalCode: input.postalCode, + blockNumber: input.blockNumber, + streetName: input.streetName, + buildingName: input.buildingName, + levelNumber: input.levelNumber, + unitNumber: input.unitNumber, + }, + } as FieldResponseV3 + break + } case BasicField.Email: case BasicField.Mobile: { const input = formInputs[ff._id] as diff --git a/frontend/src/features/public-form/utils/inputTransformation.ts b/frontend/src/features/public-form/utils/inputTransformation.ts index 010fd066fb..83ea4315cb 100644 --- a/frontend/src/features/public-form/utils/inputTransformation.ts +++ b/frontend/src/features/public-form/utils/inputTransformation.ts @@ -3,6 +3,7 @@ import { times } from 'lodash' import { DATE_PARSE_FORMAT } from '~shared/constants/dates' import { + AddressFieldResponseV3, AttachmentFieldResponseV3, CheckboxFieldResponsesV3, ChildrenCompoundFieldResponsesV3, @@ -222,7 +223,7 @@ const transformToChildOutput = ( const transformToAddressOutput = ( schema: AddressFieldSchema, - input?: AddressFieldValues, // | AddressFieldResponseV3 + input?: AddressFieldValues | AddressFieldResponseV3, ): AddressResponse => { let answer = '' if (input?.postalCode !== undefined) answer = input.postalCode // TODO: do for all input fields, just using postalCode to test now diff --git a/shared/types/response.ts b/shared/types/response.ts index 29d50f17b8..fce1b74483 100644 --- a/shared/types/response.ts +++ b/shared/types/response.ts @@ -132,6 +132,12 @@ export const UenResponse = SingleAnswerResponse.extend({ }) export type UenResponse = z.infer +// TODO: check fo multivalue response +export const AddressResponse = SingleAnswerResponse.extend({ + fieldType: z.literal(BasicField.Address), +}) +export type AddressResponse = z.infer + export const ChildBirthRecordsResponse = ResponseBase.merge( MyInfoResponseBase, ).extend({ @@ -154,12 +160,6 @@ export type SingleChildSubRecordResponse = z.infer< typeof SingleChildSubRecordResponse > -// TODO: check fo multivalue response -export const AddressResponse = SingleAnswerResponse.extend({ - fieldType: z.literal(BasicField.Address), -}) -export type AddressResponse = z.infer - export type FieldResponse = | HeaderResponse | EmailResponse diff --git a/src/app/utils/field-validation/validators/addressValidator.ts b/src/app/utils/field-validation/validators/addressValidator.ts index 053d729d00..6b4b77b8d0 100644 --- a/src/app/utils/field-validation/validators/addressValidator.ts +++ b/src/app/utils/field-validation/validators/addressValidator.ts @@ -1,11 +1,11 @@ import { chain, left, right } from 'fp-ts/lib/Either' import { flow } from 'fp-ts/lib/function' -import { AddressResponseV3, BasicField } from 'shared/types' -import { validatePostalCode } from 'shared/utils/address-validation' import { ProcessedSingleAnswerResponse } from 'src/app/modules/submission/submission.types' import { IAddressFieldSchema, OmitUnusedValidatorProps } from 'src/types' +import { AddressResponseV3, BasicField } from '../../../../../shared/types' +import { validatePostalCode } from '../../../../../shared/utils/address-validation' import { ParsedClearFormFieldResponseV3 } from '../../../../types/api' import { ResponseValidator, @@ -14,7 +14,7 @@ import { import { notEmptySingleAnswerResponse, - notEmptySingleAnswerResponseV3, + // notEmptySingleAnswerResponseV3, } from './common' type AddressValidator = ResponseValidator