This commit is contained in:
ROSHAN GARG 2024-09-06 10:50:21 +05:30
commit c8ef641661
10 changed files with 96 additions and 2 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -11,7 +11,7 @@ export const addInventory = async (req, res) => {
const { products, addedFor, addedForId } = req.body; const { products, addedFor, addedForId } = req.body;
const userId = req.user._id; const userId = req.user._id;
const userType = req.userType; const userType = req.userType;
console.log("req.user", req.user); // console.log("req.user", req.user);
const currentYear = new Date().getFullYear().toString().slice(-2); const currentYear = new Date().getFullYear().toString().slice(-2);
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase(); const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
const uniqueId = `${currentYear}-${randomChars}`; const uniqueId = `${currentYear}-${randomChars}`;

View File

@ -165,6 +165,40 @@ export const getAllKycApproved = async (req, res) => {
res.status(500).json({ message: "Server Error", error }); 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 // Get Single KYC
export const getKycById = async (req, res) => { export const getKycById = async (req, res) => {
try { try {

View File

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

View File

@ -1,6 +1,9 @@
import Task from "./TaskModel.js"; import Task from "./TaskModel.js";
import crypto from "crypto"; import crypto from "crypto";
import cron from "node-cron"; 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 // Function to update task statuses
const updateOverdueTasks = async () => { const updateOverdueTasks = async () => {
try { try {
@ -17,6 +20,17 @@ const updateOverdueTasks = async () => {
for (const task of overdueTasks) { for (const task of overdueTasks) {
task.taskStatus = "Pending"; task.taskStatus = "Pending";
await task.save(); 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".'); console.log('Overdue tasks updated to "Pending".');
@ -26,7 +40,7 @@ const updateOverdueTasks = async () => {
}; };
// Schedule the cron job to run daily at midnight // Schedule the cron job to run daily at midnight
cron.schedule("10 0 * * *", updateOverdueTasks, { cron.schedule("10 1 * * *", updateOverdueTasks, {
timezone: "Asia/Kolkata", timezone: "Asia/Kolkata",
}); });
@ -92,7 +106,23 @@ export const assignTask = async (req, res) => {
addedForId, addedForId,
tradename, 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({ res.status(201).json({
success: true, success: true,
message: "Task assigned successfully", message: "Task assigned successfully",
@ -222,7 +252,33 @@ export const updateTaskStatus = async (req, res) => {
// Update the task status to "Completed" // Update the task status to "Completed"
task.taskStatus = "Completed"; task.taskStatus = "Completed";
await task.save(); 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({ res.status(200).json({
success: true, success: true,
message: "Task status updated to Completed.", message: "Task status updated to Completed.",