diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 1e0e08aa..0084c62b 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -869,46 +869,83 @@ exports.createMasterSlaveData = async (req, reply) => { reply.status(500).send({ message: err.message }); } }; - exports.masterConnectedSlaveList = async (req, reply) => { try { const { connectedTo } = req.params; // Step 1: Find all slave tanks where connected_to matches - const slaveTanks = await Insensors.find({ connected_to: connectedTo }); - console.log(slaveTanks,"slaveTanks") + const slaveTanks = await Insensors.find({ connected_to: connectedTo }).lean(); + console.log(slaveTanks, "slaveTanks"); if (!slaveTanks || slaveTanks.length === 0) { - return reply.status(404).send({ success: false, message: "No tanks found for the given connected_to value" }); + return reply.status(404).send({ + success: false, + message: "No tanks found for the given connected_to value" + }); } - // Step 2: Fetch `tankLocation` and `typeOfWater` from the Tank schema - const tankDetails = await Tank.findOne({ hardwareId: connectedTo }, { tankLocation: 1, typeOfWater: 1 }); + // Step 2: Fetch `tankLocation` and `typeOfWater` from the Tank schema (for master device display) + const tankDetails = await Tank.findOne( + { hardwareId: connectedTo }, + { tankLocation: 1, typeOfWater: 1 } + ); if (!tankDetails) { - return reply.status(404).send({ success: false, message: "Tank details not found for the given connectedTo" }); + return reply.status(404).send({ + success: false, + message: "Tank details not found for the given connectedTo" + }); } // Step 3: Count the number of connected slaves const slaveCount = slaveTanks.length; + // Step 4: Fetch latest IotData for the master to extract tankHeight for each slave + const latestIotData = await IotData.findOne({ hardwareId: connectedTo }).sort({ date: -1 }).lean(); + + // Step 5: Prepare processed response for each tank + const processedSlaves = slaveTanks.map(slave => { + if (slave.type === 'slave') { + // Find matching tank data for this slave in IotData + const matchingTankData = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slave.hardwareId); + + return { + ...slave, + tankHeight: matchingTankData?.tankHeight ?? null // Add tankHeight if found + }; + } else { + // Remove unnecessary fields for master type + const { + tankName, + tankLocation, + typeOfWater, + ...rest + } = slave; + return rest; + } + }); + return reply.send({ success: true, tankLocation: tankDetails.tankLocation, typeOfWater: tankDetails.typeOfWater, connectedSlaveCount: slaveCount, - data: slaveTanks, + data: processedSlaves }); } catch (error) { console.error("Error fetching master connected slave data:", error); - return reply.status(500).send({ success: false, message: "Internal Server Error" }); + return reply.status(500).send({ + success: false, + message: "Internal Server Error" + }); } }; + exports.mastrerList = async (req, reply) => { try { const { customerId, installationId } = req.params;