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 = "", SCname = "",
status = "", status = "",
} = req.query; } = req.query;
const limit = parseInt(show); const limit = parseInt(show);
const skip = (parseInt(page) - 1) * limit; const skip = (parseInt(page) - 1) * limit;
const matchStage = {}; const matchStage = {};
const start = new Date(startDate);
const end = new Date(endDate);
// Filter by date range // Filter by date range
if (startDate && endDate) { if (startDate && endDate) {
const start = new Date(startDate); // Include data from start date to end date (inclusive until 23:59:59.999)
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 === "") {
matchStage.createdAt = { matchStage.createdAt = {
$gte: new Date(startDate), $gte: start,
$lte: new Date(), $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 = { 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 // Filter by task status
if (status) { if (status) matchStage.taskStatus = status;
matchStage.taskStatus = status;
}
// Aggregation pipeline // Aggregation pipeline
const tasks = await Task.aggregate([ const tasks = await Task.aggregate([
@ -498,6 +487,9 @@ export const getTasks = async (req, res) => {
as: "taskAssignedBy", as: "taskAssignedBy",
}, },
}, },
{
$unwind: { path: "$taskAssignedBy", preserveNullAndEmptyArrays: true },
},
{ {
$lookup: { $lookup: {
from: "salescoordinators", from: "salescoordinators",
@ -506,6 +498,9 @@ export const getTasks = async (req, res) => {
as: "taskAssignedTo", as: "taskAssignedTo",
}, },
}, },
{
$unwind: { path: "$taskAssignedTo", preserveNullAndEmptyArrays: true },
},
{ {
$match: { $match: {
...(TMname && { ...(TMname && {
@ -516,8 +511,6 @@ export const getTasks = async (req, res) => {
}), }),
}, },
}, },
{ $unwind: "$taskAssignedBy" },
{ $unwind: "$taskAssignedTo" },
{ $sort: { createdAt: -1 } }, { $sort: { createdAt: -1 } },
{ $skip: skip }, { $skip: skip },
{ $limit: limit }, { $limit: limit },