task push notification

This commit is contained in:
Sibunnayak 2024-09-03 09:27:29 +05:30
parent 26d04fb87b
commit 2d8642e991
2 changed files with 45 additions and 40 deletions

View File

@ -5,7 +5,7 @@ import { sendPushNotification } from "../../Utils/sendPushNotification.js";
import SalesCoOrdinator from "../SalesCoOrdinators/SalesCoOrdinatorModel.js"; import SalesCoOrdinator from "../SalesCoOrdinators/SalesCoOrdinatorModel.js";
import TerritoryManager from "../TerritoryManagers/TerritoryManagerModel.js"; import TerritoryManager from "../TerritoryManagers/TerritoryManagerModel.js";
// Function to update task statuses // Function to update task statuses
const updateOverdueTasks = async () => { export const updateOverdueTasks = async () => {
try { try {
const currentDate = new Date(); const currentDate = new Date();
const currentDateOnly = new Date(currentDate.setHours(0, 0, 0, 0)); const currentDateOnly = new Date(currentDate.setHours(0, 0, 0, 0));
@ -21,16 +21,16 @@ 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;
if (fcmToken) { // if (fcmToken) {
// Send push notification // // Send push notification
const message = `Your task "${task.task}" is Pending.`; // const message = `Your task "${task.task}" is Pending.`;
await sendPushNotification(fcmToken, "Task Status Updated", message); // await sendPushNotification(fcmToken, "Task Status Updated", message);
} // }
} // }
} }
console.log('Overdue tasks updated to "Pending".'); console.log('Overdue tasks updated to "Pending".');
@ -40,9 +40,9 @@ const updateOverdueTasks = async () => {
}; };
// Schedule the cron job to run daily at midnight // Schedule the cron job to run daily at midnight
cron.schedule("10 1 * * *", updateOverdueTasks, { // cron.schedule("5 9 * * *", updateOverdueTasks, {
timezone: "Asia/Kolkata", // timezone: "Asia/Kolkata",
}); // });
// cron.schedule("30 9 * * *", updateOverdueTasks); // cron.schedule("30 9 * * *", updateOverdueTasks);
@ -107,22 +107,22 @@ export const assignTask = async (req, res) => {
tradename, tradename,
}); });
// Fetch the FCM token of the assigned sales coordinator // Fetch the FCM token of the assigned sales coordinator
const salesCoordinator = await SalesCoOrdinator.findById(taskAssignedTo); // const salesCoordinator = await SalesCoOrdinator.findById(taskAssignedTo);
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.",
}); // });
} // }
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,
message: "Task assigned successfully", message: "Task assigned successfully",
@ -263,22 +263,22 @@ export const updateTaskStatus = async (req, res) => {
} }
// 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.",

View File

@ -3,7 +3,8 @@ dotenv.config();
import app from "./app.js"; import app from "./app.js";
import connectDatabase from "./database/db.js"; import connectDatabase from "./database/db.js";
import cloudinary from "cloudinary"; import cloudinary from "cloudinary";
import cron from "node-cron";
import {updateOverdueTasks} from "./resources/Task/TaskController.js ";
// Connecting to database // Connecting to database
connectDatabase(); connectDatabase();
@ -28,6 +29,10 @@ app.get("/", (req, res) => {
res.send("API is running.."); res.send("API is running..");
}); });
// } // }
// Schedule the cron job
cron.schedule("30 9 * * *", updateOverdueTasks, {
timezone: "Asia/Kolkata",
});
//<---------deployement-------------> //<---------deployement------------->
const server = app.listen(process.env.PORT, () => { const server = app.listen(process.env.PORT, () => {
console.log(`Server is working on http://localhost:${process.env.PORT}`); console.log(`Server is working on http://localhost:${process.env.PORT}`);