From d5cc3b339de937a17cc543ca0b38de047937ab1a Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Wed, 9 Oct 2024 13:12:40 +0530 Subject: [PATCH] get all task for TM with datewise sesrch functionality --- resources/Task/TaskController.js | 104 +++++++++++++++++++++++-------- resources/Task/TaskRoute.js | 6 ++ 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/resources/Task/TaskController.js b/resources/Task/TaskController.js index 9495dc8..1221b8a 100644 --- a/resources/Task/TaskController.js +++ b/resources/Task/TaskController.js @@ -21,7 +21,9 @@ export const updateOverdueTasks = async () => { task.taskStatus = "Pending"; await task.save(); // Fetch the Sales Coordinator who is assigned the task - const salesCoordinator = await SalesCoOrdinator.findById(task.taskAssignedTo); + const salesCoordinator = await SalesCoOrdinator.findById( + task.taskAssignedTo + ); if (salesCoordinator) { const fcmToken = salesCoordinator.fcm_token; @@ -119,9 +121,9 @@ export const assignTask = async (req, res) => { const fcmToken = salesCoordinator.fcm_token; if (fcmToken) { - // Send push notification - const message = `You have been assigned a new task: ${task}`; - await sendPushNotification(fcmToken, "New Task Assigned", message); + // Send push notification + const message = `You have been assigned a new task: ${task}`; + await sendPushNotification(fcmToken, "New Task Assigned", message); } res.status(201).json({ success: true, @@ -166,6 +168,54 @@ export const getTasksByStatus = async (req, res) => { }); } }; +const getStartAndEndOfDay = (date) => { + const startOfDay = new Date(date); + startOfDay.setUTCHours(0, 0, 0, 0); // Start of the day UTC + + const endOfDay = new Date(date); + endOfDay.setUTCHours(23, 59, 59, 999); // End of the day UTC + + return { startOfDay, endOfDay }; +}; + +export const getTasksByDates = async (req, res) => { + try { + await updateOverdueTasks(); + + // Get the date from the query + const { Date: queryDate } = req.query; + let taskDate; + // If date is provided in query, parse it, otherwise use today's date + if (queryDate) { + taskDate = parseDate(queryDate); + } else { + // Get today's date in UTC + taskDate = new Date(); + } + // Get the start and end of the day in UTC + const { startOfDay, endOfDay } = getStartAndEndOfDay(taskDate); + // Find tasks for the user, filtered by createdAt within the start and end of the day + const tasks = await Task.find({ + taskAssignedBy: req.user._id, + createdAt: { $gte: startOfDay, $lte: endOfDay }, + }) + .populate({ + path: "taskAssignedTo", + select: "name mobileNumber email", + }) + .sort({ createdAt: -1 }); + + res.status(200).json({ + success: true, + tasks, + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message, + }); + } +}; export const getAllTasksByStatus = async (req, res) => { try { await updateOverdueTasks(); @@ -255,33 +305,35 @@ export const updateTaskStatus = async (req, res) => { // Update the task status to "Completed" task.taskStatus = "Completed"; await task.save(); - // Fetch the Sales Coordinator who completed the task - const salesCoordinator = await SalesCoOrdinator.findById(req.user._id); + // Fetch the Sales Coordinator who completed the task + const salesCoordinator = await SalesCoOrdinator.findById(req.user._id); - if (!salesCoordinator) { - return res.status(404).json({ - success: false, - message: "Sales Coordinator not found.", - }); - } + if (!salesCoordinator) { + return res.status(404).json({ + success: false, + message: "Sales Coordinator not found.", + }); + } - // Fetch the FCM token of the Territory Manager - const territoryManager = await TerritoryManager.findById(task.taskAssignedBy); + // Fetch the FCM token of the Territory Manager + const territoryManager = await TerritoryManager.findById( + task.taskAssignedBy + ); - if (!territoryManager) { - return res.status(404).json({ - success: false, - message: "Territory Manager not found.", - }); - } + if (!territoryManager) { + return res.status(404).json({ + success: false, + message: "Territory Manager not found.", + }); + } - const fcmToken = territoryManager.fcm_token; + const fcmToken = territoryManager.fcm_token; - if (fcmToken) { - // Send push notification - const message = `Task "${task.task}" has been completed by ${salesCoordinator.name}.`; - await sendPushNotification(fcmToken, "Task Completed", message); - } + if (fcmToken) { + // Send push notification + const message = `Task "${task.task}" has been completed by ${salesCoordinator.name}.`; + await sendPushNotification(fcmToken, "Task Completed", message); + } res.status(200).json({ success: true, message: "Task status updated to Completed.", diff --git a/resources/Task/TaskRoute.js b/resources/Task/TaskRoute.js index 91cd74f..f35955d 100644 --- a/resources/Task/TaskRoute.js +++ b/resources/Task/TaskRoute.js @@ -5,6 +5,7 @@ import { updateTaskStatus, getTasksbytask, getAllTasksByStatus, + getTasksByDates, } from "./TaskController.js"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; @@ -29,6 +30,11 @@ router.get( isAuthenticatedTerritoryManager, getAllTasksByStatus ); +router.get( + "/alltask", + isAuthenticatedTerritoryManager, + getTasksByDates +); router.get( "/task/type/:task", isAuthenticatedSalesCoOrdinator,