This commit is contained in:
Sibunnayak 2024-10-18 12:52:06 +05:30
commit 136daeab74
2 changed files with 44 additions and 2 deletions

View File

@ -1636,3 +1636,42 @@ export const gettotalorderandvalueofrd = async (req, res) => {
res.status(500).json({ message: "Server error", error }); res.status(500).json({ message: "Server error", error });
} }
}; };
export const getOrderCounts = async (req, res) => {
try {
// console.log(req.user._id,"");
const userId = req.user._id;
const statusCounts = await RdOrder.aggregate([
{
$match: {
addedBy: userId, // Only match orders added by the current user
},
},
{
$group: {
_id: "$status", // Group by status
count: { $sum: 1 }, // Count the number of orders per status
},
},
]);
// Restructure the result to make it easier to consume
const result = {
new: 0,
dispatched: 0,
cancelled: 0,
processing: 0,
delivered: 0,
};
statusCounts.forEach((status) => {
result[status._id] = status.count;
});
res.status(200).json({ counts: result });
} catch (error) {
console.log(error.message);
res
.status(500)
.json({ message: error?.message || "Something went wrong!" });
}
};

View File

@ -8,6 +8,7 @@ import {
getDispatchedInvoicesForPd, getDispatchedInvoicesForPd,
getInvoiceDetailsByIdForPD, getInvoiceDetailsByIdForPD,
getNewOrders, getNewOrders,
getOrderCounts,
getPendignOrders, getPendignOrders,
getPlacedOrderById, getPlacedOrderById,
getPlacedOrdersForPD, getPlacedOrdersForPD,
@ -84,4 +85,6 @@ router.route("/pd-invoice/delivered/:invoiceId").put(
router router
.route("/single-rd-ordercount/:distributorId") .route("/single-rd-ordercount/:distributorId")
.get(isAuthenticatedUser, authorizeRoles("admin"), gettotalorderandvalueofrd); .get(isAuthenticatedUser, authorizeRoles("admin"), gettotalorderandvalueofrd);
router.route("/get-counts-rdOrders").get(isAuthenticatedRD, getOrderCounts);
export default router; export default router;