get all rd with total orders
This commit is contained in:
parent
16dcfae79e
commit
99e80b1ff5
@ -6,6 +6,7 @@ import {
|
|||||||
getAllRDbyscid,
|
getAllRDbyscid,
|
||||||
getAllRDbytmid,
|
getAllRDbytmid,
|
||||||
getAllRetailDistributorApproved,
|
getAllRetailDistributorApproved,
|
||||||
|
getAllRetailDistributorwithTotalorder,
|
||||||
getmyProfileRD,
|
getmyProfileRD,
|
||||||
getRDId,
|
getRDId,
|
||||||
loginRD,
|
loginRD,
|
||||||
@ -36,6 +37,13 @@ router
|
|||||||
authorizeRoles("admin"),
|
authorizeRoles("admin"),
|
||||||
getAllRetailDistributorApproved
|
getAllRetailDistributorApproved
|
||||||
);
|
);
|
||||||
|
router
|
||||||
|
.route("/getAllRDandorder")
|
||||||
|
.get(
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
getAllRetailDistributorwithTotalorder
|
||||||
|
);
|
||||||
router
|
router
|
||||||
.route("/getRD/:id")
|
.route("/getRD/:id")
|
||||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getRDId);
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getRDId);
|
||||||
|
@ -3,7 +3,7 @@ import RetailDistributor from "./RetailDistributorModel.js";
|
|||||||
import validator from "validator";
|
import validator from "validator";
|
||||||
import password from "secure-random-password";
|
import password from "secure-random-password";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
import { RdOrder } from "../RD_Ordes/rdOrderModal.js";
|
||||||
import sendEmail, { sendOtp } from "../../Utils/sendEmail.js";
|
import sendEmail, { sendOtp } from "../../Utils/sendEmail.js";
|
||||||
export const loginRD = async (req, res) => {
|
export const loginRD = async (req, res) => {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
@ -425,6 +425,151 @@ export const getAllRetailDistributorApproved = async (req, res) => {
|
|||||||
res.status(500).json({ message: "Server Error", error });
|
res.status(500).json({ message: "Server Error", error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const getAllRetailDistributorwithTotalorder = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const {
|
||||||
|
page = 1,
|
||||||
|
show = 10,
|
||||||
|
tradename,
|
||||||
|
name,
|
||||||
|
mobile_number,
|
||||||
|
principaldistributor,
|
||||||
|
} = req.query;
|
||||||
|
const skip = (page - 1) * show;
|
||||||
|
|
||||||
|
// Build the aggregation pipeline
|
||||||
|
let pipeline = [
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "kycs", // KYC collection
|
||||||
|
localField: "kyc",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "kycDetails",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $unwind: { path: "$kycDetails", preserveNullAndEmptyArrays: true } },
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "users", // Principal Distributor collection
|
||||||
|
localField: "principal_distributer",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "principalDetails",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: {
|
||||||
|
path: "$principalDetails",
|
||||||
|
preserveNullAndEmptyArrays: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "territorymanagers", // Territory Manager collection
|
||||||
|
localField: "mappedTM",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "mappedTMDetails",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: { path: "$mappedTMDetails", preserveNullAndEmptyArrays: true },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "salescoordinators", // Sales Coordinator collection
|
||||||
|
localField: "mappedSC",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "mappedSCDetails",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: { path: "$mappedSCDetails", preserveNullAndEmptyArrays: true },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// Add filters based on query parameters
|
||||||
|
const matchConditions = {};
|
||||||
|
if (tradename)
|
||||||
|
matchConditions["kycDetails.trade_name"] = new RegExp(tradename, "i");
|
||||||
|
if (name) matchConditions["name"] = new RegExp(name, "i");
|
||||||
|
if (mobile_number)
|
||||||
|
matchConditions["mobile_number"] = new RegExp(mobile_number, "i");
|
||||||
|
if (principaldistributor)
|
||||||
|
matchConditions["principalDetails.name"] = new RegExp(
|
||||||
|
principaldistributor,
|
||||||
|
"i"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (Object.keys(matchConditions).length) {
|
||||||
|
pipeline.push({ $match: matchConditions });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Project required fields early
|
||||||
|
pipeline.push({
|
||||||
|
$project: {
|
||||||
|
_id: 1,
|
||||||
|
uniqueId: 1,
|
||||||
|
name: 1,
|
||||||
|
mobile_number: 1,
|
||||||
|
email: 1,
|
||||||
|
"kycDetails.trade_name": 1,
|
||||||
|
"principalDetails.name": 1,
|
||||||
|
"mappedTMDetails.name": 1,
|
||||||
|
"mappedSCDetails.name": 1,
|
||||||
|
createdAt: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Pagination and sorting
|
||||||
|
pipeline.push({ $sort: { createdAt: -1 } });
|
||||||
|
pipeline.push({ $skip: skip });
|
||||||
|
pipeline.push({ $limit: parseInt(show) });
|
||||||
|
|
||||||
|
// Execute the main aggregation pipeline
|
||||||
|
const Retaildistributor = await RetailDistributor.aggregate(pipeline);
|
||||||
|
|
||||||
|
// Aggregate orders data for each distributor
|
||||||
|
const orderStats = await RdOrder.aggregate([
|
||||||
|
{
|
||||||
|
$match: { addedBy: { $in: Retaildistributor.map((user) => user._id) } },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: "$addedBy", // Group by distributor ID
|
||||||
|
totalOrders: { $sum: 1 }, // Count total orders
|
||||||
|
lastOrderDate: { $max: "$createdAt" }, // Get last order date
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Combine order stats with Retaildistributor data
|
||||||
|
const usersWithOrderStats = Retaildistributor.map((user) => {
|
||||||
|
const orderData = orderStats.find(
|
||||||
|
(order) => order._id.toString() === user._id.toString()
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
...user,
|
||||||
|
totalOrders: orderData ? orderData.totalOrders : 0,
|
||||||
|
lastOrderDate: orderData ? orderData.lastOrderDate : null,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get total count of documents matching the query
|
||||||
|
const countPipeline = [{ $match: matchConditions }, { $count: "total" }];
|
||||||
|
const total_data = await RetailDistributor.aggregate(countPipeline);
|
||||||
|
const totalCount = total_data[0]?.total || 0;
|
||||||
|
|
||||||
|
// Send the response
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
total_data: totalCount,
|
||||||
|
total_pages: Math.ceil(totalCount / show),
|
||||||
|
Retaildistributor: usersWithOrderStats,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({ message: "Server Error", error });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
//get RD by Id
|
//get RD by Id
|
||||||
export const getRDId = async (req, res) => {
|
export const getRDId = async (req, res) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user