Skip to content

Commit

Permalink
nested routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Sagara authored and Henrique Sagara committed Mar 11, 2024
1 parent 43c30e6 commit 9c436ff
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 66 deletions.
8 changes: 4 additions & 4 deletions public/jsForPages/shiftTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ document.addEventListener("DOMContentLoaded", function () {
document.getElementById("timeInBtn").addEventListener("click", async () => {
try {
// Make an AJAX request to the server to trigger the time-in action
const response = await fetch("/time-in", {
const response = await fetch("/record/time-in", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -201,7 +201,7 @@ document.getElementById("timeInBtn").addEventListener("click", async () => {
document.getElementById("timeOutBtn").addEventListener("click", async () => {
try {
// Make an AJAX request to the server to trigger the time-in action
const response = await fetch("/time-out", {
const response = await fetch("/record/time-out", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -232,7 +232,7 @@ document.getElementById("timeOutBtn").addEventListener("click", async () => {

document.getElementById("break").addEventListener("click", async () => {
try {
const response = await fetch("/break-in", {
const response = await fetch("/record/break-in", {
method: "POST",
headers: {
"Content-type": "application/json",
Expand Down Expand Up @@ -261,7 +261,7 @@ document.getElementById("break").addEventListener("click", async () => {

document.getElementById("break").addEventListener("click", async () => {
try {
const response = await fetch("/break-in", {
const response = await fetch("/record/break-in", {
method: "POST",
headers: {
"Content-type": "application/json",
Expand Down
64 changes: 2 additions & 62 deletions routes/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import path from "path";
import bodyParser from "body-parser";
import bcrypt from 'bcrypt';
import timeRouter from "./timeRoutes.js";
import { client, authenticateUser } from "../modules/database.js";
import { deleteUserById, addNewUser, timeIn, timeOut, checkLastShift, breakIn, breakOut} from "../modules/data-service.js";
import { createUser, getUserByEmail, getUserByName, getUserBySin } from "../modules/data-service-auth.js";
Expand All @@ -13,67 +14,6 @@ const currentDir = process.cwd();

router.use(bodyParser.urlencoded({ extended: true}));

router.post("/time-in", authenticateUser, async (req, res) => {
// const userId = req.body.userId;
const userId = req.session.user.id

const lastShift = await checkLastShift(client, userId)
if (lastShift.workedHours === undefined) {
return res.status(400).json({ message: "Previous shift time-out hasn't been recorded" });
}

try {
await timeIn(client, userId);
res.status(200).json({ message: "Time-in recorded successfully" });
} catch (error) {
console.log(error);
res.status(500).json({ message: "Internal Server Error" });
}
});


router.post("/break-in", authenticateUser, async (req, res) => {
const userId = req.session.user.id;

// const shiftCheck = await checkLastShift(client, userId);
const lastShift = await checkLastShift(client, userId);
if(lastShift.timeInNum != undefined && lastShift.workedHours != undefined){
return res.status(400).json({ message: "You haven't started a shift yet." });
}

try{
await breakIn(client, userId);
res.status(200).json({message: "Break-in recorded successfully"});
} catch (error) {
console.log(error);
res.status(500).json({ message: "Internal Server Error" });
}

})



router.post("/time-out", authenticateUser, async (req, res) => {
const userId = req.session.user.id;

const lastShift = await checkLastShift(client, userId)
console.log(lastShift)
if (lastShift.timeInNum === undefined) {
return res.status(400).json({ message: "You haven't started a shift yet." });
}
else if(lastShift.breakInNum != undefined && lastShift.breakOutNum === undefined){
return res.status(400).json({ message: "You didn't finish your break. Time-out anyway?" });
}

try {
await timeOut(client, userId);
res.status(200).json({message: "Time-out recorded successfully"})
} catch (e) {
console.error(e);
res.status(500).json({message: "Internal Server Error"})
}
});

// Route to handle creating a new user
router.post("/create-user", async (req, res) => {

Expand Down Expand Up @@ -167,6 +107,6 @@ router.get('/create-user', (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'registrationTest.html'))
})


router.use("/record", timeRouter);

export default router;
72 changes: 72 additions & 0 deletions routes/timeRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import express from "express";
import bodyParser from "body-parser";
import { client, authenticateUser } from "../modules/database.js";
import { timeIn, timeOut, checkLastShift, breakIn, breakOut } from "../modules/data-service.js";


const timeRouter = express.Router();

timeRouter.use(bodyParser.urlencoded({ extended: true}));

timeRouter.post("/time-in", authenticateUser, async (req, res) => {
// const userId = req.body.userId;
const userId = req.session.user.id

const lastShift = await checkLastShift(client, userId)
if (lastShift.workedHours === undefined) {
return res.status(400).json({ message: "Previous shift time-out hasn't been recorded" });
}

try {
await timeIn(client, userId);
res.status(200).json({ message: "Time-in recorded successfully" });
} catch (error) {
console.log(error);
res.status(500).json({ message: "Internal Server Error" });
}
});


timeRouter.post("/break-in", authenticateUser, async (req, res) => {
const userId = req.session.user.id;

// const shiftCheck = await checkLastShift(client, userId);
const lastShift = await checkLastShift(client, userId);
if(lastShift.timeInNum != undefined && lastShift.workedHours != undefined){
return res.status(400).json({ message: "You haven't started a shift yet." });
}

try{
await breakIn(client, userId);
res.status(200).json({message: "Break-in recorded successfully"});
} catch (error) {
console.log(error);
res.status(500).json({ message: "Internal Server Error" });
}

})



timeRouter.post("/time-out", authenticateUser, async (req, res) => {
const userId = req.session.user.id;

const lastShift = await checkLastShift(client, userId)
console.log(lastShift)
if (lastShift.timeInNum === undefined) {
return res.status(400).json({ message: "You haven't started a shift yet." });
}
else if(lastShift.breakInNum != undefined && lastShift.breakOutNum === undefined){
return res.status(400).json({ message: "You didn't finish your break. Time-out anyway?" });
}

try {
await timeOut(client, userId);
res.status(200).json({message: "Time-out recorded successfully"})
} catch (e) {
console.error(e);
res.status(500).json({message: "Internal Server Error"})
}
});

export default timeRouter;

0 comments on commit 9c436ff

Please sign in to comment.