| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { db } from '../utils/db'
- import { Survey } from '../entities/survey'
-
- /**
- * 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 fetchSurveyByProfileId = async profileId => {
- const myquestions = await db.get(`/survey/questions`)
- const allsteps = {}
- const questionsPerStep = 3
- const stepsNeeded = Math.ceil(myquestions.length / questionsPerStep)
-
- // Create the steps needed inside allsteps
- for (let i = 1; i <= stepsNeeded; i++) {
- allsteps[`step-${i}`] = []
- }
-
- // Add the questions into each step, dividing into questionsPerStep
- for (let i = 0; i < myquestions.length; i++) {
- const question = myquestions[i]
- // Reformats myquestions into the format we want
- const reformatted = {
- id: question.response_key_id,
- type: 'input-string',
- question: question.response_key_prompt,
- responses: null,
- description: question.response_key_description,
- category: question.response_key_category,
- }
- for (let n = 1; n <= stepsNeeded; n++) {
- if (i >= questionsPerStep * (n - 1) && i < questionsPerStep * n) {
- allsteps[`step-${n}`].push(reformatted)
- }
- }
- }
- const mysurvey = new Survey(allsteps)
- return mysurvey
- }
-
- const saveSurveyByProfileID = async (surveyResponses, profileId) => {
- surveyResponses.forEach(responseKeyIdwithVal => {
- const keyId = responseKeyIdwithVal.response_key_id
- const val = responseKeyIdwithVal.val
-
- // POST
- const myresponses = db.post(`/profile/${profileId}/respond?response_key_id=${keyId}&val=${val}`)
- return myresponses
- })
- }
-
- const updateSurveyByProfileId = async (surveyResponses, profileId) => {
- surveyResponses.forEach(responseKeyIdwithVal => {
- const keyId = responseKeyIdwithVal.response_key_id
- const val = responseKeyIdwithVal.val
- // PATCH
- const myresponses = db.patch(`/profile/${profileId}/update/${keyId}`,
- [
- {
- response_id: 2,
- profile_id: profileId,
- response_key_id: keyId,
- val: val,
- },
- ]
- )
- })
- }
-
- const scoreSurveyByProfileId = async (profileId, maxDistance) => {
- const scoreSurvey = await db.get(`/profile/${profileId}/score?max_distance=${maxDistance}`)
- return scoreSurvey
- }
-
- export {
- fetchSurveyByProfileId,
- saveSurveyByProfileID,
- updateSurveyByProfileId,
- scoreSurveyByProfileId,
- }
|