diff --git a/server/src/case-api.js b/server/src/case-api.js index 825486a6a..e5c6fb997 100644 --- a/server/src/case-api.js +++ b/server/src/case-api.js @@ -2,7 +2,6 @@ const DB = require('./db.js') const log = require('tangy-log').log const path = require('path') const fs = require('fs') -const { v4: uuidV4 } = require('uuid') const {Case: Case, CaseEvent: CaseEvent, EventForm: EventForm } = require('./classes/case.class.js') @@ -73,6 +72,17 @@ createCase = async (req, res) => { } } +readCase = async (req, res) => { + const groupDb = new DB(req.params.groupId) + let data = {} + try { + data = await groupDb.get(caseId); + } catch (err) { + res.status(500).send(err); + } + res.send(data) +} + createCaseEvent = async (req, res) => { let groupId = req.params.groupId let caseId = req.params.caseId @@ -198,42 +208,52 @@ createParticipant = async (req, res) => { } getCaseEventFormSurveyLinks = async (req, res) => { - const caseId = req.params.caseId - const groupDb = new DB(req.params.groupId) - let data = [] try { - const results = await groupDb.get(caseId) - if (results.rows.length > 0) { - const caseDoc = results.rows[0].doc - const caseDefinition = _getCaseDefinition(groupId, caseDoc.caseDefinitionId) - for (let event of doc.events) { - let eventForm = event.eventForms.find((f) => f.id === req.params.eventFormId); - if (eventForm) { - let formId; - for (let eventDefinition of caseDefinition.eventDefinitions) { - formId = eventDefinition.find((e) => e.id === eventForm.eventFormDefinitionId).formId - if (formId) break; + const caseId = req.params.caseId + const groupId = req.params.groupId + + const GROUPS_DB = new DB('groups'); + const groupData = await GROUPS_DB.get(groupId); + const onlineSurveys = groupData.onlineSurveys ? groupData.onlineSurveys : []; + + const groupDb = new DB(req.params.groupId) + const caseDoc = await groupDb.get(caseId) + const caseDefinition = _getCaseDefinition(groupId, caseDoc.caseDefinitionId) + + for (let event of caseDoc.events) { + for (let eventForm of event.eventForms.filter((f) => !f.formResponseId)) { + for (let eventDefinition of caseDefinition.eventDefinitions) { + let eventFormDefinition = eventDefinition.eventFormDefinitions.find((e) => e.id === eventForm.eventFormDefinitionId) + if (eventFormDefinition && eventFormDefinition.formId) { + const formId = eventFormDefinition.formId + const survey = onlineSurveys.find((s) => s.formId === formId && s.published === true && s.locked === true) + if (survey) { + const origin = `${process.env.T_PROTOCOL}://${process.env.T_HOST_NAME}` + const pathname = `releases/prod/online-survey-apps/${groupId}/${formId}` + const hash = `#/case/event/form/${caseId}/${event.id}/${eventForm.id}` + const url = `${origin}/${pathname}/${hash}` + data.push(url) + } } } - const url = `http://localhost/releases/prod/online-survey-apps/group-344fabfe-f892-4a6d-a1da-58616949982f/${formId}/#/caseFormResponse/${caseId}/${eventForm.id}` - data.push(url) - break; } } + res.send(data) } catch (err) { res.status(500).send(err); } - res.send(data) } module.exports = { getCaseDefinitions, getCaseDefinition, createCase, + readCase, createCaseEvent, createEventForm, readEventForm, updateEventForm, - createParticipant + createParticipant, + getCaseEventFormSurveyLinks } \ No newline at end of file diff --git a/server/src/express-app.js b/server/src/express-app.js index 595d296ed..5e0c49421 100644 --- a/server/src/express-app.js +++ b/server/src/express-app.js @@ -47,15 +47,18 @@ const { extendSession, findUserByUsername, const {registerUser, getUserByUsername, isUserSuperAdmin, isUserAnAdminUser, getGroupsByUser, deleteUser, getAllUsers, checkIfUserExistByUsername, findOneUserByUsername, findMyUser, updateUser, restoreUser, updateMyUser} = require('./users'); -const {login: surveyLogin, getResponse: getSurveyResponse, saveResponse: saveSurveyResponse, publishSurvey, unpublishSurvey} = require('./online-survey') +const {login: surveyLogin, getResponse: getSurveyResponse, saveResponse: saveSurveyResponse, publishSurvey, unpublishSurvey, getOnlineSurveys} = require('./online-survey') const { getCaseDefinitions, getCaseDefinition, createCase, + readCase, createCaseEvent, createEventForm, readEventForm, - updateEventForm + updateEventForm, + createParticipant, + getCaseEventFormSurveyLinks } = require('./case-api') const { createUserProfile } = require('./user-profile') log.info('heartbeat') @@ -191,22 +194,25 @@ app.get('/users/groupPermissionsByGroupName/:groupName', isAuthenticated, getUse app.get('/configuration/passwordPolicyConfig', isAuthenticated, passwordPolicyConfig); /** - * User Profile API + * User Profile API Routes */ -app.post('/userProfile/createUserProfile/:groupId', createUserProfile); +app.post('/userProfile/createUserProfile/:groupId', isAuthenticated, createUserProfile); /** - * Case API routes + * Case API Routes */ app.get('/case/getCaseDefinitions/:groupId', isAuthenticated, getCaseDefinitions); app.get('/case/getCaseDefinition/:groupId/:caseDefinitionId', isAuthenticated, getCaseDefinition); app.post('/case/createCase/:groupId/:caseDefinitionId', isAuthenticated, createCase); +app.post('/case/readCase/:groupId/:caseId', isAuthenticated, readCase); app.post('/case/createCaseEvent/:groupId/:caseId/:caseEventDefinitionId', isAuthenticated, createCaseEvent); app.post('/case/createEventForm/:groupId/:caseId/:caseEventId/:caseEventFormDefinitionId', isAuthenticated, createEventForm); app.get('/case/readEventForm/:groupId/:caseId/:caseEventId/:eventFormId', isAuthenticated, readEventForm); app.post('/case/updateEventForm/:groupId/:caseId/:caseEventId/:eventFormId', isAuthenticated, updateEventForm); +app.post('/case/createParticipant/:groupId/:caseId/:caseEventId/:eventFormId', isAuthenticated, createParticipant); +app.get('/case/getCaseEventFormSurveyLinks/:groupId/:caseId', isAuthenticated, getCaseEventFormSurveyLinks); /** * Online survey routes @@ -216,6 +222,7 @@ app.post('/onlineSurvey/login/:groupId/:accessCode', surveyLogin); app.post('/onlineSurvey/publish/:groupId/:formId', isAuthenticated, publishSurvey); app.put('/onlineSurvey/unpublish/:groupId/:formId', isAuthenticated, unpublishSurvey); app.post('/onlineSurvey/saveResponse/:groupId/:formId', hasSurveyUploadKey, saveSurveyResponse); +app.get('/onlineSurvey/getOnlineSurveys/:groupId', isAuthenticated, getOnlineSurveys); /* * More API diff --git a/server/src/online-survey.js b/server/src/online-survey.js index 5c1e820b6..70514e5b9 100644 --- a/server/src/online-survey.js +++ b/server/src/online-survey.js @@ -117,10 +117,33 @@ const unpublishSurvey = async (req, res) => { } }; +const getOnlineSurveys = async (req, res) => { + try { + const { groupId } = req.params; + const formId = req.params.formId || null; // optional + if (!groupId) { + return res.status(500).send('Could not find role'); + } + const data = await GROUPS_DB.get(groupId); + if (data.onlineSurveys) { + if (formId) { + return res.status(200).send({ data: data.onlineSurveys.filter((s) => s.formId === formId) }); + } else { + return res.status(200).send({ data: data.onlineSurveys }); + } + } else { + return res.status(200).send({ data: [] }); + } + } catch (error) { + res.status(500).send('Could not find survey'); + } +} + module.exports = { login, getResponse, saveResponse, publishSurvey, - unpublishSurvey + unpublishSurvey, + getOnlineSurveys } \ No newline at end of file