Skip to content

Commit

Permalink
added splitting of address fields into single responses
Browse files Browse the repository at this point in the history
  • Loading branch information
scottheng96 committed Dec 18, 2024
1 parent 9469d3d commit ca2d0ce
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
6 changes: 4 additions & 2 deletions frontend/src/features/public-form/PublicFormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ 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 @@ -284,7 +285,8 @@ export const submitStorageModeForm = async ({
},
fieldIdToQuarantineKeyMap,
)

console.log('here i am')
console.log(formData.get('body'))
return ApiService.post<SubmissionResponseDto>(
`${PUBLIC_FORMS_ENDPOINT}/${formId}/submissions/storage`,
formData,
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/features/public-form/utils/createSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`]),
)
}

Expand Down
8 changes: 7 additions & 1 deletion shared/types/response.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -139,6 +139,11 @@ export const AddressResponse = ResponseBase.extend({
})
export type AddressResponse = z.infer<typeof AddressResponse>

export const AddressSubFieldResponse = SingleAnswerResponse.extend({
fieldType: z.literal(BasicField.Address),
})
export type AddressSubFieldResponse = z.infer<typeof AddressSubFieldResponse>

export const ChildBirthRecordsResponse = ResponseBase.merge(
MyInfoResponseBase,
).extend({
Expand Down Expand Up @@ -184,3 +189,4 @@ export type FieldResponse =
| ChildBirthRecordsResponse
| SingleChildSubRecordResponse
| AddressResponse
| AddressSubFieldResponse
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -465,8 +448,9 @@ const createFormattedDataForOneField = <T extends EmailDataFields | undefined>(
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)]
}
Expand Down
1 change: 0 additions & 1 deletion src/app/modules/submission/submission.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { StatusCodes } from 'http-status-codes'
import type { Opaque } from 'type-fest'

import {
AddressAttributes,
AddressResponse,
BasicField,
CheckboxResponse,
Expand Down
30 changes: 30 additions & 0 deletions src/app/modules/submission/submission.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import {
} from './submission.errors'
import {
FilteredResponse,
ProcessedAddressResponse,
ProcessedChildrenResponse,
ProcessedFieldResponse,
ProcessedSingleAnswerResponse,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca2d0ce

Please sign in to comment.