-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sample-submission endpoint to retrieve sample submission da…
…ta of a public form (#6325) * feat: add createSampleSubmissionData function * feat: add endpoint to get sample submission * docs: add comments for GET sample submission endpoint * feat: randomise response, add attachment type * fix: remove form deactivation check * tests: add unit tests for sample submission endpoint * ref: split sample submission creation into smaller functions * fix: return static fields in createSingleSampleSubmissionAnswer
- Loading branch information
Showing
4 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import { err, errAsync, ok, okAsync, Result, ResultAsync } from 'neverthrow' | |
|
||
import { | ||
FormAuthType, | ||
FormField, | ||
FormFieldDto, | ||
FormResponseMode, | ||
FormStatus, | ||
} from '../../../../shared/types' | ||
|
@@ -343,3 +345,66 @@ export const retrievePublicFormsWithSmsVerification = ( | |
return okAsync(forms) | ||
}) | ||
} | ||
|
||
export const createSingleSampleSubmissionAnswer = (field: FormFieldDto) => { | ||
let sampleValue = null | ||
let noOfOptions = 0 | ||
let randomSelectedOption = 0 | ||
switch (field.fieldType) { | ||
case 'textarea': | ||
case 'textfield': | ||
sampleValue = | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' | ||
break | ||
case 'radiobutton': | ||
case 'dropdown': | ||
noOfOptions = field.fieldOptions.length | ||
randomSelectedOption = Math.floor(Math.random() * noOfOptions) | ||
sampleValue = field.fieldOptions[randomSelectedOption] | ||
break | ||
case 'email': | ||
sampleValue = '[email protected]' | ||
break | ||
case 'decimal': | ||
sampleValue = 1.234 | ||
break | ||
case 'number': | ||
sampleValue = 1234 | ||
break | ||
case 'mobile': | ||
sampleValue = '+6598765432' | ||
break | ||
case 'homeno': | ||
sampleValue = '+6567890123' | ||
break | ||
case 'yes_no': | ||
sampleValue = 'yes' | ||
break | ||
case 'rating': | ||
sampleValue = 1 | ||
break | ||
case 'attachment': | ||
sampleValue = 'attachmentFileName' | ||
break | ||
default: | ||
break | ||
} | ||
return { | ||
id: field._id, | ||
question: field.title, | ||
answer: sampleValue, | ||
fieldType: field.fieldType, | ||
} | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters