Skip to content

Commit

Permalink
createAnswersForAddres to split multi-response into single-responses
Browse files Browse the repository at this point in the history
  • Loading branch information
scottheng96 committed Dec 26, 2024
1 parent ce31aad commit b12632e
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class EncryptedResponseCsvGenerator extends CsvGenerator {
addRecord({ record, created, submissionId }: DecryptedSubmissionData): void {
// First pass, create object with { [fieldId]: question } from
// decryptedContent to get all the questions.
console.log(record)
const fieldRecords = record.map((content) => {
const fieldRecord = getDecryptedResponseInstance(content)
if (!fieldRecord.isHeader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { CsvRecord } from '../utils/CsvRecord.class'

// Fixes issue raised at https://stackoverflow.com/questions/66472945/referenceerror-refreshreg-is-not-defined
// Something to do with babel-loader.
if (import.meta.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
;(global as any).$RefreshReg$ = () => {}
// eslint-disable-next-line
;(global as any).$RefreshSig$ = () => () => {}
}
// if (import.meta.env.NODE_ENV !== 'production') {
// // eslint-disable-next-line
// ;(global as any).$RefreshReg$ = () => {}
// // eslint-disable-next-line
// ;(global as any).$RefreshSig$ = () => () => {}
// }

const queue = new PQueue({ concurrency: 1 })

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/features/public-form/PublicFormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ export const submitStorageModeForm = async ({
},
fieldIdToQuarantineKeyMap,
)
console.log('here i am')
console.log(formFields)
console.log(formData.get('body'))
// console.log('here i am')
// console.log(formFields)
// console.log(formData.get('body'))
return ApiService.post<SubmissionResponseDto>(
`${PUBLIC_FORMS_ENDPOINT}/${formId}/submissions/storage`,
formData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const createClearSubmissionWithVirusScanningFormData = (
}
})
}

// console.log(formData.get('body'))
return formData
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ const transformToAddressOutput = (
schema: AddressCompoundFieldSchema,
input?: AddressCompoundFieldValues | AddressCompoundFieldResponseV3,
): AddressResponse => {
console.log(input?.addressSubFields)
// console.log(input?.addressSubFields)
// const answerArray: AddressSubField[] = []
const answerArray: string[][] = []
if (input !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ import {
StorageSubmissionMiddlewareHandlerType,
ValidateSubmissionMiddlewareHandlerRequest,
} from './encrypt-submission.types'
import { formatMyInfoStorageResponseData } from './encrypt-submission.utils'
import {
formatAddressStorageResponseData,
formatMyInfoStorageResponseData,
} from './encrypt-submission.utils'
import IncomingEncryptSubmission from './IncomingEncryptSubmission.class'

const logger = createLoggerWithLabel(module)
Expand Down Expand Up @@ -422,6 +425,14 @@ export const validateStorageSubmission = async (
hashedFields,
)
req.body.responses = storageFormData
return { parsedResponses }
})
.map(({ parsedResponses }) => {
// manage address fields
const storageFormDataAddress = formatAddressStorageResponseData(
parsedResponses.getAllResponses(),
)
req.body.responses = storageFormDataAddress
return next()
})
.mapErr((error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SubmissionType,
} from '../../../../../shared/types'
import { calculatePrice } from '../../../../../shared/utils/paymentProductPrice'
import { isProcessedChildResponse } from '../../../../app/utils/field-validation/field-validation.guards'
import { isProcessedAddressResponse, isProcessedChildResponse } from '../../../../app/utils/field-validation/field-validation.guards'

Check failure on line 16 in src/app/modules/submission/encrypt-submission/encrypt-submission.utils.ts

View workflow job for this annotation

GitHub Actions / backend_lint

Replace `·isProcessedAddressResponse,·isProcessedChildResponse·` with `⏎··isProcessedAddressResponse,⏎··isProcessedChildResponse,⏎`
import {
IEncryptedSubmissionSchema,
IPopulatedEncryptedForm,
Expand All @@ -26,7 +26,7 @@ import {
} from '../../../../types/api'
import { MyInfoKey } from '../../myinfo/myinfo.types'
import { ProcessedFieldResponse } from '../submission.types'
import { getAnswersForChild, getMyInfoPrefix } from '../submission.utils'
import { getAnswersForAddress, getAnswersForChild, getMyInfoPrefix } from '../submission.utils'

Check failure on line 29 in src/app/modules/submission/encrypt-submission/encrypt-submission.utils.ts

View workflow job for this annotation

GitHub Actions / backend_lint

Replace `·getAnswersForAddress,·getAnswersForChild,·getMyInfoPrefix·` with `⏎··getAnswersForAddress,⏎··getAnswersForChild,⏎··getMyInfoPrefix,⏎`

/**
* Typeguard to check if given submission is an encrypt mode submission.
Expand Down Expand Up @@ -166,6 +166,22 @@ export const formatMyInfoStorageResponseData = (
}
}

export const formatAddressStorageResponseData = (
parsedResponses: ProcessedFieldResponse[],
) => {
return parsedResponses.flatMap((response) => {
if (isProcessedAddressResponse(response)) {
return getAnswersForAddress(response).map((subField) => {
const prefix = '1' // TODO
subField.question = `${prefix} ${subField.question}`
return subField
})
} else {
return omitResponseKeys(response)
}
})
}

export const getStripePaymentMethod = (
form: IPopulatedEncryptedForm,
): Omit<Stripe.PaymentIntentCreateParams, 'amount' | 'currency'> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ export const getQuestionTitleAnswerString = ({
continue
case BasicField.Address:
answer = response.answer.addressSubFields.postalCode // TODO
questionAnswerPair.push({
question: `Postal Code - ${questionTitle}`,
answer,
})
answer = response.answer.addressSubFields.blockNumber
questionAnswerPair.push({
question: `Block Number - ${questionTitle}`,
answer,
})
break
case BasicField.Email:
case BasicField.Mobile:
Expand Down
16 changes: 7 additions & 9 deletions src/app/modules/submission/submission.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,10 @@ export const getAnswersForChild = (
}

/**
* Creates a response for address, with its answer formatted from the answerArray
* Expands child subfields into individual fields, so that they are no longer nested under
* 1 parent field.
* @param response
* @param response.answerArray is of type AddressAttributes
* @param response.answerArray is of string[][]
* @returns the response with formatted answer
*/
export const getAnswersForAddress = (
Expand All @@ -796,17 +797,14 @@ export const getAnswersForAddress = (
return []
}
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()
const key = subField[1][0].split(':')[0]
const value = subField[1][0].split(':')[1]
subFieldResponses.push({
_id: response._id + key, // check this
_id: response._id,
fieldType: response.fieldType,
question: response.question + key,
question: response.question + 1 + key,
myInfo: response.myInfo,
isVisible: response.isVisible,
isUserVerified: response.isUserVerified,
Expand Down
4 changes: 2 additions & 2 deletions src/app/utils/field-validation/answerValidator.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ export const constructFieldResponseValidatorV3 = ({
return constructAttachmentFieldValidatorV3(formField)
case BasicField.Children:
return constructChildrenValidatorV3(formField)
case BasicField.Address:
return constructAddressValidatorV3(formField)
case BasicField.Image: // fall-through
case BasicField.Statement:
return () =>
left('Unsupported field type: field should not be part of response')
case BasicField.Address:
return constructAddressValidatorV3(formField)
default: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const exhaustiveCheck: never = formField
Expand Down

0 comments on commit b12632e

Please sign in to comment.