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);
|
// console.log(frontdecoded);
|
||||||
const fuser = await User.findById(frontdecoded.id);
|
const fuser = await User.findById(frontdecoded.id);
|
||||||
// console.log(fuser);
|
// console.log(fuser);
|
||||||
|
// if (!fuser) {
|
||||||
|
// return res.status(404).json({
|
||||||
|
// success: false,
|
||||||
|
// message: "User not found",
|
||||||
|
// });
|
||||||
|
// }
|
||||||
req.user = fuser;
|
req.user = fuser;
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
@ -77,7 +77,6 @@ export const createOrder = async (req, res) => {
|
|||||||
export const processOrder = async (req, res) => {
|
export const processOrder = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { orderId, invoiceItems } = req.body;
|
const { orderId, invoiceItems } = req.body;
|
||||||
|
|
||||||
if (!orderId || !invoiceItems || !Array.isArray(invoiceItems)) {
|
if (!orderId || !invoiceItems || !Array.isArray(invoiceItems)) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
@ -86,7 +85,6 @@ export const processOrder = async (req, res) => {
|
|||||||
|
|
||||||
// Find the order by ID
|
// Find the order by ID
|
||||||
const order = await PdOrder.findById(orderId).populate("addedBy");
|
const order = await PdOrder.findById(orderId).populate("addedBy");
|
||||||
|
|
||||||
if (!order) {
|
if (!order) {
|
||||||
return res.status(404).json({ error: "Order not found" });
|
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 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);
|
exceededItems.push(item.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -362,7 +362,6 @@ export const getCancelledOrders = async (req, res) => {
|
|||||||
export const processOrder = async (req, res) => {
|
export const processOrder = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { orderId, invoiceItems } = req.body;
|
const { orderId, invoiceItems } = req.body;
|
||||||
|
|
||||||
if (!orderId || !invoiceItems || !Array.isArray(invoiceItems)) {
|
if (!orderId || !invoiceItems || !Array.isArray(invoiceItems)) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
@ -374,25 +373,16 @@ export const processOrder = async (req, res) => {
|
|||||||
if (!order) {
|
if (!order) {
|
||||||
return res.status(404).json({ error: "Order not found" });
|
return res.status(404).json({ error: "Order not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate quantities
|
// Validate quantities
|
||||||
const exceededItems = invoiceItems
|
const exceededItems = invoiceItems
|
||||||
.filter((item) => {
|
.filter((item) => {
|
||||||
const orderItem = order.orderItem.find(
|
const orderItem = order.orderItem.find(
|
||||||
(i) => i.productId.toString() === item.productId.toString()
|
(i) => i.productId.toString() === item.productId.toString()
|
||||||
);
|
);
|
||||||
return orderItem && item.processquantity > orderItem.remainingQuantity;
|
return !orderItem || item.processquantity > orderItem.remainingQuantity;
|
||||||
})
|
})
|
||||||
.map((item) => item.name);
|
.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 there are any exceeded items, return an error with their names
|
||||||
if (exceededItems.length > 0) {
|
if (exceededItems.length > 0) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -415,7 +405,6 @@ export const processOrder = async (req, res) => {
|
|||||||
const productInStock = pdStock.products.find(
|
const productInStock = pdStock.products.find(
|
||||||
(p) => p.productid.toString() === item.productId.toString()
|
(p) => p.productid.toString() === item.productId.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
// If the product exists in stock
|
// If the product exists in stock
|
||||||
if (productInStock) {
|
if (productInStock) {
|
||||||
// Check if the processquantity is less than or equal to available stock
|
// 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
|
// If no stock or insufficient stock, don't add the item
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
// If no items are left after stock validation, return an error
|
// If no items are left after stock validation, return an error
|
||||||
if (updatedInvoiceItems.length === 0) {
|
if (updatedInvoiceItems.length === 0) {
|
||||||
return res
|
return res
|
||||||
@ -1656,9 +1644,7 @@ export const getAllOrdersforAdmin = async (req, res) => {
|
|||||||
if (retailerIds.length > 0) {
|
if (retailerIds.length > 0) {
|
||||||
filter.addedBy = { $in: retailerIds }; // Filter orders by found user IDs
|
filter.addedBy = { $in: retailerIds }; // Filter orders by found user IDs
|
||||||
} else {
|
} else {
|
||||||
return res
|
return res.status(200).json({
|
||||||
.status(200)
|
|
||||||
.json({
|
|
||||||
totalOrders: 0,
|
totalOrders: 0,
|
||||||
totalPages: 0,
|
totalPages: 0,
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
|
Loading…
Reference in New Issue
Block a user