diff --git a/resources/Notification/notificationController.js b/resources/Notification/notificationController.js index 98f352a..2dcd0fb 100644 --- a/resources/Notification/notificationController.js +++ b/resources/Notification/notificationController.js @@ -1,3 +1,4 @@ +import { getStartAndEndOfDay } from "../Task/TaskController.js"; import { Notification } from "./notificationModal.js"; export const getNotification = async (req, res) => { @@ -54,3 +55,39 @@ export const getSingleNotification = async (req, res) => { .json({ return_message: "Something went wrong", error }); } }; +export const getNotificationByDates = async (req, res) => { + try { + const filter = { added_for: req.user._id }; // Default filter by user ID + + // Get the date from the query and parse it + const { Date: queryDate } = req.query; + let notificationDate = queryDate + ? new Date(queryDate.split("/").reverse().join("-")) // Convert DD/MM/YYYY to YYYY-MM-DD + : new Date(); // Default to today's date + + // Get the start and end of the day + const { startOfDay, endOfDay } = getStartAndEndOfDay(notificationDate); + + // Find notifications filtered by user and date range + const notifications = await Notification.find({ + ...filter, + createdAt: { $gte: startOfDay, $lte: endOfDay }, + }).sort({ createdAt: -1 }); + + if (notifications.length > 0) { + return res.status(200).json({ + success: true, + notifications, + }); + } + + return res + .status(404) + .json({ success: false, message: "No notifications found" }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message, + }); + } +}; diff --git a/resources/Notification/notificationRoute.js b/resources/Notification/notificationRoute.js index 1d84115..34f6d04 100644 --- a/resources/Notification/notificationRoute.js +++ b/resources/Notification/notificationRoute.js @@ -1,6 +1,9 @@ import express from "express"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; -import { getNotification } from "./notificationController.js"; +import { + getNotification, + getNotificationByDates, +} from "./notificationController.js"; import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; import { isAuthenticatedUser } from "../../middlewares/auth.js"; import { isAuthenticatedRD } from "../../middlewares/rdAuth.js"; @@ -9,7 +12,7 @@ const router = express.Router(); router .route("/get-notification-sc") - .get(isAuthenticatedSalesCoOrdinator, getNotification); + .get(isAuthenticatedSalesCoOrdinator, getNotificationByDates); router .route("/get-notification-sc/:id") @@ -17,13 +20,17 @@ router router .route("/get-notification-tm") - .get(isAuthenticatedTerritoryManager, getNotification); + .get(isAuthenticatedTerritoryManager, getNotificationByDates); router .route("/get-notification-tm/:id") .get(isAuthenticatedTerritoryManager, getNotification); -router.route("/get-notification-pd").get(isAuthenticatedUser, getNotification); -router.route("/get-notification-rd").get(isAuthenticatedRD, getNotification); +router + .route("/get-notification-pd") + .get(isAuthenticatedUser, getNotificationByDates); +router + .route("/get-notification-rd") + .get(isAuthenticatedRD, getNotificationByDates); export default router; diff --git a/resources/Task/TaskController.js b/resources/Task/TaskController.js index d67f76e..21f10af 100644 --- a/resources/Task/TaskController.js +++ b/resources/Task/TaskController.js @@ -168,7 +168,7 @@ export const getTasksByStatus = async (req, res) => { }); } }; -const getStartAndEndOfDay = (date) => { +export const getStartAndEndOfDay = (date) => { const startOfDay = new Date(date); startOfDay.setUTCHours(0, 0, 0, 0); // Start of the day UTC @@ -181,13 +181,13 @@ const getStartAndEndOfDay = (date) => { export const getTasksByDates = async (req, res) => { try { await updateOverdueTasks(); - + // Initialize filter object const filter = {}; console.log(req.userType); // Determine the filter based on user type if (req.userType === "SalesCoOrdinator") { - filter.taskAssignedTo = req.user._id; // Use `=` to assign values, and `===` for comparison + filter.taskAssignedTo = req.user._id; // Use `=` to assign values, and `===` for comparison } else { filter.taskAssignedBy = req.user._id; } @@ -209,11 +209,14 @@ export const getTasksByDates = async (req, res) => { // Find tasks for the user, filtered by createdAt within the start and end of the day const tasks = await Task.find({ - ...filter, // Use the filter object for querying + ...filter, // Use the filter object for querying createdAt: { $gte: startOfDay, $lte: endOfDay }, }) .populate({ - path: req.userType === "SalesCoOrdinator" ? "taskAssignedBy" : "taskAssignedTo", // Change path based on user type + path: + req.userType === "SalesCoOrdinator" + ? "taskAssignedBy" + : "taskAssignedTo", // Change path based on user type select: "name mobileNumber email", }) .sort({ createdAt: -1 });