From e4e94f53d3057f4285e51877e3561588c0efc7d7 Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 6 Mar 2025 14:21:56 +0530 Subject: [PATCH] added allocated sensors to orders --- src/controllers/storeController.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 741fa024..adebba8e 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2190,13 +2190,23 @@ exports.getOrdersByStoreId = async (req, reply) => { }); } - // Fetch customer details for each order - const ordersWithCustomerDetails = await Promise.all( + // Fetch customer details & allocated sensors for each order + const ordersWithDetails = await Promise.all( orders.map(async (order) => { + // Fetch customer details const customer = await User.findOne({ customerId: order.customerId }).lean(); + + // Fetch allocated sensors for this customer + const allocatedSensors = await Insensors.find({ + storeId, + customerId: order.customerId, // Match only sensors allocated to this customer + status: "blocked", // Only fetch sensors that are allocated (blocked) + }).lean(); + return { ...order.toObject(), customer: customer || null, // Include customer details or null if not found + allocated_sensors: allocatedSensors, // List of allocated sensors }; }) ); @@ -2204,7 +2214,7 @@ exports.getOrdersByStoreId = async (req, reply) => { return reply.send({ status_code: 200, message: "Orders fetched successfully", - data: ordersWithCustomerDetails, + data: ordersWithDetails, }); } catch (err) { console.error("Error fetching orders:", err);