From 5986f0445e4ebecd07005f2647e112c2615031c1 Mon Sep 17 00:00:00 2001 From: Rafael Park Date: Tue, 3 Dec 2024 18:48:02 -0800 Subject: [PATCH] pr fixes --- backend/src/services/logbooks-service.js | 90 +++++++++++++++++------- backend/src/utils/get-logbook-type.js | 6 +- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/backend/src/services/logbooks-service.js b/backend/src/services/logbooks-service.js index 3acd2001..22221eae 100644 --- a/backend/src/services/logbooks-service.js +++ b/backend/src/services/logbooks-service.js @@ -5,38 +5,59 @@ import parseUserID from "../utils/parse-user-id.js"; export async function createLogbook(req) { const supabase = req.supabase; - const body = req.body - return insertTable(supabase, "logbooks", body); + const body = req.body; + const logbook = await insertTable(supabase, "logbooks", body); + return logbook; } export async function getUserLogbooks(req) { - const supabase = req.supabase; - const token = req.header("Authorization")?.split(" ")[1]; - const userID = parseUserID(token); - return getTable(supabase, "logbooks", "user_id", userID, "collection"); + try { + const supabase = req.supabase; + const token = req.header("Authorization")?.split(" ")[1]; + const userID = parseUserID(token); + if (userID.error) { + throw new Error(userID.error.message) + } + const userLogbooks = await getTable(supabase, "logbooks", "user_id", userID, "collection"); + return userLogbooks; + } catch (error) { + return { error: error.message }; + } } export async function getLogbook(req) { - const supabase = req.supabase; - const { logbookID } = req.params; - return getTable(supabase, "logbooks", "id", logbookID, "resource"); + try { + const supabase = req.supabase; + const { logbookID } = req.params; + const logbook = await getTable(supabase, "logbooks", "id", logbookID, "resource"); + if (typeof logbook == "undefined") { + throw new Error(`logbook ${logbookID} does not exist`); + } + return logbook; + } catch (error) { + return { error: error.message }; + } } export async function createLog(req) { try { const supabase = req.supabase; const { logbookID } = req.params; - let body = req.body - body['logbook_id'] = logbookID + let body = req.body; + body["logbook_id"] = logbookID; const logbookType = await getLogbookType(logbookID, supabase); - if (body['type'] !== logbookType) { - throw new Error(`log type '${body['type']}' does not match logbook type '${logbookType}'`); + if (logbookType.error) { + throw new Error(logbookType.error.message); + } + if (body["type"] !== logbookType) { + throw new Error(`log type '${body["type"]}' does not match logbook type '${logbookType}'`); } - switch (body['type']) { + switch (body["type"]) { case "adult_cardiac_logs": - return insertTable(supabase, "adult_cardiac_logs", body) + const log = await insertTable(supabase, "adult_cardiac_logs", body); + return log; default: - throw new Error(`log and logbook type '${body['type']}' are invalid`); + throw new Error(`log and logbook type '${body["type"]}' are invalid`); } } catch (error) { return { error: error.message }; @@ -44,15 +65,34 @@ export async function createLog(req) { } export async function getLogbookLogs(req) { - const supabase = req.supabase; - const { logbookID } = req.params; - const logbookType = await getLogbookType(logbookID, supabase); - return getTable(supabase, logbookType, "logbook_id", logbookID, "collection"); + try { + const supabase = req.supabase; + const { logbookID } = req.params; + const logbookType = await getLogbookType(logbookID, supabase); + if (logbookType.error) { + throw new Error(logbookType.error.message); + } + const logbookLogs = await getTable(supabase, logbookType, "logbook_id", logbookID, "collection"); + return logbookLogs; + } catch (error) { + return { error: error.message }; + } } export async function getLog(req) { - const supabase = req.supabase; - const { logbookID, logID } = req.params; - const logbookType = await getLogbookType(logbookID, supabase); - return getTable(supabase, logbookType, "id", logID, "resource"); -} \ No newline at end of file + try { + const supabase = req.supabase; + const { logbookID, logID } = req.params; + const logbookType = await getLogbookType(logbookID, supabase); + if (logbookType.error) { + throw new Error(logbookType.error.message); + } + const log = await getTable(supabase, logbookType, "id", logID, "resource"); + if (typeof log == "undefined") { + throw new Error(`log ${logID} does not exist`); + } + return log; + } catch (error) { + return { error: error.message }; + } +} diff --git a/backend/src/utils/get-logbook-type.js b/backend/src/utils/get-logbook-type.js index dc35e52d..b43a7d25 100644 --- a/backend/src/utils/get-logbook-type.js +++ b/backend/src/utils/get-logbook-type.js @@ -1,10 +1,12 @@ export default async function getLogbookType(logbookID, supabase) { try { const { data, error } = await supabase.from("logbooks").select().eq("id", logbookID); - if (error) { + if (data.length == 0 && typeof error == "undefined") { + throw new Error(`logbook ${logbookID} does not exist`); + } else if (error) { throw new Error(error.message); } - return data[0]['type']; + return data[0]["type"]; } catch (error) { return { error: error.message }; }