Skip to content

Commit

Permalink
Add getOnlineSurvey and getCaseEventFormSurveyLinks APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
esurface committed Jun 7, 2024
1 parent 0360347 commit 1e0077c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 26 deletions.
60 changes: 40 additions & 20 deletions server/src/case-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
17 changes: 12 additions & 5 deletions server/src/express-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 24 additions & 1 deletion server/src/online-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1e0077c

Please sign in to comment.