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

feat: add sample-submission endpoint to retrieve sample submission data of a public form #6325

Merged
merged 9 commits into from
May 18, 2023
Prev Previous commit
Next Next commit
ref: split sample submission creation into smaller functions
  • Loading branch information
wanlingt committed May 16, 2023
commit 3f2dcc19ac6a66253f05f172c83cb3dd2d70a458
24 changes: 18 additions & 6 deletions src/app/modules/form/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { err, errAsync, ok, okAsync, Result, ResultAsync } from 'neverthrow'

import {
FormAuthType,
FormField,
FormFieldDto,
FormResponseMode,
FormStatus,
Expand Down Expand Up @@ -345,10 +346,7 @@ export const retrievePublicFormsWithSmsVerification = (
})
}

export const createSampleSubmissionData = (
sampleData: Record<string, unknown>,
field: FormFieldDto,
) => {
export const createSingleSampleSubmissionAnswer = (field: FormFieldDto) => {
let sampleValue = null
let noOfOptions = 0
let randomSelectedOption = 0
Expand Down Expand Up @@ -391,12 +389,26 @@ export const createSampleSubmissionData = (
default:
break
}
let answer = {}
if (sampleValue != null) {
sampleData[field._id] = {
answer = {
id: field._id,
question: field.title,
wanlingt marked this conversation as resolved.
Show resolved Hide resolved
answer: sampleValue,
fieldType: field.fieldType,
}
}
return sampleValue
return answer
}

export const createSampleSubmissionResponses = (
formFields: FormFieldDto<FormField>[],
) => {
const sampleData: Record<string, any> = {}
formFields.forEach((field) => {
const answer = createSingleSampleSubmissionAnswer(field)
if (!answer) return
sampleData[field._id] = answer
})
return sampleData
}
7 changes: 3 additions & 4 deletions src/app/modules/form/public-form/public-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,13 @@ export const handleGetPublicFormSampleSubmission: ControllerHandler<

const publicForm = form.getPublicView() as PublicFormDto

const sampleData: Record<string, any> = {}
const formFields = publicForm.form_fields
if (!formFields) {
throw new Error('unable to get form fields')
}
for (const field of formFields) {
FormService.createSampleSubmissionData(sampleData, field)
}

const sampleData = FormService.createSampleSubmissionResponses(formFields)

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