product pagination attendance and leave

This commit is contained in:
Sibunnayak 2024-11-27 13:26:09 +05:30
parent 9aa6c8180d
commit 8e58b4b5f3
3 changed files with 82 additions and 40 deletions

View File

@ -1,4 +1,5 @@
import { Attendance } from "./AttendanceModel.js";
import { Leave } from "../Leaves/LeaveModel.js";
// Mark attendance
export const markAttendance = async (req, res) => {
@ -38,14 +39,27 @@ export const markAttendance = async (req, res) => {
$lte: endOfDay,
},
});
if (existingAttendance) {
return res.status(400).json({
success: false,
message: "Attendance for today is already marked.",
});
}
// Check if the Leavs record exists for today
const existingLeave = await Leave.findOne({
userId,
userType,
"records.date": {
$gte: startOfDay,
$lte: endOfDay,
},
});
if (existingLeave) {
return res.status(400).json({
success: false,
message: "Leave for today is already marked.",
});
}
// Check if attendance record exists for the user
let attendance = await Attendance.findOne({ userId, userType });
@ -116,7 +130,7 @@ export const AdmingetAttendanceByUser = async (req, res) => {
const attendanceDoc = await Attendance.findOne({
userId: id,
}).populate("userId", "name email");
// console.log(attendanceDoc);
// console.log(attendanceDoc);
if (!attendanceDoc) {
return res.status(404).json({
success: false,

View File

@ -1,4 +1,5 @@
import { Leave } from "./LeaveModel.js";
import { Attendance } from "../Attendance/AttendanceModel.js";
// Mark leave
export const markLeave = async (req, res) => {
@ -45,7 +46,23 @@ export const markLeave = async (req, res) => {
message: "Leave for today is already marked.",
});
}
// Check if the attendance record exists for today
const existingAttendance = await Attendance.findOne({
userId,
userType,
records: {
$elemMatch: {
date: { $gte: startOfDay, $lte: endOfDay },
},
},
});
if (existingAttendance) {
return res.status(400).json({
success: false,
message: "Attendance for today is already marked.",
});
}
// Check if leave record exists for the user
let leave = await Leave.findOne({ userId, userType });
@ -155,7 +172,16 @@ export const getTodayLeave = async (req, res) => {
try {
// Step 1: Get today's date in UTC and set the time to 00:00:00.000
const today = new Date();
const todayStartUTC = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), 0, 0, 0));
const todayStartUTC = new Date(
Date.UTC(
today.getUTCFullYear(),
today.getUTCMonth(),
today.getUTCDate(),
0,
0,
0
)
);
const tomorrowStartUTC = new Date(todayStartUTC);
tomorrowStartUTC.setUTCDate(todayStartUTC.getUTCDate() + 1); // Start of the next day
@ -166,30 +192,30 @@ export const getTodayLeave = async (req, res) => {
// Step 3: Aggregate query to get today's leave records
const leavesWithUserLookup = await Leave.aggregate([
{ $unwind: '$records' },
{ $unwind: "$records" },
{
$match: {
'records.date': {
"records.date": {
$gte: todayStartUTC,
$lt: tomorrowStartUTC
}
}
$lt: tomorrowStartUTC,
},
},
},
{
$lookup: {
from: 'salescoordinators',
localField: 'userId',
foreignField: '_id',
as: 'salesCoOrdinatorUser'
}
from: "salescoordinators",
localField: "userId",
foreignField: "_id",
as: "salesCoOrdinatorUser",
},
},
{
$lookup: {
from: 'territorymanagers',
localField: 'userId',
foreignField: '_id',
as: 'territoryManagerUser'
}
from: "territorymanagers",
localField: "userId",
foreignField: "_id",
as: "territoryManagerUser",
},
},
{
$addFields: {
@ -197,17 +223,17 @@ export const getTodayLeave = async (req, res) => {
$cond: {
if: { $eq: ["$userType", "SalesCoOrdinator"] },
then: { $arrayElemAt: ["$salesCoOrdinatorUser", 0] },
else: { $arrayElemAt: ["$territoryManagerUser", 0] }
}
}
}
else: { $arrayElemAt: ["$territoryManagerUser", 0] },
},
},
},
},
{
$project: {
salesCoOrdinatorUser: 0,
territoryManagerUser: 0
}
}
territoryManagerUser: 0,
},
},
]);
// Step 4: Calculate total data
@ -217,15 +243,17 @@ export const getTodayLeave = async (req, res) => {
const paginatedLeaves = leavesWithUserLookup.slice(skip, skip + limit);
// Step 6: Format the response
const formattedLeaves = paginatedLeaves.map(record => ({
user: record.user ? {
const formattedLeaves = paginatedLeaves.map((record) => ({
user: record.user
? {
id: record.user._id,
name: record.user.name,
email: record.user.email,
mobileNumber: record.user.mobileNumber,
userType: record.userType,
uniqueId: record.user.uniqueId,
} : null,
}
: null,
date: record.records.date,
time: record.records.time,
location: record.records.location,

View File

@ -508,8 +508,8 @@ export const getAllProductAdmin = async (req, res) => {
export const getAllProductUser = async (req, res) => {
try {
// Set default values for pagination
// const PAGE_SIZE = parseInt(req.query?.show || "10");
// const page = parseInt(req.query?.page || "1") - 1;
const PAGE_SIZE = parseInt(req.query?.show || "10");
const page = parseInt(req.query?.page || "1") - 1;
let filter = {};
// Filter by category name
@ -553,15 +553,15 @@ export const getAllProductUser = async (req, res) => {
path: "category addedBy brand",
select: "categoryName name brandName",
})
// .limit(PAGE_SIZE)
// .skip(PAGE_SIZE * page)
.limit(PAGE_SIZE)
.skip(PAGE_SIZE * page)
.sort({ createdAt: -1 })
.exec();
return res.status(200).json({
success: true,
total_data: total,
// total_pages: Math.ceil(total / PAGE_SIZE),
total_pages: Math.ceil(total / PAGE_SIZE),
products,
});
} catch (error) {