diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 09c5455e..ee3483dd 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2511,37 +2511,44 @@ exports.getOrdersByInstallationId = async (req, reply) => { const { installationId } = req.params; if (!installationId) { - return reply.status(400).send({ error: "storeId is required" }); + return reply.status(400).send({ error: "installationId is required" }); } - // Fetch orders with the matching storeId + // Fetch orders by installationId const orders = await Order.find({ installationId }); if (!orders.length) { return reply.send({ status_code: 200, - message: "No orders found for this store", + message: "No orders found for this installation", data: [], }); } - // Fetch customer details & allocated sensors for each order + // Filter unique customerId + installationId combinations + const seen = new Set(); + const uniqueOrders = orders.filter(order => { + const key = `${order.customerId}_${order.installationId}`; + if (seen.has(key)) return false; + seen.add(key); + return true; + }); + + // Fetch details for each unique order 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({ installationId, - customerId: order.customerId, // Match only sensors allocated to this customer - status: "blocked", // Only fetch sensors that are allocated (blocked) + 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, }; }) ); @@ -2557,6 +2564,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { } }; + exports.getallocatedsensorstouser= async (req, reply) => { try { const { customerId } = req.params;