From 7069317d93abfb161040cc9f016d5d966eca8631 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 21 May 2025 14:10:33 +0530 Subject: [PATCH] changes --- src/controllers/storeController.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index c9937275..f838858b 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2592,23 +2592,33 @@ exports.getOrdersByInstallationId = async (req, reply) => { }); } - // Fetch customer details & allocated sensors for each order + const uniqueCustomersMap = new Map(); + + // Build unique customerId-based map + for (const order of orders) { + if (!uniqueCustomersMap.has(order.customerId)) { + uniqueCustomersMap.set(order.customerId, order); + } + } + + // Only keep one order per customerId + const uniqueOrders = Array.from(uniqueCustomersMap.values()); + + // Enrich with customer and sensor info const ordersWithDetails = await Promise.all( - orders.map(async (order) => { - // Fetch customer details + uniqueOrders.map(async (order) => { const customer = await User.findOne({ customerId: order.customerId }).lean(); - // Fetch allocated sensors for this customer const allocatedSensors = await Insensors.find({ - storeId: order.storeId, - customerId: order.customerId, // Match only sensors allocated to this customer - status: "blocked", // Only fetch sensors that are allocated (blocked) + storeId: order.storeId, + customerId: order.customerId, + status: "blocked", }).lean(); return { ...order.toObject(), - customer: customer || null, // Include customer details or null if not found - allocated_sensors: allocatedSensors, // List of allocated sensors + customer: customer || null, + allocated_sensors: allocatedSensors, }; }) ); @@ -2618,6 +2628,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { message: "Orders fetched successfully", data: ordersWithDetails, }); + } catch (err) { console.error("Error fetching orders:", err); return reply.status(500).send({ error: "Internal server error" }); @@ -2627,6 +2638,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { + exports.getallocatedsensorstouser= async (req, reply) => { try { const { customerId } = req.params;