Skip to content

Commit

Permalink
Fix up case create api
Browse files Browse the repository at this point in the history
  • Loading branch information
esurface committed Jun 8, 2024
1 parent 1e0077c commit 5837be6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 78 deletions.
69 changes: 11 additions & 58 deletions server/src/case-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ createCase = async (req, res) => {
let groupId = req.params.groupId
let caseDefinitionId = req.params.caseDefinitionId

// check that req.body is json, if not, use JSON.parse
let inputs;
try {
if (typeof req.body == 'object' && Object.keys(req.body).length > 0) {
inputs = req.body
}
} catch (err) {
log.error(`Error parsing case inputs from request body: ${err}`)
}

try {
const caseDefinition = _getCaseDefinition(groupId, caseDefinitionId)
let caseDoc = new Case(groupId, caseDefinitionId, caseDefinition)

if (Object.keys(req.body).length > 0) {
const inputs = req.body
if (inputs) {
caseDoc.addInputs(inputs)
}

Expand Down Expand Up @@ -132,60 +141,6 @@ createEventForm = async (req, res) => {
}
}

updateEventForm = async (req, res) => {

let groupId = req.params.groupId
let caseId = req.params.caseId
let caseEventId = req.params.caseEventId
let eventFormId = req.params.eventFormId

const db = new DB(groupId);
const data = req.body;

if (eventFormId) {
let newEventForm = new EventForm(data);
try {
let caseDoc = await db.get(caseId);
if (caseDoc) {
let event = caseDoc.events.find((e) => e.id === caseEventId);
if (event) {
let eventForm = event.eventForms.find((f) => f.id === eventFormId);
if (eventForm) {
eventForm = newEventForm;
} else {
event.eventForms.push(eventForm);
}
await db.put(caseDoc);
}
}
} catch (error) {
console.error(error);
}
}
}

readEventForm = async (req, res) => {
const groupDb = new DB(req.params.groupId)
let data = {}
try {
let options = { key: req.params.eventFormId, include_docs: true }
const results = await groupDb.query('eventForms/eventForms', options);
if (results.rows.length > 0) {
const doc = results.rows[0].doc
for (let event of doc.events) {
let eventForm = event.eventForms.find((f) => f.id === req.params.eventFormId);
if (eventForm) {
data = eventForm
break;
}
}
}
} catch (err) {
res.status(500).send(err);
}
res.send(data)
}

createParticipant = async (req, res) => {
const groupId = req.params.groupId
const caseId = req.params.caseId
Expand Down Expand Up @@ -252,8 +207,6 @@ module.exports = {
readCase,
createCaseEvent,
createEventForm,
readEventForm,
updateEventForm,
createParticipant,
getCaseEventFormSurveyLinks
}
17 changes: 1 addition & 16 deletions server/src/classes/case.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,8 @@ class CaseEvent {
}

class EventForm {

id;
caseId;
caseEventId;
eventFormDefinitionId;
required;
participantId;
complete;
inactive;
formResponseId;

constructor(data) {
Object.assign(this, data);
}

constructor(caseId, caseEventId, eventFormDefinition = null) {
this.id = id || uuidV4()
this.id = uuidV4()
this.caseId = caseId
this.caseEventId = caseEventId
this.eventFormDefinitionId = eventFormDefinition.id
Expand Down
4 changes: 0 additions & 4 deletions server/src/express-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ const {
readCase,
createCaseEvent,
createEventForm,
readEventForm,
updateEventForm,
createParticipant,
getCaseEventFormSurveyLinks
} = require('./case-api')
Expand Down Expand Up @@ -209,8 +207,6 @@ app.post('/case/createCase/:groupId/:caseDefinitionId', isAuthenticated, createC
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);

Expand Down

0 comments on commit 5837be6

Please sign in to comment.