diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 7d91e8c7..bba91fd4 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4181,15 +4181,13 @@ exports.particularCategory = async (req, reply) => { tankName: slave.tankName || "", location: slave.tankLocation || "", connected_status: slave.connected_status, - connected_lora_time: slave.connected_lora_time || null, - connected_lora_date: slave.connected_lora_date || null, - lora_last_check_time: slave.lora_last_check_time || null, lora_last_disconnect_time: slave.lora_last_disconnect_time || null, connected_to: slave.connected_to || "", masterName: orderMap[master.hardwareId]?.masterName || "", type: "slave", typeOfWater: slave.typeOfWater || "", - support_lora_last_check_time: null + support_lora_last_check_time: null, + category // category included for each slave })); disconnectedIssues.push({ @@ -4198,15 +4196,11 @@ exports.particularCategory = async (req, reply) => { location: orderMap[master.hardwareId]?.location || "", type: "master", connected_status: master.connected_status, - gsm_last_check_time: master.gsm_last_check_time || null, gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, - connected_gsm_date: master.connected_gsm_date || null, - connected_gsm_time: master.connected_gsm_time || null, - connected_lora_date: master.connected_lora_date || null, - connected_lora_time: master.connected_lora_time || null, support_gsm_last_check_time: null, connected_slave_count: slaveDetails.length, - connected_slaves: slaveDetails + connected_slaves: slaveDetails, + category // category included for master }); } diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index fd4deef1..c9937275 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2582,7 +2582,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { } // Fetch orders with the matching installationId - const orders = await Order.find({ installationId }).lean(); + const orders = await Order.find({ installationId }); if (!orders.length) { return reply.send({ @@ -2592,42 +2592,31 @@ exports.getOrdersByInstallationId = async (req, reply) => { }); } - // Group orders by customerId - const groupedByCustomer = {}; - - for (const order of orders) { - const { customerId, storeId } = order; - - // Fetch allocated sensors - const allocatedSensors = await Insensors.find({ - storeId, - customerId, - status: "blocked", - }).lean(); + // 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(); - const enrichedOrder = { - ...order, - allocated_sensors: allocatedSensors, - }; + // 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) + }).lean(); - if (!groupedByCustomer[customerId]) { - // Fetch customer once - const customer = await User.findOne({ customerId }).lean(); - groupedByCustomer[customerId] = { - customer: customer || null, - orders: [enrichedOrder], + return { + ...order.toObject(), + customer: customer || null, // Include customer details or null if not found + allocated_sensors: allocatedSensors, // List of allocated sensors }; - } else { - groupedByCustomer[customerId].orders.push(enrichedOrder); - } - } - - const response = Object.values(groupedByCustomer); + }) + ); return reply.send({ status_code: 200, - message: "Orders grouped by customer fetched successfully", - data: response, + message: "Orders fetched successfully", + data: ordersWithDetails, }); } catch (err) { console.error("Error fetching orders:", err); @@ -2637,6 +2626,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { + exports.getallocatedsensorstouser= async (req, reply) => { try { const { customerId } = req.params;