bugs fixed
This commit is contained in:
parent
4e4aefb84c
commit
546ee6ac3e
@ -27,6 +27,12 @@ export const isAuthenticatedUser = async (req, res, next) => {
|
||||
// console.log(frontdecoded);
|
||||
const fuser = await User.findById(frontdecoded.id);
|
||||
// console.log(fuser);
|
||||
// if (!fuser) {
|
||||
// return res.status(404).json({
|
||||
// success: false,
|
||||
// message: "User not found",
|
||||
// });
|
||||
// }
|
||||
req.user = fuser;
|
||||
|
||||
next();
|
||||
|
@ -77,7 +77,6 @@ export const createOrder = async (req, res) => {
|
||||
export const processOrder = async (req, res) => {
|
||||
try {
|
||||
const { orderId, invoiceItems } = req.body;
|
||||
|
||||
if (!orderId || !invoiceItems || !Array.isArray(invoiceItems)) {
|
||||
return res
|
||||
.status(400)
|
||||
@ -86,7 +85,6 @@ export const processOrder = async (req, res) => {
|
||||
|
||||
// Find the order by ID
|
||||
const order = await PdOrder.findById(orderId).populate("addedBy");
|
||||
|
||||
if (!order) {
|
||||
return res.status(404).json({ error: "Order not found" });
|
||||
}
|
||||
@ -101,7 +99,7 @@ export const processOrder = async (req, res) => {
|
||||
);
|
||||
|
||||
// If processquantity exceeds remainingQuantity, add the item name to exceededItems
|
||||
if (orderItem && item.processquantity > orderItem.remainingQuantity) {
|
||||
if (!orderItem || item.processquantity > orderItem.remainingQuantity) {
|
||||
exceededItems.push(item.name);
|
||||
}
|
||||
}
|
||||
|
@ -362,7 +362,6 @@ export const getCancelledOrders = async (req, res) => {
|
||||
export const processOrder = async (req, res) => {
|
||||
try {
|
||||
const { orderId, invoiceItems } = req.body;
|
||||
|
||||
if (!orderId || !invoiceItems || !Array.isArray(invoiceItems)) {
|
||||
return res
|
||||
.status(400)
|
||||
@ -374,25 +373,16 @@ export const processOrder = async (req, res) => {
|
||||
if (!order) {
|
||||
return res.status(404).json({ error: "Order not found" });
|
||||
}
|
||||
|
||||
// Validate quantities
|
||||
const exceededItems = invoiceItems
|
||||
.filter((item) => {
|
||||
const orderItem = order.orderItem.find(
|
||||
(i) => i.productId.toString() === item.productId.toString()
|
||||
);
|
||||
return orderItem && item.processquantity > orderItem.remainingQuantity;
|
||||
return !orderItem || item.processquantity > orderItem.remainingQuantity;
|
||||
})
|
||||
.map((item) => item.name);
|
||||
|
||||
if (exceededItems.length > 0) {
|
||||
return res.status(400).json({
|
||||
error: `The following items have more quantity than remaining in the order: ${exceededItems.join(
|
||||
", "
|
||||
)}`,
|
||||
});
|
||||
}
|
||||
|
||||
// If there are any exceeded items, return an error with their names
|
||||
if (exceededItems.length > 0) {
|
||||
return res.status(400).json({
|
||||
@ -415,7 +405,6 @@ export const processOrder = async (req, res) => {
|
||||
const productInStock = pdStock.products.find(
|
||||
(p) => p.productid.toString() === item.productId.toString()
|
||||
);
|
||||
|
||||
// If the product exists in stock
|
||||
if (productInStock) {
|
||||
// Check if the processquantity is less than or equal to available stock
|
||||
@ -431,7 +420,6 @@ export const processOrder = async (req, res) => {
|
||||
// If no stock or insufficient stock, don't add the item
|
||||
return false;
|
||||
});
|
||||
|
||||
// If no items are left after stock validation, return an error
|
||||
if (updatedInvoiceItems.length === 0) {
|
||||
return res
|
||||
@ -1656,9 +1644,7 @@ export const getAllOrdersforAdmin = async (req, res) => {
|
||||
if (retailerIds.length > 0) {
|
||||
filter.addedBy = { $in: retailerIds }; // Filter orders by found user IDs
|
||||
} else {
|
||||
return res
|
||||
.status(200)
|
||||
.json({
|
||||
return res.status(200).json({
|
||||
totalOrders: 0,
|
||||
totalPages: 0,
|
||||
currentPage: page,
|
||||
@ -1670,7 +1656,7 @@ export const getAllOrdersforAdmin = async (req, res) => {
|
||||
// Fetch orders with population
|
||||
const orders = await RdOrder.find(filter)
|
||||
.populate({ path: "addedBy", select: "name email" }) // Populate retailer details
|
||||
.populate({path: "pd", select: "name email"}) // Populate PD details
|
||||
.populate({ path: "pd", select: "name email" }) // Populate PD details
|
||||
.sort({ createdAt: -1 })
|
||||
.skip(skip)
|
||||
.limit(limit);
|
||||
|
Loading…
Reference in New Issue
Block a user