Merge branch 'main' of https://git.cnapp.co.in/gitadmin/api
This commit is contained in:
commit
d15bddf3e1
@ -21,7 +21,9 @@ export const updateOverdueTasks = async () => {
|
|||||||
task.taskStatus = "Pending";
|
task.taskStatus = "Pending";
|
||||||
await task.save();
|
await task.save();
|
||||||
// Fetch the Sales Coordinator who is assigned the task
|
// 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) {
|
if (salesCoordinator) {
|
||||||
const fcmToken = salesCoordinator.fcm_token;
|
const fcmToken = salesCoordinator.fcm_token;
|
||||||
@ -119,9 +121,9 @@ export const assignTask = async (req, res) => {
|
|||||||
const fcmToken = salesCoordinator.fcm_token;
|
const fcmToken = salesCoordinator.fcm_token;
|
||||||
|
|
||||||
if (fcmToken) {
|
if (fcmToken) {
|
||||||
// Send push notification
|
// Send push notification
|
||||||
const message = `You have been assigned a new task: ${task}`;
|
const message = `You have been assigned a new task: ${task}`;
|
||||||
await sendPushNotification(fcmToken, "New Task Assigned", message);
|
await sendPushNotification(fcmToken, "New Task Assigned", message);
|
||||||
}
|
}
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
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) => {
|
export const getAllTasksByStatus = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await updateOverdueTasks();
|
await updateOverdueTasks();
|
||||||
@ -255,33 +305,35 @@ export const updateTaskStatus = async (req, res) => {
|
|||||||
// Update the task status to "Completed"
|
// Update the task status to "Completed"
|
||||||
task.taskStatus = "Completed";
|
task.taskStatus = "Completed";
|
||||||
await task.save();
|
await task.save();
|
||||||
// Fetch the Sales Coordinator who completed the task
|
// Fetch the Sales Coordinator who completed the task
|
||||||
const salesCoordinator = await SalesCoOrdinator.findById(req.user._id);
|
const salesCoordinator = await SalesCoOrdinator.findById(req.user._id);
|
||||||
|
|
||||||
if (!salesCoordinator) {
|
if (!salesCoordinator) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: "Sales Coordinator not found.",
|
message: "Sales Coordinator not found.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the FCM token of the Territory Manager
|
// Fetch the FCM token of the Territory Manager
|
||||||
const territoryManager = await TerritoryManager.findById(task.taskAssignedBy);
|
const territoryManager = await TerritoryManager.findById(
|
||||||
|
task.taskAssignedBy
|
||||||
|
);
|
||||||
|
|
||||||
if (!territoryManager) {
|
if (!territoryManager) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: "Territory Manager not found.",
|
message: "Territory Manager not found.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const fcmToken = territoryManager.fcm_token;
|
const fcmToken = territoryManager.fcm_token;
|
||||||
|
|
||||||
if (fcmToken) {
|
if (fcmToken) {
|
||||||
// Send push notification
|
// Send push notification
|
||||||
const message = `Task "${task.task}" has been completed by ${salesCoordinator.name}.`;
|
const message = `Task "${task.task}" has been completed by ${salesCoordinator.name}.`;
|
||||||
await sendPushNotification(fcmToken, "Task Completed", message);
|
await sendPushNotification(fcmToken, "Task Completed", message);
|
||||||
}
|
}
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: "Task status updated to Completed.",
|
message: "Task status updated to Completed.",
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
updateTaskStatus,
|
updateTaskStatus,
|
||||||
getTasksbytask,
|
getTasksbytask,
|
||||||
getAllTasksByStatus,
|
getAllTasksByStatus,
|
||||||
|
getTasksByDates,
|
||||||
} from "./TaskController.js";
|
} from "./TaskController.js";
|
||||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||||
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
||||||
@ -29,6 +30,11 @@ router.get(
|
|||||||
isAuthenticatedTerritoryManager,
|
isAuthenticatedTerritoryManager,
|
||||||
getAllTasksByStatus
|
getAllTasksByStatus
|
||||||
);
|
);
|
||||||
|
router.get(
|
||||||
|
"/alltask",
|
||||||
|
isAuthenticatedTerritoryManager,
|
||||||
|
getTasksByDates
|
||||||
|
);
|
||||||
router.get(
|
router.get(
|
||||||
"/task/type/:task",
|
"/task/type/:task",
|
||||||
isAuthenticatedSalesCoOrdinator,
|
isAuthenticatedSalesCoOrdinator,
|
||||||
|
Loading…
Reference in New Issue
Block a user