Skip to content

Commit

Permalink
break-out feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Sagara authored and Henrique Sagara committed Mar 12, 2024
1 parent 9c436ff commit 9c5c032
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
39 changes: 34 additions & 5 deletions public/jsForPages/shiftTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions routes/timeRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion views/shiftTracker.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
Punch in
</button>

<button type="button" class="btn" id="break" >
<button type="button" class="btn" id="breakInBtn" >
Take a Break
</button>

Expand Down

0 comments on commit 9c5c032

Please sign in to comment.