Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
parkrafael committed Dec 4, 2024
1 parent 30c8a74 commit 5986f04
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
90 changes: 65 additions & 25 deletions backend/src/services/logbooks-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,94 @@ 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);

Check failure on line 57 in backend/src/services/logbooks-service.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected lexical declaration in case block

Check failure on line 57 in backend/src/services/logbooks-service.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected lexical declaration in case block
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 };
}
}

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");
}
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 };
}
}
6 changes: 4 additions & 2 deletions backend/src/utils/get-logbook-type.js
Original file line number Diff line number Diff line change
@@ -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 };
}
Expand Down

0 comments on commit 5986f04

Please sign in to comment.