date wise filter fixed

This commit is contained in:
Sibunnayak 2024-11-15 10:49:00 +05:30
parent 6ab315c247
commit 45ef34c3de

View File

@ -446,46 +446,35 @@ export const getTasks = async (req, res) => {
SCname = "",
status = "",
} = req.query;
const limit = parseInt(show);
const skip = (parseInt(page) - 1) * limit;
const matchStage = {};
const start = new Date(startDate);
const end = new Date(endDate);
// Filter by date range
if (startDate && endDate) {
const start = new Date(startDate);
const end = new Date(endDate);
if (start.toDateString() === end.toDateString()) {
matchStage.createdAt = {
$gte: start,
$lt: new Date(start).setDate(start.getDate() + 1),
};
} else {
matchStage.createdAt = {
$gte: start,
$lte: new Date(end).setDate(end.getDate() + 1),
};
}
// matchStage.createdAt = {
// $gte: start,
// $lte: new Date(end.setDate(end.getDate() + 1)),
// };
} else if (startDate && endDate === "") {
// Include data from start date to end date (inclusive until 23:59:59.999)
matchStage.createdAt = {
$gte: new Date(startDate),
$lte: new Date(),
$gte: start,
$lte: new Date(end.setHours(23, 59, 59, 999)),
};
} else if (endDate && startDate === "") {
} else if (startDate && !endDate) {
// Include data from start date up to the current date
const end = new Date();
matchStage.createdAt = {
$lte: new Date(endDate),
$gte: start,
$lte: new Date(end.setHours(23, 59, 59, 999)),
};
} else if (endDate && !startDate) {
// Include data from the beginning up to and including the end date
matchStage.createdAt = {
$lte: new Date(end.setHours(23, 59, 59, 999)),
};
}
// console.log(matchStage);
// Filter by task status
if (status) {
matchStage.taskStatus = status;
}
if (status) matchStage.taskStatus = status;
// Aggregation pipeline
const tasks = await Task.aggregate([
@ -498,6 +487,9 @@ export const getTasks = async (req, res) => {
as: "taskAssignedBy",
},
},
{
$unwind: { path: "$taskAssignedBy", preserveNullAndEmptyArrays: true },
},
{
$lookup: {
from: "salescoordinators",
@ -506,6 +498,9 @@ export const getTasks = async (req, res) => {
as: "taskAssignedTo",
},
},
{
$unwind: { path: "$taskAssignedTo", preserveNullAndEmptyArrays: true },
},
{
$match: {
...(TMname && {
@ -516,8 +511,6 @@ export const getTasks = async (req, res) => {
}),
},
},
{ $unwind: "$taskAssignedBy" },
{ $unwind: "$taskAssignedTo" },
{ $sort: { createdAt: -1 } },
{ $skip: skip },
{ $limit: limit },
@ -557,4 +550,4 @@ export const getTasks = async (req, res) => {
error: error.message,
});
}
};
};