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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { db } from '../utils/db'
  2. import { Survey } from '../entities/survey'
  3. /**
  4. * Get Survey for first time profile creation from the database and
  5. * create a class from the data and
  6. * validate the incoming against the schema
  7. * @param {number} profileId
  8. * @returns {array} instantiated Profile objects (see: /entites/profile)
  9. */
  10. const fetchSurveyByProfileId = async profileId => {
  11. const myquestions = await db.get(`/survey/questions`)
  12. const allsteps = {}
  13. const questionsPerStep = 3
  14. const stepsNeeded = Math.ceil(myquestions.length / questionsPerStep)
  15. // Create the steps needed inside allsteps
  16. for (let i = 1; i <= stepsNeeded; i++) {
  17. allsteps[`step-${i}`] = []
  18. }
  19. // Add the questions into each step, dividing into questionsPerStep
  20. for (let i = 0; i < myquestions.length; i++) {
  21. const question = myquestions[i]
  22. // Reformats myquestions into the format we want
  23. const reformatted = {
  24. id: question.response_key_id,
  25. type: 'input-string',
  26. question: question.response_key_prompt,
  27. responses: null,
  28. description: question.response_key_description,
  29. category: question.response_key_category,
  30. }
  31. for (let n = 1; n <= stepsNeeded; n++) {
  32. if (i >= questionsPerStep * (n - 1) && i < questionsPerStep * n) {
  33. allsteps[`step-${n}`].push(reformatted)
  34. }
  35. }
  36. }
  37. const mysurvey = new Survey(allsteps)
  38. return mysurvey
  39. }
  40. const saveSurveyByProfileID = async (surveyResponses, profileId) => {
  41. surveyResponses.forEach(responseKeyIdwithVal => {
  42. const keyId = responseKeyIdwithVal.response_key_id
  43. const val = responseKeyIdwithVal.val
  44. // POST
  45. const myresponses = db.post(`/profile/${profileId}/respond?response_key_id=${keyId}&val=${val}`)
  46. return myresponses
  47. })
  48. }
  49. const updateSurveyByProfileId = async (surveyResponses, profileId) => {
  50. surveyResponses.forEach(responseKeyIdwithVal => {
  51. const keyId = responseKeyIdwithVal.response_key_id
  52. const val = responseKeyIdwithVal.val
  53. // PATCH
  54. const myresponses = db.patch(`/profile/${profileId}/update/${keyId}`,
  55. [
  56. {
  57. response_id: 2,
  58. profile_id: profileId,
  59. response_key_id: keyId,
  60. val: val,
  61. },
  62. ]
  63. )
  64. })
  65. }
  66. const scoreSurveyByProfileId = async (profileId, maxDistance) => {
  67. const scoreSurvey = await db.get(`/profile/${profileId}/score?max_distance=${maxDistance}`)
  68. return scoreSurvey
  69. }
  70. export {
  71. fetchSurveyByProfileId,
  72. saveSurveyByProfileID,
  73. updateSurveyByProfileId,
  74. scoreSurveyByProfileId,
  75. }