update
This commit is contained in:
parent
9a4e7912b8
commit
d4ad02de6a
@ -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' });
|
||||
}
|
||||
};
|
@ -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;
|
||||
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user