task push notification

This commit is contained in:
Sibunnayak 2024-09-02 16:09:53 +05:30
parent 528098d72c
commit 26d04fb87b
3 changed files with 95 additions and 1 deletions

View File

@ -165,6 +165,40 @@ export const getAllKycApproved = async (req, res) => {
res.status(500).json({ message: "Server Error", error });
}
};
export const getAllKycApprovedbytmid = async (req, res) => {
try {
const { id } = req.params; // Extracting `addedBy` ID from req.params
const { tradename, page = 1, show = 10 } = req.query; // Extracting filters and pagination from req.query
const query = { status: "approved", addedBy: id }; // Base query with status and addedBy
if (tradename) {
query.trade_name = new RegExp(tradename, "i"); // Adding trade_name filter with case-insensitive regex
}
const skip = (page - 1) * show; // Calculating the number of documents to skip
// Fetch KYC documents from the database based on query, sorted by creation date, with pagination
const retaildistributor = await KYC.find(query)
.sort({ createdAt: -1 })
.populate("principal_distributer", "name")
.populate("addedBy")
.skip(skip)
.limit(parseInt(show));
const total_data = await KYC.countDocuments(query);
// Send the fetched data as a response
res.status(200).json({
success: true,
total_data: total_data,
total_pages: Math.ceil(total_data / page),
retaildistributor
});
} catch (error) {
// Handle any errors that occur during the fetch operation
console.error(error);
res.status(500).json({ message: "Server Error", error });
}
};
// Get Single KYC
export const getKycById = async (req, res) => {
try {

View File

@ -6,6 +6,7 @@ import {
createKyc,
getAllKyc,
getAllKycApproved,
getAllKycApprovedbytmid,
getAllKycRejected,
getAllPrincipalDistributers,
getKycById,
@ -30,6 +31,9 @@ router
router
.route("/kyc/getAllapproved/")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApproved);
router
.route("/kyc/getAllapprovedbytmid/:id")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApprovedbytmid);
router
.route("/kyc/get-single-kyc/:id")
.get(

View File

@ -1,6 +1,9 @@
import Task from "./TaskModel.js";
import crypto from "crypto";
import cron from "node-cron";
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 () => {
try {
@ -17,6 +20,17 @@ const updateOverdueTasks = async () => {
for (const task of overdueTasks) {
task.taskStatus = "Pending";
await task.save();
// Fetch the Sales Coordinator who is assigned the task
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);
}
}
}
console.log('Overdue tasks updated to "Pending".');
@ -26,7 +40,7 @@ const updateOverdueTasks = async () => {
};
// Schedule the cron job to run daily at midnight
cron.schedule("10 0 * * *", updateOverdueTasks, {
cron.schedule("10 1 * * *", updateOverdueTasks, {
timezone: "Asia/Kolkata",
});
@ -92,7 +106,23 @@ export const assignTask = async (req, res) => {
addedForId,
tradename,
});
// Fetch the FCM token of the assigned sales coordinator
const salesCoordinator = await SalesCoOrdinator.findById(taskAssignedTo);
if (!salesCoordinator) {
return res.status(404).json({
success: false,
message: "Sales Coordinator not found.",
});
}
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);
}
res.status(201).json({
success: true,
message: "Task assigned successfully",
@ -222,7 +252,33 @@ export const updateTaskStatus = async (req, res) => {
// Update the task status to "Completed"
task.taskStatus = "Completed";
await task.save();
// Fetch the Sales Coordinator who completed the task
const salesCoordinator = await SalesCoOrdinator.findById(req.user._id);
if (!salesCoordinator) {
return res.status(404).json({
success: false,
message: "Sales Coordinator not found.",
});
}
// Fetch the FCM token of the Territory Manager
const territoryManager = await TerritoryManager.findById(task.taskAssignedBy);
if (!territoryManager) {
return res.status(404).json({
success: false,
message: "Territory Manager not found.",
});
}
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);
}
res.status(200).json({
success: true,
message: "Task status updated to Completed.",