From 2a241a6c621142a3e954dd5d6d93fea3d12f35fa Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 6 Sep 2024 14:14:59 +0530 Subject: [PATCH] consumption --- src/controllers/tanksController.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 37dcdb74..a89267ac 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -701,9 +701,6 @@ let supplier_tanks = []; // } - - - // if(supplier_tank_type==="bore" && receiver_type === "sump"){ // const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) @@ -771,7 +768,6 @@ let supplier_tanks = []; // } // // console.log(customerId,req.body.from,req.body.from_type,receiver_tank,req.body.to_type,) - // } // else if (action === "stop") { @@ -944,6 +940,7 @@ exports.consumption = async (request, reply) => { const { customerId } = request.params; const { startDate, stopDate, block } = request.body; let { typeofwater } = request.body; + // Convert typeofwater to lowercase typeofwater = typeofwater.toLowerCase(); const start = startDate; @@ -963,6 +960,9 @@ exports.consumption = async (request, reply) => { const tanks = await Tank.find(tankQuery); const tankData = []; + // Variable to track total consumption for the selected block and typeofwater + let totalConsumptionForSelectedBlockAndTypeOfWater = 0; + for (const tank of tanks) { const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10); const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10); @@ -987,6 +987,9 @@ exports.consumption = async (request, reply) => { const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; + // Add to the total consumption for the selected block and typeofwater + totalConsumptionForSelectedBlockAndTypeOfWater += consumption; + tankData.push({ tankname, totalConsumption: consumption, @@ -998,7 +1001,14 @@ exports.consumption = async (request, reply) => { }); } - reply.send({ status_code: 200, tankData }); + // Include the total consumption in the response + const response = { + status_code: 200, + tankData, + [`total consumption of ${typeofwater} and selected block`]: totalConsumptionForSelectedBlockAndTypeOfWater + }; + + reply.send(response); } catch (err) { throw boom.boomify(err); } @@ -1009,6 +1019,7 @@ exports.consumption = async (request, reply) => { + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); //const moment = require('moment'); // Import moment.js for date/time operations