From 9c5c03231c4723be17c90ad13168df418601686d Mon Sep 17 00:00:00 2001 From: Henrique Sagara Date: Mon, 11 Mar 2024 20:36:21 -0400 Subject: [PATCH] break-out feature --- public/jsForPages/shiftTracker.js | 39 +++++++++++++++++++++++++++---- routes/timeRoutes.js | 20 ++++++++++++++++ views/shiftTracker.html | 2 +- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/public/jsForPages/shiftTracker.js b/public/jsForPages/shiftTracker.js index 660efe94..d4c3a230 100644 --- a/public/jsForPages/shiftTracker.js +++ b/public/jsForPages/shiftTracker.js @@ -223,7 +223,7 @@ document.getElementById("timeOutBtn").addEventListener("click", async () => { if(errorData.message === "You haven't started a shift yet."){ alert("Please start you shift before you time-out") } - console.error("Error recording time-out", errorData.message); + console.error(errorData.message); } } catch (error) { console.error(error); @@ -252,14 +252,14 @@ document.getElementById("break").addEventListener("click", async () => { if(errorData.message) { alert(errorData.message); } - console.error("Error recording break-in", errorData.message); + console.error(errorData.message); } } catch (error){ console.error(error) } }); -document.getElementById("break").addEventListener("click", async () => { +document.getElementById("breakInBtn").addEventListener("click", async () => { try { const response = await fetch("/record/break-in", { method: "POST", @@ -275,13 +275,42 @@ document.getElementById("break").addEventListener("click", async () => { breakInTime = new Date(); formattedTime = breakInTime.toLocaleTimeString(); - document.getElementById("break").innerHTML = `Your break has started at :${formattedTime}` + document.getElementById("breakInBtn").innerHTML = `Your break has started at :${formattedTime}` + } else{ + const errorData = await response.json(); + if(errorData.message) { + alert(errorData.message); + } + console.error(errorData.message); + } + } catch (error){ + console.error(error) + } +}); + +document.getElementById("breakOutBtn").addEventListener("click", async () => { + try { + const response = await fetch("/record/break-out", { + method: "POST", + headers: { + "Content-type": "application/json", + }, + body: JSON.stringify({userId: 1}), + }); + + if(response.ok) { + const data = await response.json(); + console.log(data.message); + breakOutTime = new Date(); + + formattedTime = breakOutTime.toLocaleTimeString(); + document.getElementById("breakOutBtn").innerHTML = `Your break has ended at :${formattedTime}` } else{ const errorData = await response.json(); if(errorData.message) { alert(errorData.message); } - console.error("Error recording break-in", errorData.message); + console.error( errorData.message); } } catch (error){ console.error(error) diff --git a/routes/timeRoutes.js b/routes/timeRoutes.js index 0af1dd88..370c0ca3 100644 --- a/routes/timeRoutes.js +++ b/routes/timeRoutes.js @@ -46,6 +46,26 @@ timeRouter.post("/break-in", authenticateUser, async (req, res) => { }) +timeRouter.post("/break-out", authenticateUser, async (req, res) => { + const userId = req.session.user.id; + const lastShift = await checkLastShift(client, userId); + + if(lastShift.breakInNum === undefined){ + return res.status(400).json({ message: "You haven't started your break yet." }); + } + else if(lastShift.timeOutNum != undefined){ + return res.status(400).json({ message: "You haven't started your shift yet." }); + } + + try{ + await breakOut(client, userId); + res.status(200).json({message: "Break-out recorded successfully"}); + } catch (error) { + console.log(error); + res.status(500).json({ message: "Internal Server Error" }); + } + +}) timeRouter.post("/time-out", authenticateUser, async (req, res) => { diff --git a/views/shiftTracker.html b/views/shiftTracker.html index 0e30f8ae..b8861877 100644 --- a/views/shiftTracker.html +++ b/views/shiftTracker.html @@ -52,7 +52,7 @@ Punch in -