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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Survey } from '../entities'
  2. import { fetchQuestions } from '../services'
  3. class SurveyFactory {
  4. constructor(responses) {
  5. this.responsesByCategory = responses
  6. this.questionsFromDb = []
  7. }
  8. _setSteps(langFile) {
  9. const stepsToProcess = [...Object.values(langFile) ]
  10. const seenIds = []
  11. const stepsInCommon = stepsToProcess.map(step => {
  12. // Match question to step
  13. const match = this.questionsFromDb.filter(q => q.response_key_prompt == step)[0]
  14. if(match) { seenIds.push(match.response_key_id) }
  15. return {
  16. response_key_id: match ? match.response_key_id: null,
  17. response_key_category: match ? match.response_key_category: 'profile',
  18. response_key_prompt: match ? match.response_key_prompt: step,
  19. response_key_description: match ? match.response_key_description: null,
  20. responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : []
  21. }
  22. })
  23. const unseen = this.questionsFromDb.filter(q => !seenIds.includes(q.response_key_id))
  24. return [...stepsInCommon, ...unseen]
  25. }
  26. async getQuestions() {
  27. try {
  28. this.questionsFromDb = await fetchQuestions()
  29. return this.questionsFromDb
  30. } catch(err) {
  31. console.error(err)
  32. }
  33. }
  34. async createSurvey(langFile, roleTree) {
  35. if(!this.questionsFromDb.length) {
  36. const res = await this.getQuestions()
  37. console.warn(`Attempted to create a survey before getting questions: retrieved ${res.length} questions`)
  38. }
  39. const steps = this._setSteps(langFile)
  40. return new Survey(steps, roleTree)
  41. }
  42. }
  43. export { SurveyFactory }