Skip to content

Commit

Permalink
fix: prefill myinfo dropdown field options when faking sample submiss…
Browse files Browse the repository at this point in the history
…ion data (#7225)

* fix: prefill myinfo dropdown field options when faking sample submission data

* fix: add appropriate sendStatus
  • Loading branch information
justynoh authored Apr 2, 2024
1 parent e7f425e commit 9741b70
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/app/modules/form/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
PossibleDatabaseError,
} from '../core/core.errors'
import { IntranetService } from '../intranet/intranet.service'
import { getMyInfoFieldOptions } from '../myinfo/myinfo.util'

import {
FormDeletedError,
Expand Down Expand Up @@ -358,6 +359,11 @@ export const retrievePublicFormsWithSmsVerification = (
}

export const createSingleSampleSubmissionAnswer = (field: FormFieldDto) => {
// Prefill dropdown MyInfo field options for faking
if (field.fieldType === BasicField.Dropdown && field.myInfo) {
field.fieldOptions = getMyInfoFieldOptions(field.myInfo.attr)
}

let sampleValue = null
let noOfTableRows
let noOfTableCols
Expand All @@ -371,7 +377,10 @@ export const createSingleSampleSubmissionAnswer = (field: FormFieldDto) => {
break
case BasicField.Radio:
case BasicField.Dropdown:
sampleValue = faker.helpers.arrayElement(field.fieldOptions)
sampleValue =
field.fieldOptions.length === 0
? ''
: faker.helpers.arrayElement(field.fieldOptions)
break
case BasicField.Email:
sampleValue = faker.internet.email()
Expand Down Expand Up @@ -412,7 +421,10 @@ export const createSingleSampleSubmissionAnswer = (field: FormFieldDto) => {
sampleValue = tableSampleValue
break
case BasicField.Checkbox:
sampleValue = faker.helpers.arrayElements(field.fieldOptions)
sampleValue =
field.fieldOptions.length === 0
? ''
: faker.helpers.arrayElements(field.fieldOptions)
break
case BasicField.Date:
sampleValue = faker.date.anytime().toLocaleDateString('en-SG', {
Expand Down
20 changes: 18 additions & 2 deletions src/app/modules/form/public-form/public-form.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HttpStatusCode } from 'axios'
import { celebrate, Joi, Segments } from 'celebrate'
import { StatusCodes } from 'http-status-codes'
import { err, ok, Result } from 'neverthrow'
Expand Down Expand Up @@ -400,16 +401,31 @@ export const handleGetPublicFormSampleSubmission: ControllerHandler<

return res.status(statusCode).json({ message: errorMessage })
}

const form = formResult.value

const publicForm = form.getPublicView() as PublicFormDto

const formFields = publicForm.form_fields
if (!formFields) {
throw new Error('unable to get form fields')
logger.error({
message: 'Form fields not found from form public view',
meta: logMeta,
})
return res.sendStatus(HttpStatusCode.InternalServerError)
}

const sampleData = FormService.createSampleSubmissionResponses(formFields)
let sampleData
try {
sampleData = FormService.createSampleSubmissionResponses(formFields)
} catch (error) {
logger.error({
message: 'Error faking sample submission data',
meta: logMeta,
error,
})
return res.sendStatus(HttpStatusCode.InternalServerError)
}

return res.json({ responses: sampleData })
}
Expand Down

0 comments on commit 9741b70

Please sign in to comment.