Merge branch 'main' of https://git.cnapp.co.in/gitadmin/api
This commit is contained in:
commit
7056d178f9
@ -1477,11 +1477,15 @@ export const getAllOrdersByDistributor = async (req, res) => {
|
|||||||
export const gettotalorderandvalueofpd = async (req, res) => {
|
export const gettotalorderandvalueofpd = async (req, res) => {
|
||||||
const { distributorId } = req.params;
|
const { distributorId } = req.params;
|
||||||
try {
|
try {
|
||||||
const orders = await PdOrder.find({ addedBy: distributorId });
|
const orders = await PdOrder.find({ addedBy: distributorId }).sort({ createdAt: -1 });
|
||||||
const totalOrders = orders.length;
|
|
||||||
const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0);
|
|
||||||
|
|
||||||
res.status(200).json({ totalOrders, totalValue });
|
const totalOrders = orders.length;
|
||||||
|
const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0).toFixed(2);
|
||||||
|
|
||||||
|
// Get the date of the last order
|
||||||
|
const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null;
|
||||||
|
|
||||||
|
res.status(200).json({ totalOrders, totalValue: parseFloat(totalValue), lastPurchaseOrderDate });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching orders:", error);
|
console.error("Error fetching orders:", error);
|
||||||
res.status(500).json({ message: "Server error", error });
|
res.status(500).json({ message: "Server error", error });
|
||||||
|
@ -1548,3 +1548,21 @@ export const getAllOrdersByDistributor = async (req, res) => {
|
|||||||
res.status(500).json({ message: "Server error", error });
|
res.status(500).json({ message: "Server error", error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const gettotalorderandvalueofrd = async (req, res) => {
|
||||||
|
const { distributorId } = req.params;
|
||||||
|
try {
|
||||||
|
const orders = await RdOrder.find({ addedBy: distributorId }).sort({ createdAt: -1 });
|
||||||
|
|
||||||
|
const totalOrders = orders.length;
|
||||||
|
const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0).toFixed(2);
|
||||||
|
|
||||||
|
// Get the date of the last order
|
||||||
|
const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null;
|
||||||
|
|
||||||
|
res.status(200).json({ totalOrders, totalValue: parseFloat(totalValue), lastPurchaseOrderDate });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching orders:", error);
|
||||||
|
res.status(500).json({ message: "Server error", error });
|
||||||
|
}
|
||||||
|
};
|
@ -15,12 +15,12 @@ import {
|
|||||||
getProcessingInvoicesForPd,
|
getProcessingInvoicesForPd,
|
||||||
getSinglePlacedOrderForPD,
|
getSinglePlacedOrderForPD,
|
||||||
getSinglePlacedOrderForRD,
|
getSinglePlacedOrderForRD,
|
||||||
|
gettotalorderandvalueofrd,
|
||||||
processOrder,
|
processOrder,
|
||||||
updateCourierStatusToDeliveredForPD,
|
updateCourierStatusToDeliveredForPD,
|
||||||
updateCourierStatusToDispatchedForPD,
|
updateCourierStatusToDispatchedForPD,
|
||||||
} from "./rdOrderController.js";
|
} from "./rdOrderController.js";
|
||||||
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
||||||
import { isAuthenticatedUser } from "../../middlewares/auth.js";
|
|
||||||
|
|
||||||
import { authorizeRoles } from "../../middlewares/auth.js";
|
import { authorizeRoles } from "../../middlewares/auth.js";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@ -81,5 +81,7 @@ router.route("/pd-invoice/delivered/:invoiceId").put(
|
|||||||
|
|
||||||
updateCourierStatusToDeliveredForPD
|
updateCourierStatusToDeliveredForPD
|
||||||
);
|
);
|
||||||
|
router
|
||||||
|
.route("/single-rd-ordercount/:distributorId")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), gettotalorderandvalueofrd);
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -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