From 2d90cbc1992ae1a43e8412b13992d573d02f02bf Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 17 Oct 2024 13:34:48 +0530 Subject: [PATCH] changes --- src/controllers/tanksController.js | 32 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5e1ea2f2..a863ee2c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -4413,14 +4413,17 @@ client.on('message', async (topic, message) => { //const moment = require('moment'); + + + exports.consumptionofparticulartank = async (request, reply) => { try { const { customerId } = request.params; const { startDate, stopDate, tankName, tankLocation, block } = request.body; - console.log("hii") - // Convert input dates into strings in the same format as stored in the database - const start = moment(startDate, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); - const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); + + // Convert input dates into proper JavaScript Date objects for comparison + const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); + const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); // Find the tank by customerId, tankLocation, and tankName const tank = await Tank.findOne({ @@ -4440,19 +4443,21 @@ exports.consumptionofparticulartank = async (request, reply) => { const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ""), 10); const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ""), 10); - // Find consumption records by comparing string dates + // Fetch all records for the tank (no date filtering yet) const tankConsumptions = await TankConsumptionOriginalSchema.find({ customerId, tankName, tankLocation: tankLocation, - time: { - $gte: start, // Start date as string - $lte: end, // End date as string - } }).sort({ time: 1 }); - // Calculate total consumption from records - const total_consumption_from_records = tankConsumptions.reduce((acc, record) => { + // Filter records in JavaScript by comparing the 'time' field after converting to Date + const filteredConsumptions = tankConsumptions.filter((record) => { + const recordTime = moment(record.time, "DD-MMM-YYYY - HH:mm").toDate(); + return recordTime >= start && recordTime <= end; + }); + + // Calculate total consumption from filtered records + const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => { return acc + parseInt(record.consumption, 10); }, 0); @@ -4470,12 +4475,12 @@ exports.consumptionofparticulartank = async (request, reply) => { waterlevel: tank.waterlevel, }; - // Send the response, including both total consumption and tankConsumptions data + // Send the response, including both total consumption and filtered consumption records reply.send({ status_code: 200, tankData, totalConsumption: consumption, - consumptionRecords: tankConsumptions, // Add the consumption records here + consumptionRecords: filteredConsumptions, }); } catch (err) { throw boom.boomify(err); @@ -4485,3 +4490,4 @@ exports.consumptionofparticulartank = async (request, reply) => { +