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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 insertNewSurveyResponse = async (surveyResponse, profileId) => {
  24. const keyId = surveyResponse.response_key_id
  25. const val = surveyResponse.val
  26. // POST
  27. db.post(`/profile/${profileId}/insert/${keyId}`, {
  28. profile_id: profileId,
  29. response_key_id: keyId,
  30. val: val,
  31. })
  32. }
  33. const updateSurveyByProfileId = async (surveyResponses, profileId) => {
  34. surveyResponses.forEach(responseKeyIdwithVal => {
  35. const keyId = responseKeyIdwithVal.response_key_id
  36. const val = responseKeyIdwithVal.val
  37. // PATCH
  38. db.patch(`/profile/${profileId}/update/${keyId}`, [
  39. {
  40. response_id: 2,
  41. profile_id: profileId,
  42. response_key_id: keyId,
  43. val: val,
  44. },
  45. ])
  46. })
  47. }
  48. const scoreSurveyByProfileId = async (
  49. profileId,
  50. maxDistance = 99,
  51. duration,
  52. presence,
  53. certifications,
  54. ) => {
  55. const scoreSurvey = await db.get(
  56. `/profile/${profileId}/score?max_distance=${maxDistance}&duration=${duration}&presence=${presence}&certifications=${certifications}`,
  57. )
  58. return scoreSurvey
  59. }
  60. const fetchResponsesByProfileId = async profileId => {
  61. return await db.get(`/profile/${profileId}/responses`)
  62. }
  63. export {
  64. fetchQuestions,
  65. insertNewSurveyResponse,
  66. updateSurveyByProfileId,
  67. scoreSurveyByProfileId,
  68. fetchResponsesByProfileId,
  69. }