-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Henrique Sagara
authored and
Henrique Sagara
committed
Mar 11, 2024
1 parent
43c30e6
commit 9c436ff
Showing
3 changed files
with
78 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |