Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
KenLSM marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading