attendance section update

This commit is contained in:
Sibunnayak 2024-07-18 14:47:37 +05:30
parent 92e5ef9f94
commit 0d0a2978c0

View File

@ -196,6 +196,18 @@ export const markAttendance = async (req, res) => {
const userId = req.user._id;
const userType = req.userType;
// Get today's date
const today = new Date();
today.setHours(0, 0, 0, 0);
// Check if the date in req.body is not today's date
if (!isSameDay(new Date(date), today)) {
return res.status(400).json({
success: false,
message: `Attendance can only be marked for today's date: ${today.toLocaleDateString()}`,
});
}
// Get the start and end of the day to check if the attendance is already marked
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
@ -207,7 +219,11 @@ export const markAttendance = async (req, res) => {
const existingAttendance = await Attendance.findOne({
userId,
userType,
"records.date": { $gte: startOfDay, $lte: endOfDay },
records: {
$elemMatch: {
date: { $gte: startOfDay, $lte: endOfDay }
}
}
});
if (existingAttendance) {
@ -246,6 +262,15 @@ export const markAttendance = async (req, res) => {
}
};
// Helper function to check if two dates are on the same day
function isSameDay(date1, date2) {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
}
// Get attendance by user ID
export const getAttendanceByUser = async (req, res) => {
try {