|
|
|
@ -312,28 +312,29 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
let sumSumpBoreWaterCapacity = 0;
|
|
|
|
|
let sumOverheadBoreWaterCapacity = 0;
|
|
|
|
|
|
|
|
|
|
let totalInputPercentage = 0;
|
|
|
|
|
let inputCount = 0;
|
|
|
|
|
let totalOutputPercentage = 0;
|
|
|
|
|
let outputCount = 0;
|
|
|
|
|
|
|
|
|
|
const updated_data = await Tank.find({ customerId: customerId });
|
|
|
|
|
console.log("updated_data", updated_data);
|
|
|
|
|
|
|
|
|
|
updated_data.forEach((tank) => {
|
|
|
|
|
const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10);
|
|
|
|
|
const capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
const waterlevel = parseInt(tank.waterlevel ? tank.waterlevel.replace(/,/g, '') : '0', 10);
|
|
|
|
|
const capacity = parseInt(tank.capacity ? tank.capacity.replace(/,/g, '') : '0', 10);
|
|
|
|
|
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
|
|
|
|
|
if (tank.connections.inputConnections) {
|
|
|
|
|
tank.connections.inputConnections.forEach(inputConnection => {
|
|
|
|
|
if (inputConnection.water_level && inputConnection.capacity) {
|
|
|
|
|
const inputWaterLevel = parseInt(inputConnection.water_level.replace(/,/g, ''), 10);
|
|
|
|
|
const inputCapacity = parseInt(inputConnection.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
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++;
|
|
|
|
@ -341,14 +342,18 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
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
|
|
|
|
|
if (tank.connections.outputConnections) {
|
|
|
|
|
tank.connections.outputConnections.forEach(outputConnection => {
|
|
|
|
|
if (outputConnection.water_level && outputConnection.capacity) {
|
|
|
|
|
const outputWaterLevel = parseInt(outputConnection.water_level.replace(/,/g, ''), 10);
|
|
|
|
|
const outputCapacity = parseInt(outputConnection.capacity.replace(/,/g, ''), 10);
|
|
|
|
|
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++;
|
|
|
|
@ -356,6 +361,9 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
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
|
|
|
|
@ -378,15 +386,9 @@ exports.getTanklevels = async (req, reply) => {
|
|
|
|
|
const buildingName = user ? user.buildingName : null;
|
|
|
|
|
const address1 = user ? user.profile.address1 : null;
|
|
|
|
|
|
|
|
|
|
// Calculate average percentages
|
|
|
|
|
const inputWaterlevelPercentage = inputCount > 0 ? (totalInputPercentage / inputCount).toFixed(2) : null;
|
|
|
|
|
const outputWaterlevelPercentage = outputCount > 0 ? (totalOutputPercentage / outputCount).toFixed(2) : null;
|
|
|
|
|
|
|
|
|
|
const responseData = {
|
|
|
|
|
status_code: 200,
|
|
|
|
|
data: updated_data,
|
|
|
|
|
inputWaterlevelPercentage: inputWaterlevelPercentage,
|
|
|
|
|
outputWaterlevelPercentage: outputWaterlevelPercentage,
|
|
|
|
|
totalDrinkingWaterInSump: sumSumpDrinkingWater,
|
|
|
|
|
totalDrinkingWaterInOverhead: sumOverheadDrinkingWater,
|
|
|
|
|
totalBoreWaterInSump: sumSumpBoreWater,
|
|
|
|
|