Skip to content

Commit

Permalink
submission successful but still 1 string
Browse files Browse the repository at this point in the history
  • Loading branch information
scottheng96 committed Dec 19, 2024
1 parent ca2d0ce commit ce31aad
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 21 deletions.
3 changes: 1 addition & 2 deletions frontend/src/features/public-form/PublicFormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ export const submitEmailModeForm = async ({
formInputs: filteredInputs,
responseMetadata,
})
console.log('here i am')
console.log(formData.get('body'))
return ApiService.post<SubmissionResponseDto>(
`${PUBLIC_FORMS_ENDPOINT}/${formId}/submissions/email`,
formData,
Expand Down Expand Up @@ -286,6 +284,7 @@ export const submitStorageModeForm = async ({
fieldIdToQuarantineKeyMap,
)
console.log('here i am')
console.log(formFields)
console.log(formData.get('body'))
return ApiService.post<SubmissionResponseDto>(
`${PUBLIC_FORMS_ENDPOINT}/${formId}/submissions/storage`,
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/features/public-form/utils/inputTransformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '~shared/types/field'
import {
AddressResponse,
AddressSubField,
AttachmentResponse,
CheckboxResponse,
ChildBirthRecordsResponse,
Expand Down Expand Up @@ -230,13 +231,15 @@ const transformToAddressOutput = (
schema: AddressCompoundFieldSchema,
input?: AddressCompoundFieldValues | AddressCompoundFieldResponseV3,
): AddressResponse => {
console.log(input?.addressSubFields)
// const answerArray: AddressSubField[] = []
const answerArray: string[][] = []
if (input !== undefined) {
Object.entries(input.addressSubFields).map(([key, value]) =>
answerArray.push([`${key}:${value}`]),
)
Object.entries(input.addressSubFields).map(([key, value]) => {
// answerArray.push({ key: key, value: value })
answerArray.push([`${key}:${value}`])
})
}

return {
...pickBaseOutputFromSchema(schema),
answerArray,
Expand Down
5 changes: 5 additions & 0 deletions shared/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ export const UenResponse = SingleAnswerResponse.extend({
export type UenResponse = z.infer<typeof UenResponse>

// TODO: check fo multivalue response as a string[]
export type AddressSubField = {
key: string
value: string
}

export const AddressResponse = ResponseBase.extend({
fieldType: z.literal(BasicField.Address),
answerArray: z.array(z.array(z.string())) as unknown as z.Schema<string[][]>,
Expand Down
23 changes: 14 additions & 9 deletions src/app/modules/submission/submission.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,20 +795,25 @@ export const getAnswersForAddress = (
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
const subFieldResponses: ProcessedSingleAnswerResponse[] = []
// Object.entries(obj).forEach(([key, value]) => {
// console.log(`${key}: ${value}`); // Output each key-value pair
// });

Object.entries(subFields).forEach((subField) => {
const key = subField[0].split(':')[0].trim()
const value = subField[0].split(':')[1].trim()
subFieldResponses.push({
_id: response._id + key, // check this
fieldType: response.fieldType,
question: response.question + subFieldName,
question: response.question + key,
myInfo: response.myInfo,
isVisible: response.isVisible,
isUserVerified: response.isUserVerified,
answer: subFieldValue,
}
answer: value,
})
})
return subFieldResponses
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/app/utils/field-validation/field-validation.guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ export const isProcessedAddressResponse = (
return (
response.fieldType === BasicField.Address &&
'answerArray' in response &&
Array.isArray(response.answerArray) &&
response.answerArray.every(
(item) => typeof item === 'object' && item !== null, // Assuming AddressAttribute is an object
)
Array.isArray(response.answerArray)
// &&
// response.answerArray.every(
// (item) => typeof item === 'object' && item !== null,
// )
)
}

Expand Down
19 changes: 19 additions & 0 deletions src/app/utils/field-validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { err, ok, Result } from 'neverthrow'
import { FIELDS_TO_REJECT } from '../../../../shared/constants/field/basic'
import { BasicField, FormField, FormFieldDto } from '../../../../shared/types'
import {
ProcessedAddressResponse,
ProcessedAttachmentResponse,
ProcessedCheckboxResponse,
ProcessedChildrenResponse,
Expand All @@ -25,6 +26,7 @@ import {
} from '../../modules/submission/submission.errors'

import {
constructAddressFieldValidator,
constructAttachmentFieldValidator,
constructCheckboxFieldValidator,
constructChildFieldValidator,
Expand All @@ -34,6 +36,7 @@ import {
} from './answerValidator.factory'
import {
isGenericStringAnswerResponseV3,
isProcessedAddressResponse,
isProcessedAttachmentResponse,
isProcessedCheckboxResponse,
isProcessedChildResponse,
Expand Down Expand Up @@ -146,6 +149,12 @@ const tableRequiresValidation = (
return requiredVisible || answerPresent
}

const addressRequiresValidation = (
formField: FieldValidationSchema,
response: ProcessedAddressResponse,
) =>
(formField.required && response.isVisible) || response.answerArray.length > 0

/**
* Generic logging function for invalid fields.
* Incomplete for table fields as the columnType is not logged.
Expand Down Expand Up @@ -275,6 +284,16 @@ export const validateField = (
response,
)
}
} else if (isProcessedAddressResponse(response)) {
if (addressRequiresValidation(formField, response)) {
const validator = constructAddressFieldValidator(formField)
return validateResponseWithValidator(
validator,
formId,
formField,
response,
)
}
} else {
logInvalidAnswer(formId, formField, 'Invalid response shape')
return err(new ValidateFieldError('Response has invalid shape'))
Expand Down
7 changes: 5 additions & 2 deletions src/app/utils/field-validation/validators/addressValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type AddressValidatorConstructor = (
* Returns a validator to check if postal code format is correct
*/
const addressValidator: AddressValidator = (response) => {
const { answerArray } = response
return validatePostalCode(answerArray[0][0]) // TODO
// const { answerArray } = response
// const entry = answerArray.find(({ key }) => key === 'postalCode')
// const postalCode = entry ? entry.value : ''
// return validatePostalCode(postalCode) // TODO
return true

Check failure on line 36 in src/app/utils/field-validation/validators/addressValidator.ts

View workflow job for this annotation

GitHub Actions / backend_lint

Unexpected constant condition
? right(response)
: left(`AddressValidator:\t answer is not a valid postal code`)
}
Expand Down

0 comments on commit ce31aad

Please sign in to comment.