384 lines
11 KiB
JavaScript
384 lines
11 KiB
JavaScript
// import { Attendance } from "./AttandanceModel.js";
|
|
|
|
// export const markAttendance = async (req, res) => {
|
|
// try {
|
|
// const { date, time, location, notes } = req.body;
|
|
// const salesCoordinatorId = req.user._id;
|
|
|
|
// // 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);
|
|
|
|
// const endOfDay = new Date(date);
|
|
// endOfDay.setHours(23, 59, 59, 999);
|
|
|
|
// // Check if the attendance record exists for today
|
|
// const existingAttendance = await AttendanceSalesCoOrdinator.findOne({
|
|
// salesCoordinator: salesCoordinatorId,
|
|
// "records.date": { $gte: startOfDay, $lte: endOfDay },
|
|
// });
|
|
|
|
// if (existingAttendance) {
|
|
// return res.status(400).json({
|
|
// success: false,
|
|
// message: "Attendance for today is already marked.",
|
|
// });
|
|
// }
|
|
|
|
// // Check if attendance record exists for the sales coordinator
|
|
// let attendance = await AttendanceSalesCoOrdinator.findOne({
|
|
// salesCoordinator: salesCoordinatorId,
|
|
// });
|
|
|
|
// if (!attendance) {
|
|
// // Create a new attendance record if it doesn't exist
|
|
// attendance = new AttendanceSalesCoOrdinator({
|
|
// salesCoordinator: salesCoordinatorId,
|
|
// records: [],
|
|
// });
|
|
// }
|
|
|
|
// // Add the new attendance record to the array
|
|
// attendance.records.push({ date, time, location, notes });
|
|
// await attendance.save();
|
|
|
|
// res.status(201).json({
|
|
// success: true,
|
|
// message: "Attendance marked successfully",
|
|
// record: { date, time, location, notes },
|
|
// });
|
|
// } catch (error) {
|
|
// res.status(500).json({
|
|
// success: false,
|
|
// message: error.message || "Something went wrong",
|
|
// });
|
|
// }
|
|
// };
|
|
|
|
// export const getAttendanceBySalesCoordinator = async (req, res) => {
|
|
// try {
|
|
// const salesCoordinatorId = req.user._id;
|
|
|
|
// const attendance = await AttendanceSalesCoOrdinator.findOne({
|
|
// salesCoordinator: salesCoordinatorId,
|
|
// }).populate("salesCoordinator", "name email");
|
|
|
|
// if (!attendance) {
|
|
// return res.status(404).json({
|
|
// success: false,
|
|
// message: "No attendance records found for this sales coordinator.",
|
|
// });
|
|
// }
|
|
|
|
// res.status(200).json({
|
|
// success: true,
|
|
// attendance,
|
|
// });
|
|
// } catch (error) {
|
|
// res.status(500).json({
|
|
// success: false,
|
|
// message: error.message || "Something went wrong",
|
|
// });
|
|
// }
|
|
// };
|
|
// // admin
|
|
// export const AdmingetAttendanceBySalesCoordinator = async (req, res) => {
|
|
// try {
|
|
// const { id } = req.params;
|
|
// const { page = 1, show = 10 } = req.query;
|
|
// const limit = parseInt(show);
|
|
// const skip = (page - 1) * limit;
|
|
|
|
// // Find attendance records for the given sales coordinator
|
|
// const attendanceDoc = await AttendanceSalesCoOrdinator.findOne({
|
|
// salesCoordinator: id,
|
|
// }).populate("salesCoordinator", "name email");
|
|
|
|
// if (!attendanceDoc) {
|
|
// return res.status(404).json({
|
|
// success: false,
|
|
// message: "No attendance records found for this sales coordinator.",
|
|
// });
|
|
// }
|
|
|
|
// // Pagination and slicing records
|
|
// const totalData = attendanceDoc.records.length;
|
|
// const paginatedRecords = attendanceDoc.records.slice(skip, skip + limit);
|
|
|
|
// res.status(200).json({
|
|
// success: true,
|
|
// salesCoordinator: attendanceDoc.salesCoordinator,
|
|
// attendance: paginatedRecords,
|
|
// total_data: totalData,
|
|
// });
|
|
// } catch (error) {
|
|
// res.status(500).json({
|
|
// success: false,
|
|
// message: error.message || "Something went wrong",
|
|
// });
|
|
// }
|
|
// };
|
|
|
|
// export const getTodayAttendance = async (req, res) => {
|
|
// try {
|
|
// const today = new Date();
|
|
// today.setHours(0, 0, 0, 0);
|
|
|
|
// const startOfDay = new Date(today);
|
|
// const endOfDay = new Date(today);
|
|
// endOfDay.setHours(23, 59, 59, 999);
|
|
|
|
// const { page = 1, show = 10 } = req.query;
|
|
// const limit = parseInt(show);
|
|
// const skip = (page - 1) * limit;
|
|
|
|
// const attendances = await AttendanceSalesCoOrdinator.aggregate([
|
|
// { $unwind: '$records' },
|
|
// {
|
|
// $match: {
|
|
// 'records.date': { $gte: startOfDay, $lte: endOfDay }
|
|
// }
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: 'salescoordinators',
|
|
// localField: 'salesCoordinator',
|
|
// foreignField: '_id',
|
|
// as: 'salesCoordinator'
|
|
// }
|
|
// },
|
|
// { $unwind: '$salesCoordinator' },
|
|
// {
|
|
// $facet: {
|
|
// totalData: [{ $count: 'count' }],
|
|
// attendance: [
|
|
// { $skip: skip },
|
|
// { $limit: limit },
|
|
// {
|
|
// $project: {
|
|
// salesCoordinator: {
|
|
// id: '$salesCoordinator._id',
|
|
// name: '$salesCoordinator.name',
|
|
// email: '$salesCoordinator.email'
|
|
// },
|
|
// date: '$records.date',
|
|
// time: '$records.time',
|
|
// location: '$records.location',
|
|
// note: '$records.notes'
|
|
// }
|
|
// }
|
|
// ]
|
|
// }
|
|
// }
|
|
// ]);
|
|
|
|
// const totalData = attendances[0].totalData[0] ? attendances[0].totalData[0].count : 0;
|
|
|
|
// res.status(200).json({
|
|
// success: true,
|
|
// attendance: attendances[0].attendance,
|
|
// total_data: totalData
|
|
// });
|
|
// } catch (error) {
|
|
// res.status(500).json({
|
|
// success: false,
|
|
// message: error.message || 'Something went wrong'
|
|
// });
|
|
// }
|
|
// };
|
|
|
|
import { Attendance } from "./AttendanceModel.js";
|
|
|
|
// Mark attendance
|
|
export const markAttendance = async (req, res) => {
|
|
try {
|
|
const { date, time, location, notes } = req.body;
|
|
const userId = req.user._id;
|
|
const userType = req.userType;
|
|
|
|
// 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);
|
|
|
|
const endOfDay = new Date(date);
|
|
endOfDay.setHours(23, 59, 59, 999);
|
|
|
|
// Check if the attendance record exists for today
|
|
const existingAttendance = await Attendance.findOne({
|
|
userId,
|
|
userType,
|
|
"records.date": { $gte: startOfDay, $lte: endOfDay },
|
|
});
|
|
|
|
if (existingAttendance) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: "Attendance for today is already marked.",
|
|
});
|
|
}
|
|
|
|
// Check if attendance record exists for the user
|
|
let attendance = await Attendance.findOne({ userId, userType });
|
|
|
|
if (!attendance) {
|
|
// Create a new attendance record if it doesn't exist
|
|
attendance = new Attendance({
|
|
userId,
|
|
userType,
|
|
records: [],
|
|
});
|
|
}
|
|
|
|
// Add the new attendance record to the array
|
|
attendance.records.push({ date, time, location, notes });
|
|
await attendance.save();
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
message: "Attendance marked successfully",
|
|
record: { date, time, location, notes },
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Something went wrong",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Get attendance by user ID
|
|
export const getAttendanceByUser = async (req, res) => {
|
|
try {
|
|
const userId = req.user._id;
|
|
|
|
const attendance = await Attendance.findOne({
|
|
userId,
|
|
}).populate("userId", "name email");
|
|
|
|
if (!attendance) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: "No attendance records found for this user.",
|
|
});
|
|
}
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
attendance,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Something went wrong",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Admin route to get attendance by user ID
|
|
export const AdmingetAttendanceByUser = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { page = 1, show = 10 } = req.query;
|
|
const limit = parseInt(show);
|
|
const skip = (page - 1) * limit;
|
|
|
|
// Find attendance records for the given user
|
|
const attendanceDoc = await Attendance.findOne({
|
|
userId: id,
|
|
}).populate("userId", "name email");
|
|
|
|
if (!attendanceDoc) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: "No attendance records found for this user.",
|
|
});
|
|
}
|
|
|
|
// Pagination and slicing records
|
|
const totalData = attendanceDoc.records.length;
|
|
const paginatedRecords = attendanceDoc.records.slice(skip, skip + limit);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
user: attendanceDoc.userId,
|
|
attendance: paginatedRecords,
|
|
total_data: totalData,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Something went wrong",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Get today's attendance for admin
|
|
export const getTodayAttendance = async (req, res) => {
|
|
try {
|
|
const today = new Date();
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
const startOfDay = new Date(today);
|
|
const endOfDay = new Date(today);
|
|
endOfDay.setHours(23, 59, 59, 999);
|
|
|
|
const { page = 1, show = 10 } = req.query;
|
|
const limit = parseInt(show);
|
|
const skip = (page - 1) * limit;
|
|
|
|
const attendances = await Attendance.aggregate([
|
|
{ $unwind: '$records' },
|
|
{
|
|
$match: {
|
|
'records.date': { $gte: startOfDay, $lte: endOfDay }
|
|
}
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: 'users',
|
|
localField: 'userId',
|
|
foreignField: '_id',
|
|
as: 'user'
|
|
}
|
|
},
|
|
{ $unwind: '$user' },
|
|
{
|
|
$facet: {
|
|
totalData: [{ $count: 'count' }],
|
|
attendance: [
|
|
{ $skip: skip },
|
|
{ $limit: limit },
|
|
{
|
|
$project: {
|
|
user: {
|
|
id: '$user._id',
|
|
name: '$user.name',
|
|
email: '$user.email',
|
|
userType: '$userType'
|
|
},
|
|
date: '$records.date',
|
|
time: '$records.time',
|
|
location: '$records.location',
|
|
notes: '$records.notes',
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]);
|
|
|
|
const totalData = attendances[0].totalData[0]?.count || 0;
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
total_data: totalData,
|
|
attendance: attendances[0].attendance
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: error.message || "Something went wrong",
|
|
});
|
|
}
|
|
};
|