diff --git a/modules/data-service-auth.js b/modules/data-service-auth.js index ab14a228..9bf5a1ed 100644 --- a/modules/data-service-auth.js +++ b/modules/data-service-auth.js @@ -22,7 +22,7 @@ async function isUserIdExists(userId) { async function getUserByEmail(client, email) { const result = await client.db(dbName).collection(userCollection).findOne({email: email}) - + return result } diff --git a/public/jsForPages/shiftTracker.js b/public/jsForPages/shiftTracker.js index c3033813..92eb4f39 100644 --- a/public/jsForPages/shiftTracker.js +++ b/public/jsForPages/shiftTracker.js @@ -164,6 +164,49 @@ document.addEventListener("DOMContentLoaded", function () { }); +// Post the time on the data base +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", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ userId: 1 }), + }); + + if (response.ok) { + const data = await response.json(); + console.log(data.message); + } else { + console.error("Error recording time-in"); + } + } catch (error) { + console.error(error); + } +}); +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", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ userId: 1 }), + }); + + if (response.ok) { + const data = await response.json(); + console.log(data.message); + } else { + console.error("Error recording time-in"); + } + } catch (error) { + console.error(error); + } +}); diff --git a/routes/root.js b/routes/root.js index 4ad4cb18..56aa620f 100644 --- a/routes/root.js +++ b/routes/root.js @@ -1,9 +1,9 @@ import express from "express"; import path from "path"; import bodyParser from "body-parser"; -import bcrypt from 'bcrypt' +import bcrypt from 'bcrypt'; import { client, authenticateUser } from "../modules/database.js"; -import { deleteUserById, addNewUser} from "../modules/data-service.js"; +import { deleteUserById, addNewUser, timeIn, timeOut} from "../modules/data-service.js"; import { createUser, getUserByEmail, getUserByName, getUserBySin } from "../modules/data-service-auth.js"; @@ -13,6 +13,42 @@ 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 + + console.log(userId) + + 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.get("/time-in", authenticateUser, (req, res) => { + res.sendFile(path.resolve(currentDir, 'views', 'time-in.html')); +}); + +router.post("/time-out", authenticateUser, async (req, res) => { + const userId = req.session.user.id; + + 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"}) + } +}); + +router.get("/time-out", authenticateUser, (req, res) => { + res.sendFile(path.resolve(currentDir, 'views', 'time-out.html')) +}) + // Route to handle creating a new user router.post("/create-user", async (req, res) => { @@ -43,7 +79,7 @@ router.post("/login", async (req, res) => { const {email, password} = req.body; try { - const user = await getUserByEmail(email) + const user = await getUserByEmail(client, email) console.log(user) if(!user) { @@ -58,12 +94,13 @@ router.post("/login", async (req, res) => { id: user._id, email: user.email, } - res.status(200).json({message: 'Login successful'}); + res.sendFile(path.resolve(currentDir, 'views', 'shiftTracker.html')) } else { res.status(401).json({ message: 'Invalid email or password' }); } + } catch (error) { console.error('Error during login:', error); res.status(500).json({ message: 'Internal Server Error' }); @@ -104,4 +141,7 @@ router.get('/about-us', (req, res) => { router.get('/create-user', (req, res) => { res.sendFile(path.resolve(currentDir, 'views', 'registrationTest.html')) }) + + + export default router; \ No newline at end of file diff --git a/routes/time-in.js b/routes/time-in.js deleted file mode 100644 index deae203e..00000000 --- a/routes/time-in.js +++ /dev/null @@ -1,26 +0,0 @@ -import express from "express"; -import path from "path"; -import { client, authenticateUser } from "../models/database.js"; -import { timeIn } from "../models/data-service.js"; - -const router = express.Router(); -const currentDir = process.cwd(); - -router.post("/time-in", authenticateUser, async (req, res) => { - const userId = req.body.userId; - - 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.get("/time-in", authenticateUser, (req, res) => { - res.sendFile(path.resolve(currentDir, 'views', 'time-in.html')); -}); - -export default router; \ No newline at end of file diff --git a/routes/time-out.js b/routes/time-out.js deleted file mode 100644 index d6a01f27..00000000 --- a/routes/time-out.js +++ /dev/null @@ -1,25 +0,0 @@ -import express from "express"; -import path from "path"; -import { client } from "../models/database.js"; -import { timeOut } from "../models/data-service.js"; - -const router = express.Router(); -const currentDir = process.cwd(); - -router.post("/time-out", authenticateUser, async (req, res) => { - const userId = req.body.userId; - - 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"}) - } -}); - -router.get("/time-out", authenticateUser, (req, res) => { - res.sendFile(path.resolve(currentDir, 'views', 'time-out.html')) -}) - -export default router; \ No newline at end of file diff --git a/server.js b/server.js index 40e314bc..87195db8 100644 --- a/server.js +++ b/server.js @@ -23,7 +23,7 @@ const sessionSecret = 'htsagara'; app.use(session({ secret: sessionSecret, resave: false, - saveUninitialized: true, + saveUninitialized: false, })) // Call the connectToDatabase function to establish the connection diff --git a/views/index.html b/views/index.html index b25e486b..60d75c8d 100644 --- a/views/index.html +++ b/views/index.html @@ -7,7 +7,7 @@ - + @@ -76,34 +76,36 @@

By CoffeeCollab

Register Now!

- - +
- -
+ +
-
+ +
- - -
+ + + +
-
+ +
-
+
- - +
+ +
@@ -149,7 +152,7 @@

Register Now!