| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { Survey } from '../entities'
- import { fetchQuestions } from '../services'
-
- class SurveyFactory {
- constructor(responses) {
- this.responsesByCategory = responses
- this.questionsFromDb = []
- }
- _setSteps(langFile) {
- const stepsToProcess = [...Object.values(langFile) ]
- const seenIds = []
- const stepsInCommon = stepsToProcess.map(step => {
- // Match question to step
- const match = this.questionsFromDb.filter(q => q.response_key_prompt == step)[0]
- if(match) { seenIds.push(match.response_key_id) }
- return {
- response_key_id: match ? match.response_key_id: null,
- response_key_category: match ? match.response_key_category: 'profile',
- response_key_prompt: match ? match.response_key_prompt: step,
- response_key_description: match ? match.response_key_description: null,
- responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : []
- }
- })
- const unseen = this.questionsFromDb.filter(q => !seenIds.includes(q.response_key_id))
- return [...stepsInCommon, ...unseen]
- }
- async getQuestions() {
- try {
- this.questionsFromDb = await fetchQuestions()
- return this.questionsFromDb
- } catch(err) {
- console.error(err)
- }
- }
- async createSurvey(langFile, roleTree) {
- if(!this.questionsFromDb.length) {
- const res = await this.getQuestions()
- console.warn(`Attempted to create a survey before getting questions: retrieved ${res.length} questions`)
- }
- const steps = this._setSteps(langFile)
- return new Survey(steps, roleTree)
- }
- }
-
- export { SurveyFactory }
|