import { db } from '../utils/db.js' /** * 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 fetchQuestions = async () => { let withResponses try { const questions = await db.get(`/survey/questions`) // Add responses to match the format from the survery factory withResponses = questions.map(q => { q.responses = !q.responses ? [] : q.responses return q }) } catch (error) { console.error(`[Survey Service]: ${error}\nquestions: ${withResponses}`) } return withResponses } const insertNewSurveyResponse = async (surveyResponse, profileId) => { const keyId = surveyResponse.response_key_id const val = surveyResponse.val // POST db.post(`/profile/${profileId}/insert/${keyId}`, { profile_id: profileId, response_key_id: keyId, val: val, }) } const updateSurveyByProfileId = async (surveyResponses, profileId) => { surveyResponses.forEach(responseKeyIdwithVal => { const keyId = responseKeyIdwithVal.response_key_id const val = responseKeyIdwithVal.val // PATCH db.patch(`/profile/${profileId}/update/${keyId}`, [ { response_id: 2, profile_id: profileId, response_key_id: keyId, val: val, }, ]) }) } const scoreSurveyByProfileId = async ( profileId, maxDistance = 99, duration, presence, certifications, ) => { const scoreSurvey = await db.get( `/profile/${profileId}/score?max_distance=${maxDistance}&duration=${duration}&presence=${presence}&certifications=${certifications}`, ) return scoreSurvey } const fetchResponsesByProfileId = async profileId => { return await db.get(`/profile/${profileId}/responses`) } export { fetchQuestions, insertNewSurveyResponse, updateSurveyByProfileId, scoreSurveyByProfileId, fetchResponsesByProfileId, }