get distributor in sorted format

This commit is contained in:
Sibunnayak 2024-10-10 11:53:55 +05:30
parent 00a5f90355
commit 5fe1da6c98

View File

@ -13,12 +13,14 @@ 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;
if (addedFor === "RetailDistributor") { if (addedFor === "RetailDistributor") {
// Find the RetailDistributor stock // Find the RetailDistributor stock
const rdStock = await RDStock.findOne({ userId: addedForId }); const rdStock = await RDStock.findOne({ userId: addedForId });
if (!rdStock) { if (!rdStock) {
return res.status(404).json({ error: "Stock not available for the RetailDistributor" }); return res
.status(404)
.json({ error: "Stock not available for the RetailDistributor" });
} }
// Array to hold product names that have issues // Array to hold product names that have issues
@ -29,7 +31,9 @@ export const addInventory = async (req, res) => {
const { productid, Sale, ProductName } = product; const { productid, Sale, ProductName } = product;
// Find product in the RetailDistributor's stock // Find product in the RetailDistributor's stock
const rdProduct = rdStock.products.find(p => p.productid.toString() === productid.toString()); const rdProduct = rdStock.products.find(
(p) => p.productid.toString() === productid.toString()
);
if (rdProduct) { if (rdProduct) {
// Check if sales quantity is less than or equal to the stock // Check if sales quantity is less than or equal to the stock
@ -50,7 +54,9 @@ export const addInventory = async (req, res) => {
if (invalidProducts.length > 0) { if (invalidProducts.length > 0) {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
message: `Insufficient stock or product not found for: ${invalidProducts.join(", ")}`, message: `Insufficient stock or product not found for: ${invalidProducts.join(
", "
)}`,
}); });
} }
@ -73,7 +79,6 @@ export const addInventory = async (req, res) => {
message: "Inventory added successfully", message: "Inventory added successfully",
data: newInventory, data: newInventory,
}); });
} catch (error) { } catch (error) {
res.status(500).json({ success: false, message: "Server error", error }); res.status(500).json({ success: false, message: "Server error", error });
} }
@ -88,7 +93,7 @@ export const getDistributors = async (req, res) => {
return res.status(400).json({ message: "Invalid distributor type" }); return res.status(400).json({ message: "Invalid distributor type" });
} }
let filter = { role: "principal-Distributor" }; let filter = { role: "principal-Distributor" };
let query={}; let query = {};
// Check the user type and adjust the filter accordingly // Check the user type and adjust the filter accordingly
if (req.userType === "SalesCoOrdinator") { if (req.userType === "SalesCoOrdinator") {
// If userType is "SalesCoOrdinator", filter by req.user.mappedBy // If userType is "SalesCoOrdinator", filter by req.user.mappedBy
@ -103,7 +108,9 @@ export const getDistributors = async (req, res) => {
// console.log("type",type); // console.log("type",type);
if (type === "PrincipalDistributor") { if (type === "PrincipalDistributor") {
// Fetch all PrincipalDistributors // Fetch all PrincipalDistributors
const principalDistributors = await User.find(filter); const principalDistributors = await User.find(filter).sort({
createdAt: -1,
});
// console.log("principalDistributors",principalDistributors); // console.log("principalDistributors",principalDistributors);
// Map each PrincipalDistributor to include their ShippingAddress // Map each PrincipalDistributor to include their ShippingAddress
distributors = await Promise.all( distributors = await Promise.all(
@ -119,7 +126,9 @@ export const getDistributors = async (req, res) => {
); );
} else { } else {
// For RetailDistributor, fetch approved KYC documents // For RetailDistributor, fetch approved KYC documents
distributors = await RetailDistributor.find(query).populate("kyc"); distributors = await RetailDistributor.find(query)
.populate("kyc")
.sort({ createdAt: -1 });
} }
res.status(200).json(distributors); res.status(200).json(distributors);
@ -148,12 +157,12 @@ export const getAllInventories = async (req, res) => {
$lte: new Date(end).setDate(end.getDate() + 1), $lte: new Date(end).setDate(end.getDate() + 1),
}; };
} }
} else if (startDate && endDate==='') { } else if (startDate && endDate === "") {
query.createdAt = { query.createdAt = {
$gte: new Date(startDate), $gte: new Date(startDate),
$lte: new Date(), $lte: new Date(),
}; };
} else if (endDate && startDate==='') { } else if (endDate && startDate === "") {
query.createdAt = { query.createdAt = {
$lte: new Date(endDate), $lte: new Date(endDate),
}; };