You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

survey.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /** @module survey/survey */
  2. import { _baseRecord } from '../index.js'
  3. import { surveySchema } from './survey.schema.js'
  4. import { answerValidator } from './survey.answer.validator.js'
  5. import { aspectsArr } from '../../utils/lang.js'
  6. const SCORED = aspectsArr
  7. const _isScored = id => SCORED.includes(id)
  8. const _makeCategoryFriendly = responseCategory => {
  9. const labels = responseCategory.split('_vs_')
  10. labels.forEach((a, i) => {
  11. if (a.indexOf('_') == -1) return
  12. labels[i] = a.split('_').join(' ')
  13. })
  14. return labels
  15. }
  16. const _formatAspectQuestions = steps => {
  17. return steps
  18. .map(q => {
  19. if (!_isScored(q.response_key_id)) return null
  20. return {
  21. id: q.response_key_id,
  22. question: q.response_key_prompt,
  23. labels: _makeCategoryFriendly(q.response_key_category),
  24. answer: null,
  25. }
  26. })
  27. .filter(step => step != null)
  28. }
  29. class Survey extends _baseRecord {
  30. constructor(questionSteps) {
  31. super()
  32. this.type = this.constructor.name.toLowerCase()
  33. /** Fields */
  34. this.steps = [...questionSteps] // ! required
  35. this.aspectQuestions = _formatAspectQuestions(this.steps)
  36. console.log(
  37. 'this.aspectQuestions: ',
  38. JSON.stringify(this.aspectQuestions),
  39. )
  40. }
  41. hasMinResponsesToCreateProfile(responses) {
  42. if (responses.name &&
  43. responses.email &&
  44. responses.seeking) {
  45. return true
  46. } else return false
  47. }
  48. validateAnswer(payload) {
  49. const { question, input } = payload
  50. // Continue our ugly hacks
  51. const validationType =
  52. question.category == 'aspect'
  53. ? question.category
  54. : question.survey_stage
  55. const validate = answerValidator[validationType].validate(input)
  56. if (validate.error) {
  57. console.error(`error: ${validate.error}`)
  58. }
  59. return !validate.error ? true : false
  60. }
  61. isValid() {
  62. const validate = surveySchema.validate(this)
  63. /**
  64. * Log out some useful error messages
  65. */
  66. if (validate.error) {
  67. console.error(`error: ${validate.error} - ${this.type} validation`)
  68. }
  69. /** validate(this) always returns something so force it to a bool */
  70. return !validate.error ? true : false
  71. }
  72. }
  73. export { Survey }