-
Notifications
You must be signed in to change notification settings - Fork 0
API created for Dashboard Data #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||||||||||||||||||||
| import { DEPARTMENTS } from "../constants/departments.js"; | ||||||||||||||||||||||||
| import Attendance from "../models/Attendance.js" | ||||||||||||||||||||||||
| import Employee from "../models/Employee.js" | ||||||||||||||||||||||||
| import LeaveApplication from "../models/LeaveApplication.js"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| //Get dashboard for employee and admin | ||||||||||||||||||||||||
| //GET /api/dashboard | ||||||||||||||||||||||||
| export const getDashboard = async (req, res) => { | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const session = req.session; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (session.role === "ADMIN") { | ||||||||||||||||||||||||
| const [totalEmployees, todayAttendance, pendingLeaves] = await | ||||||||||||||||||||||||
| Promise.all([ | ||||||||||||||||||||||||
| Employee.countDocuments({ isDeleted: { $ne: true } }), | ||||||||||||||||||||||||
| Attendance.countDocuments({ | ||||||||||||||||||||||||
| date: { | ||||||||||||||||||||||||
| $gte: new Date(new Date().setHours(0, 0, 0, 0)), | ||||||||||||||||||||||||
| $lt: new Date(new Date().setHours(0, 0, 0, 0)), | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
|
Comment on lines
+18
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 20 and Line 21 both resolve to the same timestamp, so the query becomes Suggested fix+ const startOfToday = new Date();
+ startOfToday.setHours(0, 0, 0, 0);
+ const startOfTomorrow = new Date(startOfToday);
+ startOfTomorrow.setDate(startOfTomorrow.getDate() + 1);
+
const [totalEmployees, todayAttendance, pendingLeaves] = await
Promise.all([
Employee.countDocuments({ isDeleted: { $ne: true } }),
Attendance.countDocuments({
date: {
- $gte: new Date(new Date().setHours(0, 0, 0, 0)),
- $lt: new Date(new Date().setHours(0, 0, 0, 0)),
+ $gte: startOfToday,
+ $lt: startOfTomorrow,
}
}),🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| LeaveApplication.countDocuments({ status: "PENDING" }) | ||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return res.json({ | ||||||||||||||||||||||||
| role: "ADMIN", | ||||||||||||||||||||||||
| totalEmployees, | ||||||||||||||||||||||||
| totalDepartments: DEPARTMENTS.length, | ||||||||||||||||||||||||
| todayAttendance, | ||||||||||||||||||||||||
| pendingLeaves, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| const employee = await Employee.findOne({ userId: session.userId }).lean(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!employee) | ||||||||||||||||||||||||
| return res.status(404).json({ error: "Employee not found" }); | ||||||||||||||||||||||||
|
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Soft-deleted employees can still fetch dashboard data. This lookup only checks Suggested fix- const employee = await Employee.findOne({ userId: session.userId }).lean();
+ const employee = await Employee.findOne({
+ userId: session.userId,
+ isDeleted: { $ne: true },
+ }).lean();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const [currentMonthAttendance, pendingLeaves, latestPayslip] = await Promise.all([ | ||||||||||||||||||||||||
| Attendance.countDocuments({ | ||||||||||||||||||||||||
| employeeId: employee._id, | ||||||||||||||||||||||||
| date: { | ||||||||||||||||||||||||
| $gte: new Date(today.getFullYear(), today.getMonth(), today.getDate(), 1), | ||||||||||||||||||||||||
| $lt: new Date(today.getFullYear(), today.getMonth() + 1, 1), | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
|
Comment on lines
+40
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 44 and Line 45 reference Suggested fix+ const today = new Date();
+ const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
+ const startOfNextMonth = new Date(today.getFullYear(), today.getMonth() + 1, 1);
+
const [currentMonthAttendance, pendingLeaves, latestPayslip] = await Promise.all([
Attendance.countDocuments({
employeeId: employee._id,
date: {
- $gte: new Date(today.getFullYear(), today.getMonth(), today.getDate(), 1),
- $lt: new Date(today.getFullYear(), today.getMonth() + 1, 1),
+ $gte: startOfMonth,
+ $lt: startOfNextMonth,
}
}),🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| LeaveApplication.countDocuments({ | ||||||||||||||||||||||||
| employeeId: employee._id, | ||||||||||||||||||||||||
| status: "PENDING", | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
| Payslip.findOne({ employeeId: employee._id }).sort({ createdAt: -1 }).lean() | ||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return res.json({ | ||||||||||||||||||||||||
| role: "EMPLOYEE", | ||||||||||||||||||||||||
| employee: { ...employee, id: employee._id.toString() }, | ||||||||||||||||||||||||
| currentMonthAttendance, | ||||||||||||||||||||||||
| pendingLeaves, | ||||||||||||||||||||||||
| latestPayslip: latestPayslip ? { ...latestPayslip, id: latestPayslip._id.toString() } : null, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||
| console.error("Dashbaord error:", error); | ||||||||||||||||||||||||
| return res.status(500).json({ error: "Failed" }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Router } from "express"; | ||
| import { protect } from "../middlewares/protect.js"; | ||
| import { getDashboard } from "../controllers/dashboardController.js"; | ||
|
Comment on lines
+2
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -e
echo "Candidate middleware files:"
fd -t f 'protect\.js|auth\.js' server
echo
echo "Where protect is exported:"
rg -n -C2 --glob 'server/**/*.js' 'export\s+(const|function)\s+protect|export\s*\{\s*protect\s*\}' server
echo
echo "Where the dashboard route imports protect from:"
rg -n -C2 --glob 'server/routes/**/*.js' 'import\s*\{\s*protect\s*\}\s*from' server/routesRepository: harshGupta090722/EMS Length of output: 1771 Fix incorrect import path for the The All other route files use this correct path. The current import will fail during module resolution. 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
|
|
||
| const dashboardRouter = Router(); | ||
|
|
||
|
|
||
| dashboardRouter.get("/", protect, getDashboard); | ||
|
|
||
|
|
||
| export default dashboardRouter; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
Payslipbefore using it on Line 53.The non-admin branch calls
Payslip.findOne(...), but this model is never imported in this file. That path will crash with aReferenceError.Suggested fix
import { DEPARTMENTS } from "../constants/departments.js"; import Attendance from "../models/Attendance.js" import Employee from "../models/Employee.js" import LeaveApplication from "../models/LeaveApplication.js"; +import Payslip from "../models/Payslip.js";📝 Committable suggestion
🤖 Prompt for AI Agents