From b23c57b399206abdc441e8130b2aa9b8eadd48a5 Mon Sep 17 00:00:00 2001 From: Rafael Park Date: Tue, 26 Nov 2024 01:32:48 -0800 Subject: [PATCH] added endpoints for creating & retrieving logs (adult cardiac) --- backend/src/routes/logbooks-route.js | 22 +++-- backend/src/services/logbooks-service.js | 104 +++++++++++++++++++---- 2 files changed, 102 insertions(+), 24 deletions(-) diff --git a/backend/src/routes/logbooks-route.js b/backend/src/routes/logbooks-route.js index 2793a793..add94897 100644 --- a/backend/src/routes/logbooks-route.js +++ b/backend/src/routes/logbooks-route.js @@ -1,6 +1,6 @@ import express from "express"; import auth from "../middlewares/auth.js"; -import { createLogbook, getUserLogbooks, getLogbook, createLog } from "../services/logbooks-service.js"; +import { createLogbook, getUserLogbooks, getLogbook, createLog, getLogbookLogs, getLog } from "../services/logbooks-service.js"; const router = express.Router(); @@ -10,18 +10,28 @@ router.post("/", auth, async (req, res) => { }); router.get("/", auth, async (req, res) => { - const logbooks = await getUserLogbooks(req); - res.status(200).json({ data: logbooks }); + const userLogbooks = await getUserLogbooks(req); + res.status(200).json({ data: userLogbooks }); }); -router.get("/:id", auth, async (req, res) => { +router.get("/:logbookID", auth, async (req, res) => { const logbook = await getLogbook(req); res.status(200).json({ data: logbook }); }); -router.post("/logs", auth, async (req, res) => { +router.post("/:logbookID/logs", auth, async (req, res) => { const log = await createLog(req); res.status(201).json({ data: log }); }); -export default router; \ No newline at end of file +router.get("/:logbookID/logs/", auth, async (req, res) => { + const logbookLogs = await getLogbookLogs(req) + res.status(200).json({ data: logbookLogs }); +}) + +router.get("/:logbookID/logs/:logID", auth, async (req, res) => { + const log = await getLog(req) + res.status(200).json({ data: log }); +}) + +export default router; diff --git a/backend/src/services/logbooks-service.js b/backend/src/services/logbooks-service.js index 2973ea0d..0acb7772 100644 --- a/backend/src/services/logbooks-service.js +++ b/backend/src/services/logbooks-service.js @@ -4,13 +4,10 @@ export async function createLogbook(req) { const { type } = req.body; const { data, error } = await supabase.from("logbooks").insert({ type: type }).select(); if (error) { - console.error("Error in createLogbook:", error.message); throw new Error(error.message); - } else { - return data[0]; } + return data[0]; } catch (error) { - console.error("Error in createLogbook:", error.message); throw new Error(error.message); } } @@ -19,40 +16,111 @@ export async function getUserLogbooks(req) { try { const supabase = req.supabase; const token = req.header("Authorization")?.split(" ")[1]; - const userID = parseUserID(token) + const userID = parseUserID(token); const { data, error } = await supabase.from("logbooks").select().eq("user_id", userID); if (error) { - console.error("Error in getUserLogbooks:", error.message); throw new Error(error.message); - } else { - return data; } + return data; } catch (error) { - console.error("Error in getUserLogbooks:", error.message); throw new Error(error.message); } } function parseUserID(token) { - const parts = token.split('.'); + const parts = token.split("."); const decodedPayload = JSON.parse(atob(parts[1])); - const userID = decodedPayload["sub"] - return userID + const userID = decodedPayload["sub"]; + return userID; } export async function getLogbook(req) { try { const supabase = req.supabase; - const { id } = req.params; - const { data, error } = await supabase.from("logbooks").select().eq("logbook_id", id); + const { logbookID } = req.params; + const { data, error } = await supabase.from("logbooks").select().eq("id", logbookID); if (error) { - console.error("Error in getLogbook:", error.message); throw new Error(error.message); - } else { - return data[0]; } + return data[0]; + } catch (error) { + throw new Error(error.message); + } +} + +export async function createLog(req) { + try { + const supabase = req.supabase; + const { logbookID } = req.params; + const { type } = req.body; + const logbookType = await getLogbookType(logbookID, supabase); + if (type !== logbookType) { + throw new Error("Error: Log does not match logbook type"); + } + let data; + switch (type) { + case "adult_cardiac_logs": + data = createAdultCardiacLog(req, supabase); + break; + default: + break; + } + return data; + } catch (error) { + throw new Error(error.message); + } +} + +async function createAdultCardiacLog(req, supabase) { + const { logbookID } = req.params; + const { caseNo } = req.body; + const { data, error } = await supabase + .from("adult_cardiac_logs") + .insert({ + logbook_id: logbookID, + case_no: Number(caseNo), + }) + .select(); + if (error) { + throw new Error(error.message); + } + return data[0]; +} + +async function getLogbookType(logbookID, supabase) { + const { data, error } = await supabase.from("logbooks").select().eq("id", logbookID); + if (error) { + throw new Error(error.message); + } + return data[0]["type"]; +} + +export async function getLogbookLogs(req) { + try { + const supabase = req.supabase; + const { logbookID } = req.params; + const logbookType = await getLogbookType(logbookID, supabase); + const { data, error } = await supabase.from(logbookType).select(); + if (error) { + throw new Error(error.message); + } + return data; + } catch (error) { + throw new Error(error.message); + } +} + +export async function getLog(req) { + try { + const supabase = req.supabase; + const { logbookID, logID } = req.params; + const logbookType = await getLogbookType(logbookID, supabase); + const { data, error } = await supabase.from(logbookType).select().eq("id", logID); + if (error) { + throw new Error(error.message); + } + return data[0]; } catch (error) { - console.error("Error in getLogbook:", error.message); throw new Error(error.message); } } \ No newline at end of file