get distributor in sorted format
This commit is contained in:
parent
00a5f90355
commit
5fe1da6c98
@ -13,12 +13,14 @@ export const addInventory = async (req, res) => {
|
||||
const { products, addedFor, addedForId } = req.body;
|
||||
const userId = req.user._id;
|
||||
const userType = req.userType;
|
||||
|
||||
|
||||
if (addedFor === "RetailDistributor") {
|
||||
// Find the RetailDistributor stock
|
||||
const rdStock = await RDStock.findOne({ userId: addedForId });
|
||||
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
|
||||
@ -29,7 +31,9 @@ export const addInventory = async (req, res) => {
|
||||
const { productid, Sale, ProductName } = product;
|
||||
|
||||
// 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) {
|
||||
// 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) {
|
||||
return res.status(400).json({
|
||||
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",
|
||||
data: newInventory,
|
||||
});
|
||||
|
||||
} catch (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" });
|
||||
}
|
||||
let filter = { role: "principal-Distributor" };
|
||||
let query={};
|
||||
let query = {};
|
||||
// Check the user type and adjust the filter accordingly
|
||||
if (req.userType === "SalesCoOrdinator") {
|
||||
// If userType is "SalesCoOrdinator", filter by req.user.mappedBy
|
||||
@ -103,7 +108,9 @@ export const getDistributors = async (req, res) => {
|
||||
// console.log("type",type);
|
||||
if (type === "PrincipalDistributor") {
|
||||
// Fetch all PrincipalDistributors
|
||||
const principalDistributors = await User.find(filter);
|
||||
const principalDistributors = await User.find(filter).sort({
|
||||
createdAt: -1,
|
||||
});
|
||||
// console.log("principalDistributors",principalDistributors);
|
||||
// Map each PrincipalDistributor to include their ShippingAddress
|
||||
distributors = await Promise.all(
|
||||
@ -119,7 +126,9 @@ export const getDistributors = async (req, res) => {
|
||||
);
|
||||
} else {
|
||||
// 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);
|
||||
@ -148,12 +157,12 @@ export const getAllInventories = async (req, res) => {
|
||||
$lte: new Date(end).setDate(end.getDate() + 1),
|
||||
};
|
||||
}
|
||||
} else if (startDate && endDate==='') {
|
||||
} else if (startDate && endDate === "") {
|
||||
query.createdAt = {
|
||||
$gte: new Date(startDate),
|
||||
$lte: new Date(),
|
||||
};
|
||||
} else if (endDate && startDate==='') {
|
||||
} else if (endDate && startDate === "") {
|
||||
query.createdAt = {
|
||||
$lte: new Date(endDate),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user