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 1.4KB

12345678910111213141516171819202122232425262728293031323334
  1. import { Survey } from '../entities'
  2. import { fetchQuestionsByProfileId } from '../services'
  3. class SurveyFactory {
  4. constructor(responses) {
  5. this.responsesByCategory = responses
  6. }
  7. async setSteps(langFile) {
  8. const stepsToProcess = [...Object.values(langFile) ]
  9. console.log(stepsToProcess)
  10. const questions = await fetchQuestionsByProfileId()
  11. const seenIds = []
  12. const stepsInCommon = stepsToProcess.map(step => {
  13. // Match question to step
  14. const match = questions.filter(q => q.response_key_prompt == step)[0]
  15. if(match) { seenIds.push(match.response_key_id) }
  16. return {
  17. response_key_category: match ? match.response_key_category: 'profile',
  18. response_key_description: match ? match.response_key_description: null,
  19. response_key_id: match ? match.response_key_id: null,
  20. response_key_prompt: match ? match.response_key_prompt: step,
  21. responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : []
  22. }
  23. })
  24. const unseen = questions.filter(q => !seenIds.includes(q.response_key_id))
  25. return [...stepsInCommon, ...unseen]
  26. }
  27. async createSurvey(langFile) {
  28. const steps = await this.setSteps(langFile)
  29. return new Survey(steps)
  30. }
  31. }
  32. export { SurveyFactory }