/** @module survey/survey */ import { _baseRecord } from '../index.js' import { surveySchema } from './survey.schema.js' import { answersSchema } from './survey.answer.schema.js' import { aspectsArr } from '../../utils/lang.js' const SCORED = aspectsArr const _isScored = id => SCORED.includes(id) const _makeCategoryFriendly = responseCategory => { const labels = responseCategory.split('_vs_') labels.forEach((a, i) => { if (a.indexOf('_') == -1) return labels[i] = a.split('_').join(' ') }) return labels } const _formatAspectQuestions = steps => { return steps .map(q => { if (!_isScored(q.response_key_id)) return null return { id: q.response_key_id, question: q.response_key_prompt, labels: _makeCategoryFriendly(q.response_key_category), answer: null, } }) .filter(step => step != null) } class Survey extends _baseRecord { constructor(questionSteps) { super() this.type = this.constructor.name.toLowerCase() /** Fields */ this.steps = [...questionSteps] // ! required this.aspectQuestions = _formatAspectQuestions(this.steps) console.log('this.aspectQuestions: ', JSON.stringify(this.aspectQuestions)) } validateAnswer(answer) { const validate = answersSchema.validate(answer) if (validate.error) { console.error(`error: ${validate.error}`) } return !validate.error ? true: false } isValid() { const validate = surveySchema.validate(this) /** * Log out some useful error messages */ if (validate.error) { console.error(`error: ${validate.error} - ${this.type} validation`) } /** validate(this) always returns something so force it to a bool */ return !validate.error ? true : false } } export { Survey }