diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 414a2ba4..a3801708 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -509,8 +509,9 @@ exports.getTanklevels = async (req, reply) => { let sumSumpBoreWaterCapacity = 0; let sumOverheadBoreWaterCapacity = 0; - const updated_data = await Tank.find({ customerId: customerId }); - console.log("updated_data", updated_data); + // Fetch only active tanks + const updated_data = await Tank.find({ customerId: customerId, status: "active" }); + console.log("Active Tanks Data:", updated_data); updated_data.forEach((tank) => { const waterlevel = parseInt(tank.waterlevel ? tank.waterlevel.replace(/,/g, '') : '0', 10); @@ -518,52 +519,53 @@ exports.getTanklevels = async (req, reply) => { const waterlevelPercentage = ((waterlevel / capacity) * 100).toFixed(2); tank.waterlevelPercentage = waterlevelPercentage; // Add water level percentage to each tank object console.log(`Processing tank: ${tank.tankName}`); - console.log(`Type of Water: ${tank.typeOfWater}, Location: ${tank.tankLocation}, Waterlevel: ${waterlevel}, Capacity: ${capacity}, Waterlevel Percentage: ${waterlevelPercentage}%`); let totalInputPercentage = 0; let inputCount = 0; let totalOutputPercentage = 0; let outputCount = 0; - // Calculate and add water level percentages for inputConnections + // Process input connections if (tank.connections.inputConnections) { tank.connections.inputConnections.forEach(inputConnection => { - const inputWaterLevel = inputConnection.water_level ? parseInt(inputConnection.water_level.replace(/,/g, ''), 10) : 0; - const inputCapacity = inputConnection.capacity ? parseInt(inputConnection.capacity.replace(/,/g, ''), 10) : 0; - - if (inputCapacity > 0) { - inputConnection.waterlevelPercentage = ((inputWaterLevel / inputCapacity) * 100).toFixed(2); - totalInputPercentage += parseFloat(inputConnection.waterlevelPercentage); - inputCount++; - } else { - inputConnection.waterlevelPercentage = null; + if (inputConnection.status === "active") { // Process only active connections + const inputWaterLevel = inputConnection.water_level ? parseInt(inputConnection.water_level.replace(/,/g, ''), 10) : 0; + const inputCapacity = inputConnection.capacity ? parseInt(inputConnection.capacity.replace(/,/g, ''), 10) : 0; + + if (inputCapacity > 0) { + inputConnection.waterlevelPercentage = ((inputWaterLevel / inputCapacity) * 100).toFixed(2); + totalInputPercentage += parseFloat(inputConnection.waterlevelPercentage); + inputCount++; + } else { + inputConnection.waterlevelPercentage = null; + } } }); - // Add the average input water level percentage to the tank's connections object tank.connections.inputWaterlevelPercentage = inputCount > 0 ? (totalInputPercentage / inputCount).toFixed(2) : null; } - // Calculate and add water level percentages for outputConnections + // Process output connections if (tank.connections.outputConnections) { tank.connections.outputConnections.forEach(outputConnection => { - const outputWaterLevel = outputConnection.water_level ? parseInt(outputConnection.water_level.replace(/,/g, ''), 10) : 0; - const outputCapacity = outputConnection.capacity ? parseInt(outputConnection.capacity.replace(/,/g, ''), 10) : 0; - - if (outputCapacity > 0) { - outputConnection.waterlevelPercentage = ((outputWaterLevel / outputCapacity) * 100).toFixed(2); - totalOutputPercentage += parseFloat(outputConnection.waterlevelPercentage); - outputCount++; - } else { - outputConnection.waterlevelPercentage = null; + if (outputConnection.status === "active") { // Process only active connections + const outputWaterLevel = outputConnection.water_level ? parseInt(outputConnection.water_level.replace(/,/g, ''), 10) : 0; + const outputCapacity = outputConnection.capacity ? parseInt(outputConnection.capacity.replace(/,/g, ''), 10) : 0; + + if (outputCapacity > 0) { + outputConnection.waterlevelPercentage = ((outputWaterLevel / outputCapacity) * 100).toFixed(2); + totalOutputPercentage += parseFloat(outputConnection.waterlevelPercentage); + outputCount++; + } else { + outputConnection.waterlevelPercentage = null; + } } }); - // Add the average output water level percentage to the tank's connections object tank.connections.outputWaterlevelPercentage = outputCount > 0 ? (totalOutputPercentage / outputCount).toFixed(2) : null; } - // Summing up the total water levels and capacities + // Summing up the total water levels and capacities for active tanks only if (tank.tankLocation === 'sump' && tank.typeOfWater === 'drinking') { sumSumpDrinkingWater += waterlevel; sumSumpDrinkingWaterCapacity += capacity; @@ -581,8 +583,6 @@ exports.getTanklevels = async (req, reply) => { totalavailableDrinkingwater += waterlevel; totalDrinkingcapacity += capacity; } - - else if ( tank.typeOfWater === 'bore') { totalavailableBorewater += waterlevel; totalBorewatercapacity += capacity;