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 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.",

View File

@ -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}`);