You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

survey.service.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const questions = await db.get(`/survey/questions`)
  11. // Add responses to match the format from the survery factory
  12. return questions.map(q => {
  13. q.responses = !q.responses ? [] : q.responses
  14. return q
  15. })
  16. }
  17. const updateSurveyByProfileId = async (surveyResponses, profileId) => {
  18. surveyResponses.forEach(responseKeyIdwithVal => {
  19. const keyId = responseKeyIdwithVal.response_key_id
  20. const val = responseKeyIdwithVal.val
  21. // PATCH
  22. db.patch(`/profile/${profileId}/update/${keyId}`, [
  23. {
  24. response_id: 2,
  25. profile_id: profileId,
  26. response_key_id: keyId,
  27. val: val,
  28. },
  29. ])
  30. })
  31. }
  32. // const updateSurveyByProfileId = async (surveyResponses, profileId) => {
  33. // surveyResponses.forEach(responseKeyIdwithVal => {
  34. // const keyId = responseKeyIdwithVal.response_key_id
  35. // const val = responseKeyIdwithVal.val
  36. // // PATCH
  37. // db.patch(`/profile/${profileId}/update/${keyId}`, [
  38. // {
  39. // response_id: 2,
  40. // profile_id: profileId,
  41. // response_key_id: keyId,
  42. // val: val,
  43. // },
  44. // ])
  45. // })
  46. // }
  47. const scoreSurveyByProfileId = async (profileId, maxDistance = 99) => {
  48. const scoreSurvey = await db.get(
  49. `/profile/${profileId}/score?max_distance=${maxDistance}`,
  50. )
  51. return scoreSurvey
  52. }
  53. const fetchResponsesByProfileId = async profileId => {
  54. return await db.get(`/profile/${profileId}/responses`)
  55. }
  56. export {
  57. fetchQuestions,
  58. updateSurveyByProfileId,
  59. scoreSurveyByProfileId,
  60. fetchResponsesByProfileId,
  61. }