diff --git a/resources/KYC/KycController.js b/resources/KYC/KycController.js index 0319510..adc5b06 100644 --- a/resources/KYC/KycController.js +++ b/resources/KYC/KycController.js @@ -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 { diff --git a/resources/KYC/KycRoutes.js b/resources/KYC/KycRoutes.js index 22e5337..e74fd4e 100644 --- a/resources/KYC/KycRoutes.js +++ b/resources/KYC/KycRoutes.js @@ -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( diff --git a/resources/Task/TaskController.js b/resources/Task/TaskController.js index ab57e8e..f495a35 100644 --- a/resources/Task/TaskController.js +++ b/resources/Task/TaskController.js @@ -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.",