| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import { db } from '../utils/db.js'
-
- /**
- * Get Survey for first time profile creation from the database and
- * create a class from the data and
- * validate the incoming against the schema
- * @param {number} profileId
- * @returns {array} instantiated Profile objects (see: /entites/profile)
- */
- const fetchQuestions = async () => {
- let withResponses
- try {
- const questions = await db.get(`/survey/questions`)
- // Add responses to match the format from the survery factory
- withResponses = questions.map(q => {
- q.responses = !q.responses ? [] : q.responses
- return q
- })
- } catch (error) {
- console.error(`[Survey Service]: ${error}\nquestions: ${withResponses}`)
- }
- return withResponses
- }
-
- const updateSurveyByProfileId = async (surveyResponses, profileId) => {
- surveyResponses.forEach(responseKeyIdwithVal => {
- const keyId = responseKeyIdwithVal.response_key_id
- const val = responseKeyIdwithVal.val
- // PATCH
- db.patch(`/profile/${profileId}/update/${keyId}`, [
- {
- response_id: 2,
- profile_id: profileId,
- response_key_id: keyId,
- val: val,
- },
- ])
- })
- }
-
- const scoreSurveyByProfileId = async (profileId, maxDistance = 99) => {
- const scoreSurvey = await db.get(
- `/profile/${profileId}/score?max_distance=${maxDistance}`,
- )
- return scoreSurvey
- }
-
- const fetchResponsesByProfileId = async profileId => {
- return await db.get(`/profile/${profileId}/responses`) // TODO write on backend, does not exist
- }
-
- export {
- fetchQuestions,
- updateSurveyByProfileId,
- scoreSurveyByProfileId,
- fetchResponsesByProfileId,
- }
|