From b3c29cf9f4e9739c0d4e6ae8136359deb40dae8d Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 16 Oct 2024 16:24:56 +0530 Subject: [PATCH] changes --- src/controllers/tanksController.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 894841db..e24f1cc8 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -3791,7 +3791,7 @@ const updatetotalConsumptiontillmidnight = async () => { const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); // Check if the record already exists - const existingRecord = await TankConsumptionOriginalSchema.findOne({ + const existingRecord = await TankConsumptionSchema.findOne({ customerId: tank.customerId, tankName: tank.tankName, tankLocation: tank.tankLocation, @@ -3800,7 +3800,7 @@ const updatetotalConsumptiontillmidnight = async () => { if (!existingRecord) { // Create and save the new document if it doesn't exist - const newTankConsumption = new TankConsumptionOriginalSchema({ + const newTankConsumption = new TankConsumptionSchema({ customerId: tank.customerId, tankName: tank.tankName, tankLocation: tank.tankLocation, @@ -4408,14 +4408,18 @@ 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; - // Ensure dates are formatted or parsed correctly for the query - const start = startDate; - const end = stopDate; + // 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"); // Find the tank by customerId, tankLocation, and tankName const tank = await Tank.findOne({ @@ -4435,17 +4439,16 @@ 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 for the tank between the given dates + // Find consumption records by comparing string dates const tankConsumptions = await TankConsumptionOriginalSchema.find({ customerId, tankName, tankLocation: tankLocation, time: { - $gte: start, - $lte: end, - }, - - }); + $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) => { @@ -4479,3 +4482,5 @@ exports.consumptionofparticulartank = async (request, reply) => { }; + +