From be0c4063944a661ad3b7a5448cb150e73106e4d2 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 5 Mar 2025 11:05:39 +0530 Subject: [PATCH] changes in get orders of storeId --- src/controllers/storeController.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 9e440f0f..b6be6944 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2069,6 +2069,7 @@ exports.acceptQuotation = async (req, reply) => { }; + exports.getOrdersByStoreId = async (req, reply) => { try { const { storeId } = req.params; @@ -2080,10 +2081,29 @@ exports.getOrdersByStoreId = async (req, reply) => { // Fetch orders with the matching storeId const orders = await Order.find({ storeId }); + if (!orders.length) { + return reply.send({ + status_code: 200, + message: "No orders found for this store", + data: [], + }); + } + + // Fetch customer details for each order + const ordersWithCustomerDetails = await Promise.all( + orders.map(async (order) => { + const customer = await User.findOne({ customerId: order.customerId }).lean(); + return { + ...order.toObject(), + customer: customer || null, // Include customer details or null if not found + }; + }) + ); + return reply.send({ status_code: 200, message: "Orders fetched successfully", - data: orders, + data: ordersWithCustomerDetails, }); } catch (err) { console.error("Error fetching orders:", err);