From 5837be682347c927a2cf1bd61611189d996c5f78 Mon Sep 17 00:00:00 2001 From: esurface Date: Sat, 8 Jun 2024 15:38:06 -0400 Subject: [PATCH] Fix up case create api --- server/src/case-api.js | 69 +++++--------------------------- server/src/classes/case.class.js | 17 +------- server/src/express-app.js | 4 -- 3 files changed, 12 insertions(+), 78 deletions(-) diff --git a/server/src/case-api.js b/server/src/case-api.js index e5c6fb997..36fd03bc3 100644 --- a/server/src/case-api.js +++ b/server/src/case-api.js @@ -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) } @@ -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 @@ -252,8 +207,6 @@ module.exports = { readCase, createCaseEvent, createEventForm, - readEventForm, - updateEventForm, createParticipant, getCaseEventFormSurveyLinks } \ No newline at end of file diff --git a/server/src/classes/case.class.js b/server/src/classes/case.class.js index c9f146936..a19d0d8b1 100644 --- a/server/src/classes/case.class.js +++ b/server/src/classes/case.class.js @@ -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 diff --git a/server/src/express-app.js b/server/src/express-app.js index 5e0c49421..839b5ce00 100644 --- a/server/src/express-app.js +++ b/server/src/express-app.js @@ -55,8 +55,6 @@ const { readCase, createCaseEvent, createEventForm, - readEventForm, - updateEventForm, createParticipant, getCaseEventFormSurveyLinks } = require('./case-api') @@ -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);