From 45ef34c3de4f271cf0af7305877f7d6fec33b86b Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Fri, 15 Nov 2024 10:49:00 +0530 Subject: [PATCH] date wise filter fixed --- resources/Task/TaskController.js | 55 ++++++++++++++------------------ 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/resources/Task/TaskController.js b/resources/Task/TaskController.js index 8d3c0e2..d13fd09 100644 --- a/resources/Task/TaskController.js +++ b/resources/Task/TaskController.js @@ -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, }); } -}; \ No newline at end of file +};