| 12345678910111213141516171819202122232425262728293031323334 |
- import { Survey } from '../entities'
- import { fetchQuestionsByProfileId } from '../services'
-
- class SurveyFactory {
- constructor(responses) {
- this.responsesByCategory = responses
- }
- async setSteps(langFile) {
- const stepsToProcess = [...Object.values(langFile) ]
- console.log(stepsToProcess)
- const questions = await fetchQuestionsByProfileId()
- const seenIds = []
- const stepsInCommon = stepsToProcess.map(step => {
- // Match question to step
- const match = questions.filter(q => q.response_key_prompt == step)[0]
- if(match) { seenIds.push(match.response_key_id) }
- return {
- response_key_category: match ? match.response_key_category: 'profile',
- response_key_description: match ? match.response_key_description: null,
- response_key_id: match ? match.response_key_id: null,
- response_key_prompt: match ? match.response_key_prompt: step,
- responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : []
- }
- })
- const unseen = questions.filter(q => !seenIds.includes(q.response_key_id))
- return [...stepsInCommon, ...unseen]
- }
- async createSurvey(langFile) {
- const steps = await this.setSteps(langFile)
- return new Survey(steps)
- }
- }
-
- export { SurveyFactory }
|