| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /** @module survey/survey */
- import { _baseRecord } from '../index.js'
- import { surveySchema } from './survey.schema.js'
- import { answerValidator } from './survey.answer.validator.js'
- import { aspectsArr } from '../../utils/lang.js'
-
- const SCORED = aspectsArr
- const _isScored = id => SCORED.includes(id)
- const _makeCategoryFriendly = responseCategory => {
- const labels = responseCategory.split('_vs_')
- labels.forEach((a, i) => {
- if (a.indexOf('_') == -1) return
- labels[i] = a.split('_').join(' ')
- })
- return labels
- }
- const _formatAspectQuestions = steps => {
- return steps
- .map(q => {
- if (!_isScored(q.response_key_id)) return null
- return {
- id: q.response_key_id,
- question: q.response_key_prompt,
- labels: _makeCategoryFriendly(q.response_key_category),
- answer: null,
- }
- })
- .filter(step => step != null)
- }
-
- class Survey extends _baseRecord {
- constructor(questionSteps) {
- super()
-
- this.type = this.constructor.name.toLowerCase()
-
- /** Fields */
- this.steps = [...questionSteps] // ! required
- this.aspectQuestions = _formatAspectQuestions(this.steps)
- console.log(
- 'this.aspectQuestions: ',
- JSON.stringify(this.aspectQuestions),
- )
- }
-
- hasMinResponsesToCreateProfile(responses) {
- if (responses.name &&
- responses.email &&
- responses.seeking) {
- return true
- } else return false
- }
-
- validateAnswer(payload) {
- const { question, input } = payload
-
- // Continue our ugly hacks
- const validationType =
- question.category == 'aspect'
- ? question.category
- : question.survey_stage
- const validate = answerValidator[validationType].validate(input)
- if (validate.error) {
- console.error(`error: ${validate.error}`)
- }
- return !validate.error ? true : false
- }
-
- isValid() {
- const validate = surveySchema.validate(this)
-
- /**
- * Log out some useful error messages
- */
- if (validate.error) {
- console.error(`error: ${validate.error} - ${this.type} validation`)
- }
-
- /** validate(this) always returns something so force it to a bool */
- return !validate.error ? true : false
- }
- }
-
- export { Survey }
|