Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

survey.service.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { db } from '../utils/db.js'
  2. /**
  3. * Get Survey for first time profile creation from the database and
  4. * create a class from the data and
  5. * validate the incoming against the schema
  6. * @param {number} profileId
  7. * @returns {array} instantiated Profile objects (see: /entites/profile)
  8. */
  9. const fetchQuestions = async () => {
  10. let withResponses
  11. try {
  12. const questions = await db.get(`/survey/questions`)
  13. // Add responses to match the format from the survery factory
  14. withResponses = questions.map(q => {
  15. q.responses = !q.responses ? [] : q.responses
  16. return q
  17. })
  18. } catch (error) {
  19. console.error(`[Survey Service]: ${error}\nquestions: ${withResponses}`)
  20. }
  21. return withResponses
  22. }
  23. const updateSurveyByProfileId = async (surveyResponses, profileId) => {
  24. surveyResponses.forEach(responseKeyIdwithVal => {
  25. const keyId = responseKeyIdwithVal.response_key_id
  26. const val = responseKeyIdwithVal.val
  27. // PATCH
  28. db.patch(`/profile/${profileId}/update/${keyId}`, [
  29. {
  30. response_id: 2,
  31. profile_id: profileId,
  32. response_key_id: keyId,
  33. val: val,
  34. },
  35. ])
  36. })
  37. }
  38. const scoreSurveyByProfileId = async (profileId, maxDistance = 99) => {
  39. const scoreSurvey = await db.get(
  40. `/profile/${profileId}/score?max_distance=${maxDistance}`,
  41. )
  42. return scoreSurvey
  43. }
  44. const fetchResponsesByProfileId = async profileId => {
  45. return await db.get(`/profile/${profileId}/responses`) // TODO write on backend, does not exist
  46. }
  47. export {
  48. fetchQuestions,
  49. updateSurveyByProfileId,
  50. scoreSurveyByProfileId,
  51. fetchResponsesByProfileId,
  52. }