From 9e2c2409196dcc41779dc1b887ed1e1bf8657587 Mon Sep 17 00:00:00 2001 From: Henrique Date: Tue, 16 Apr 2024 14:53:41 -0400 Subject: [PATCH] EJS implementation on the user's card in shiftTracker --- modules/data-service.js | 7 +------ routes/{root.js => routes.js} | 19 ++++++++++++++----- routes/timeRoutes.js | 8 ++++---- server.js | 3 +-- views/{shiftTracker.html => shiftTracker.ejs} | 12 ++++++------ 5 files changed, 26 insertions(+), 23 deletions(-) rename routes/{root.js => routes.js} (85%) rename views/{shiftTracker.html => shiftTracker.ejs} (92%) diff --git a/modules/data-service.js b/modules/data-service.js index fc1c3ef..a2e4e9a 100644 --- a/modules/data-service.js +++ b/modules/data-service.js @@ -1,9 +1,4 @@ -import { - client, - dbName, - collectionName, - connectToDatabase, -} from "./database.js"; +import { client, dbName, collectionName } from "./database.js"; // Call the connectToDatabase function to establish the connection // connectToDatabase(); diff --git a/routes/root.js b/routes/routes.js similarity index 85% rename from routes/root.js rename to routes/routes.js index ebb4aad..c302b78 100644 --- a/routes/root.js +++ b/routes/routes.js @@ -9,10 +9,11 @@ import { createUser, getUserByEmail, getUserByName, + getUserBySin, } from "../modules/data-service-auth.js"; import jwt from "jsonwebtoken"; import cookieParser from "cookie-parser"; -import { requireAuth } from "../middleware/authMiddleware.js"; +import { checkUser } from "../middleware/authMiddleware.js"; const router = express.Router(); const currentDir = process.cwd(); @@ -57,7 +58,7 @@ router.post("/create-user", async (req, res) => { } }); -router.post("/shift-table", async (req, res) => { +router.post("/shift-tracker", async (req, res) => { const { email, password } = req.body; try { @@ -74,7 +75,7 @@ router.post("/shift-table", async (req, res) => { const token = createToken(user._id); res .cookie("jwt", token, { httpOnly: true, maxAge: maxAge * 1000 }) - .sendFile(path.resolve(currentDir, "views", "shiftTracker.html")); + .sendFile(path.resolve(currentDir, "views", "shiftTracker.ejs")); } else { res.status(401).json({ message: "Invalid password" }); } @@ -117,8 +118,16 @@ router.get("/logout", (req, res) => { res.cookie("jwt", "", { maxAge: 1 }).redirect("/"); }); -router.get("/shift-tracker", requireAuth, (req, res) => { - res.sendFile(path.resolve(currentDir, "views", "shiftTracker.html")); +router.get("/shift-tracker", checkUser, async (req, res) => { + // res.sendFile(path.resolve(currentDir, "views", "shiftTracker.ejs")); + const userId = res.locals.user._id; + const userInfo = await getUserBySin(client, userId); + console.log(JSON.stringify(userInfo)); + res.render(path.resolve(currentDir, "views", "shiftTracker.ejs"), { + userName: userInfo.fullname, + userEmail: userInfo.email, + userPhone: userInfo.phone, + }); }); router.use("/record", timeRouter); diff --git a/routes/timeRoutes.js b/routes/timeRoutes.js index ccd33a6..9c0b4c7 100644 --- a/routes/timeRoutes.js +++ b/routes/timeRoutes.js @@ -21,7 +21,7 @@ timeRouter.post("/time-in", checkUser, async (req, res) => { const lastShift = await checkLastShift(client, userId); if (lastShift) { console.log(`Last shift ${lastShift}`); - if (lastShift.workedHours === undefined) { + if (lastShift.shift.workedHours === undefined) { console.log(`Last shift worked hours ${lastShift.workedHours}`); return res .status(400) @@ -114,13 +114,13 @@ timeRouter.post("/time-out", checkUser, async (req, res) => { const lastShift = await checkLastShift(client, userId); - if (!lastShift || lastShift.timeOutNum) { + if (!lastShift || lastShift.shift.timeOutNum) { return res.status(400).json({ message: "Please start a new shift", }); } else if ( - lastShift.breakInNum != undefined && - lastShift.breakOutNum === undefined + lastShift.shift.breakInNum != undefined && + lastShift.shift.breakOutNum === undefined ) { return res .status(400) diff --git a/server.js b/server.js index bb2224f..c7ae84f 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,6 @@ import express from "express"; -import session from "express-session"; import path from "path"; -import router from "./routes/root.js"; +import router from "./routes/routes.js"; import { client, connectToDatabase, diff --git a/views/shiftTracker.html b/views/shiftTracker.ejs similarity index 92% rename from views/shiftTracker.html rename to views/shiftTracker.ejs index eb2954a..06b8c9e 100644 --- a/views/shiftTracker.html +++ b/views/shiftTracker.ejs @@ -12,17 +12,17 @@
- +
Company: CoffeeCollab - Manager: Henrique sagara +
- First Name: Evrim - Last Name: Ciftci - email: exampleemail@gmail.com - Phone Number: +1 6558 5885 58 + Name: <%=userName%> + + email: <%=userEmail%> + Phone Number: <%= userPhone%>