From 2d8642e9913b2f7758114a2d76e7b480dff6fc73 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Tue, 3 Sep 2024 09:27:29 +0530 Subject: [PATCH] task push notification --- resources/Task/TaskController.js | 78 ++++++++++++++++---------------- server.js | 7 ++- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/resources/Task/TaskController.js b/resources/Task/TaskController.js index f495a35..5f6543c 100644 --- a/resources/Task/TaskController.js +++ b/resources/Task/TaskController.js @@ -5,7 +5,7 @@ import { sendPushNotification } from "../../Utils/sendPushNotification.js"; import SalesCoOrdinator from "../SalesCoOrdinators/SalesCoOrdinatorModel.js"; import TerritoryManager from "../TerritoryManagers/TerritoryManagerModel.js"; // Function to update task statuses -const updateOverdueTasks = async () => { +export const updateOverdueTasks = async () => { try { const currentDate = new Date(); const currentDateOnly = new Date(currentDate.setHours(0, 0, 0, 0)); @@ -21,16 +21,16 @@ const updateOverdueTasks = async () => { task.taskStatus = "Pending"; await task.save(); // 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) { - const fcmToken = salesCoordinator.fcm_token; - if (fcmToken) { - // Send push notification - const message = `Your task "${task.task}" is Pending.`; - await sendPushNotification(fcmToken, "Task Status Updated", message); - } - } + // if (salesCoordinator) { + // const fcmToken = salesCoordinator.fcm_token; + // if (fcmToken) { + // // Send push notification + // const message = `Your task "${task.task}" is Pending.`; + // await sendPushNotification(fcmToken, "Task Status Updated", message); + // } + // } } console.log('Overdue tasks updated to "Pending".'); @@ -40,9 +40,9 @@ const updateOverdueTasks = async () => { }; // Schedule the cron job to run daily at midnight -cron.schedule("10 1 * * *", updateOverdueTasks, { - timezone: "Asia/Kolkata", -}); +// cron.schedule("5 9 * * *", updateOverdueTasks, { +// timezone: "Asia/Kolkata", +// }); // cron.schedule("30 9 * * *", updateOverdueTasks); @@ -107,22 +107,22 @@ export const assignTask = async (req, res) => { tradename, }); // Fetch the FCM token of the assigned sales coordinator - const salesCoordinator = await SalesCoOrdinator.findById(taskAssignedTo); + // const salesCoordinator = await SalesCoOrdinator.findById(taskAssignedTo); - if (!salesCoordinator) { - return res.status(404).json({ - success: false, - message: "Sales Coordinator not found.", - }); - } + // if (!salesCoordinator) { + // return res.status(404).json({ + // success: false, + // message: "Sales Coordinator not found.", + // }); + // } - const fcmToken = salesCoordinator.fcm_token; + // const fcmToken = salesCoordinator.fcm_token; - if (fcmToken) { - // Send push notification - const message = `You have been assigned a new task: ${task}`; - await sendPushNotification(fcmToken, "New Task Assigned", message); - } + // if (fcmToken) { + // // Send push notification + // const message = `You have been assigned a new task: ${task}`; + // await sendPushNotification(fcmToken, "New Task Assigned", message); + // } res.status(201).json({ success: true, message: "Task assigned successfully", @@ -263,22 +263,22 @@ export const updateTaskStatus = async (req, res) => { } // Fetch the FCM token of the Territory Manager - const territoryManager = await TerritoryManager.findById(task.taskAssignedBy); + // const territoryManager = await TerritoryManager.findById(task.taskAssignedBy); - if (!territoryManager) { - return res.status(404).json({ - success: false, - message: "Territory Manager not found.", - }); - } + // if (!territoryManager) { + // return res.status(404).json({ + // success: false, + // message: "Territory Manager not found.", + // }); + // } - const fcmToken = territoryManager.fcm_token; + // const fcmToken = territoryManager.fcm_token; - if (fcmToken) { - // Send push notification - const message = `Task "${task.task}" has been completed by ${salesCoordinator.name}.`; - await sendPushNotification(fcmToken, "Task Completed", message); - } + // if (fcmToken) { + // // Send push notification + // const message = `Task "${task.task}" has been completed by ${salesCoordinator.name}.`; + // await sendPushNotification(fcmToken, "Task Completed", message); + // } res.status(200).json({ success: true, message: "Task status updated to Completed.", diff --git a/server.js b/server.js index 73a6b66..ca270f6 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,8 @@ dotenv.config(); import app from "./app.js"; import connectDatabase from "./database/db.js"; import cloudinary from "cloudinary"; - +import cron from "node-cron"; +import {updateOverdueTasks} from "./resources/Task/TaskController.js "; // Connecting to database connectDatabase(); @@ -28,6 +29,10 @@ app.get("/", (req, res) => { res.send("API is running.."); }); // } +// Schedule the cron job +cron.schedule("30 9 * * *", updateOverdueTasks, { + timezone: "Asia/Kolkata", +}); //<---------deployement-------------> const server = app.listen(process.env.PORT, () => { console.log(`Server is working on http://localhost:${process.env.PORT}`);