Skip to content

Commit

Permalink
added endpoints for creating & retrieving logs (adult cardiac)
Browse files Browse the repository at this point in the history
  • Loading branch information
parkrafael committed Nov 26, 2024
1 parent 3ec7752 commit b23c57b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 24 deletions.
22 changes: 16 additions & 6 deletions backend/src/routes/logbooks-route.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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;
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;
104 changes: 86 additions & 18 deletions backend/src/services/logbooks-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}

0 comments on commit b23c57b

Please sign in to comment.