From d4ad02de6a85b142d121b5677a4440b7f18eb2f5 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Fri, 18 Oct 2024 11:21:20 +0530 Subject: [PATCH] update --- resources/Task/TaskController.js | 68 ++++++++++++++++++++++++++++++++ resources/Task/TaskRoute.js | 10 ++++- resources/user/userModel.js | 1 - 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/resources/Task/TaskController.js b/resources/Task/TaskController.js index 21f10af..a2c6f89 100644 --- a/resources/Task/TaskController.js +++ b/resources/Task/TaskController.js @@ -4,6 +4,8 @@ import cron from "node-cron"; import { sendPushNotification } from "../../Utils/sendPushNotification.js"; import SalesCoOrdinator from "../SalesCoOrdinators/SalesCoOrdinatorModel.js"; import TerritoryManager from "../TerritoryManagers/TerritoryManagerModel.js"; +import User from "../user/userModel.js"; +import RetailDistributor from "../RetailDistributor/RetailDistributorModel.js"; // Function to update task statuses export const updateOverdueTasks = async () => { try { @@ -362,3 +364,69 @@ export const updateTaskStatus = async (req, res) => { }); } }; + +// Controller to get today's tasks with pagination and conditional population +export const getTodaysTasks = async (req, res) => { + try { + // Get the current page and items per page (defaults to page 1, 10 items per page) + const currentPage = parseInt(req.query.page) || 1; + const itemsPerPage = parseInt(req.query.show) || 10; + + // Get today's date at midnight + const startOfToday = new Date(); + startOfToday.setHours(0, 0, 0, 0); // Set time to 00:00:00 + + // Get the end of today at 23:59:59 + const endOfToday = new Date(); + endOfToday.setHours(23, 59, 59, 999); // Set time to 23:59:59 + + // Calculate the number of items to skip + const skip = (currentPage - 1) * itemsPerPage; + + // Find tasks that are due today, with pagination + let tasksQuery = Task.find({ + taskDueDate: { + $gte: startOfToday, + $lte: endOfToday, + }, + }) + .populate('taskAssignedTo') // Optional: populate assigned coordinator details + .populate('taskAssignedBy') // Optional: populate assigned manager details + .skip(skip) // Skip documents for pagination + .limit(itemsPerPage); // Limit the number of documents + + // Modify the population based on the `addedFor` field value + tasksQuery = tasksQuery.populate({ + path: 'addedForId', + model: function (doc) { + return doc.addedFor === 'PrincipalDistributor' ? 'User' : 'RetailDistributor'; + }, + }); + + // Execute the query + const tasks = await tasksQuery.exec(); + + // Count the total number of tasks for pagination metadata + const totalTasks = await Task.countDocuments({ + taskDueDate: { + $gte: startOfToday, + $lte: endOfToday, + }, + }); + + // Calculate total pages + const totalPages = Math.ceil(totalTasks / itemsPerPage); + + // Send paginated tasks in response + res.status(200).json({ + tasks, // Paginated tasks + currentPage, // Current page number + itemsPerPage, // Number of tasks per page + totalTasks, // Total number of tasks + totalPages, // Total number of pages + }); + } catch (error) { + console.error('Error fetching today\'s tasks with pagination:', error); + res.status(500).json({ message: 'Failed to retrieve tasks for today' }); + } +}; \ No newline at end of file diff --git a/resources/Task/TaskRoute.js b/resources/Task/TaskRoute.js index d654595..4bc12ab 100644 --- a/resources/Task/TaskRoute.js +++ b/resources/Task/TaskRoute.js @@ -6,11 +6,12 @@ import { getTasksbytask, getAllTasksByStatus, getTasksByDates, + getTodaysTasks, } from "./TaskController.js"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; import { isAuthenticated_SC_TM } from "../../middlewares/generalAuth.js"; - +import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; const router = express.Router(); // Route for Territory Manager to assign a task @@ -31,5 +32,10 @@ router.put( isAuthenticatedSalesCoOrdinator, updateTaskStatus ); - +router.get( + "/today", + isAuthenticatedUser, + authorizeRoles("admin"), + getTodaysTasks +); export default router; diff --git a/resources/user/userModel.js b/resources/user/userModel.js index e19c597..d9ce78c 100644 --- a/resources/user/userModel.js +++ b/resources/user/userModel.js @@ -43,7 +43,6 @@ const userSchema = new mongoose.Schema( email: { type: String, required: [true, "Please Enter Your Email"], - unique: true, validate: [validator.isEmail, "Please Enter a valid Email"], }, phone: {