diff --git a/backend/index.js b/backend/index.js index fddd630d..056c2437 100644 --- a/backend/index.js +++ b/backend/index.js @@ -1,8 +1,8 @@ import cors from "cors"; import dotenv from "dotenv"; import express from "express"; -import authRoutes from "./src/routes/authRoutes.js"; -import logRoutes from "./src/routes/logRoutes.js"; +import authRoutes from "./src/routes/auth-route.js"; +import logRoutes from "./src/routes/logbooks-route.js"; dotenv.config(); @@ -15,9 +15,8 @@ const PORT = process.env.PORT || 8080; app.use(cors(corsOptions)); app.use(express.json()); -//Routes app.use('/api/auth', authRoutes); -app.use('/api/log', logRoutes); +app.use('/api/logbooks', logRoutes); app.listen(PORT, () => { console.log(`Server listening on ${PORT}`); diff --git a/backend/src/middlewares/__tests__/auth.test.js b/backend/src/middlewares/__tests__/auth.test.js deleted file mode 100644 index d8d52d4b..00000000 --- a/backend/src/middlewares/__tests__/auth.test.js +++ /dev/null @@ -1,3 +0,0 @@ -test("empty test", () => { - expect(true).toBe(true) -}) \ No newline at end of file diff --git a/backend/src/middlewares/auth.js b/backend/src/middlewares/auth.js index f758065c..e0596177 100644 --- a/backend/src/middlewares/auth.js +++ b/backend/src/middlewares/auth.js @@ -26,5 +26,4 @@ const auth = async (req, res, next) => { } }; -export default auth; - +export default auth; \ No newline at end of file diff --git a/backend/src/routes/authRoutes.js b/backend/src/routes/auth-route.js similarity index 95% rename from backend/src/routes/authRoutes.js rename to backend/src/routes/auth-route.js index b0474aac..a0f746f7 100644 --- a/backend/src/routes/authRoutes.js +++ b/backend/src/routes/auth-route.js @@ -3,7 +3,6 @@ import auth from "../middlewares/auth.js"; const router = express.Router(); -//auth test router.post("/check", auth, async (req, res) => { res.json({ message: "Auth Check Ok"}); }) diff --git a/backend/src/routes/logRoutes.js b/backend/src/routes/logRoutes.js deleted file mode 100644 index 4f7bca2f..00000000 --- a/backend/src/routes/logRoutes.js +++ /dev/null @@ -1,17 +0,0 @@ -import express from "express"; -import auth from "../middlewares/auth.js"; -import insertTable from "../services/cardiacSurgeryAdultService.js" - -const router = express.Router(); - -router.post("/cardiacSurgeryAdultService", auth, async (req, res) => { - try { - const result = await insertTable(req, res); - res.json(result); - } catch(error) { - console.error("Caught Error:", error.message); - res.status(500).json({message: error.message}); - } -}) - -export default router; \ No newline at end of file diff --git a/backend/src/routes/logbooks-route.js b/backend/src/routes/logbooks-route.js new file mode 100644 index 00000000..2793a793 --- /dev/null +++ b/backend/src/routes/logbooks-route.js @@ -0,0 +1,27 @@ +import express from "express"; +import auth from "../middlewares/auth.js"; +import { createLogbook, getUserLogbooks, getLogbook, createLog } from "../services/logbooks-service.js"; + +const router = express.Router(); + +router.post("/", auth, async (req, res) => { + const logbook = await createLogbook(req); + res.status(201).json({ data: logbook }); +}); + +router.get("/", auth, async (req, res) => { + const logbooks = await getUserLogbooks(req); + res.status(200).json({ data: logbooks }); +}); + +router.get("/:id", auth, async (req, res) => { + const logbook = await getLogbook(req); + res.status(200).json({ data: logbook }); +}); + +router.post("/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 diff --git a/backend/src/services/cardiacSurgeryAdultService.js b/backend/src/services/cardiacSurgeryAdultService.js deleted file mode 100644 index 5cc3ce69..00000000 --- a/backend/src/services/cardiacSurgeryAdultService.js +++ /dev/null @@ -1,76 +0,0 @@ - -async function insertTable(req) { - try{ - const supabase = req.supabase; - - const { - case_no, - patient_id, - type, - surgeon, - or_date, - age, - sex, - reason, - hpi, - social, - pmhx, - meds, - allergies, - exam, - veins, - allen_test, - pulses, - invx, - cxr, - ct, - cath, - surgical_plan, - operative_notes, - post_op_course, - learning_points - } = req.body; - - const error = await supabase.schema("user_info").from("cardiac_surgery_adult_log") - .insert({ - case_no: case_no, - patient_id: patient_id, - type: type, - surgeon: surgeon, - or_date: or_date, - age: age, - sex: sex, - reason: reason, - hpi: hpi, - social: social, - pmhx: pmhx, - meds: meds, - allergies: allergies, - exam: exam, - veins: veins, - allen_test: allen_test, - pulses: pulses, - invx: invx, - cxr: cxr, - ct: ct, - cath: cath, - surgical_plan: surgical_plan, - operative_notes: operative_notes, - post_op_course: post_op_course, - learning_points: learning_points }); - - if (error.error) { - console.log(error); - console.error("Insert Error:", error.error.message); - throw new Error("Failed to insert data: " + error.error.message); - } - - return {message: "Log Successful"}; - } catch (error) { - console.error("Error in insertTable:", error.message); - throw new Error(error.message); - } -} - -export default insertTable; - diff --git a/backend/src/services/logbooks-service.js b/backend/src/services/logbooks-service.js new file mode 100644 index 00000000..2973ea0d --- /dev/null +++ b/backend/src/services/logbooks-service.js @@ -0,0 +1,58 @@ +export async function createLogbook(req) { + try { + const supabase = req.supabase; + 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]; + } + } catch (error) { + console.error("Error in createLogbook:", error.message); + throw new Error(error.message); + } +} + +export async function getUserLogbooks(req) { + try { + const supabase = req.supabase; + const token = req.header("Authorization")?.split(" ")[1]; + 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; + } + } catch (error) { + console.error("Error in getUserLogbooks:", error.message); + throw new Error(error.message); + } +} + +function parseUserID(token) { + const parts = token.split('.'); + const decodedPayload = JSON.parse(atob(parts[1])); + 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); + if (error) { + console.error("Error in getLogbook:", error.message); + throw new Error(error.message); + } else { + return data[0]; + } + } catch (error) { + console.error("Error in getLogbook:", error.message); + throw new Error(error.message); + } +} \ No newline at end of file