diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 99eb3e58..b11c890d 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -5828,6 +5828,7 @@ "version": "5.2.0", "resolved": "https://registry.npmjs.org/fastify-cors/-/fastify-cors-5.2.0.tgz", "integrity": "sha512-Lde71qT23M3Ip3pmny3uN6q6lQ4J5J0/YWoqe2stL4sMT99R5LEtTJ2La2zijFOR5OFhxvxqGgR7BIc2x3amPg==", + "license": "MIT", "dependencies": { "fastify-plugin": "^3.0.0", "vary": "^1.1.2" diff --git a/package-lock.json b/package-lock.json index e841c8e3..3f928ea4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5893,6 +5893,7 @@ "version": "5.2.0", "resolved": "https://registry.npmjs.org/fastify-cors/-/fastify-cors-5.2.0.tgz", "integrity": "sha512-Lde71qT23M3Ip3pmny3uN6q6lQ4J5J0/YWoqe2stL4sMT99R5LEtTJ2La2zijFOR5OFhxvxqGgR7BIc2x3amPg==", + "license": "MIT", "dependencies": { "fastify-plugin": "^3.0.0", "vary": "^1.1.2" diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e24f1cc8..a863ee2c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -4412,14 +4412,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; - // 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({ @@ -4439,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); @@ -4469,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); @@ -4484,3 +4490,4 @@ exports.consumptionofparticulartank = async (request, reply) => { + diff --git a/src/index.js b/src/index.js index 2ad09141..38ab3ab9 100644 --- a/src/index.js +++ b/src/index.js @@ -8,9 +8,10 @@ const storeController = require("./controllers/storeController.js") const boom = require("boom"); const bcrypt = require('bcrypt'); const { ProfilePictureStore,generateinstallationId,Store} = require("./models/store"); +const cors = require('fastify-cors'); -const cors = require("cors"); +//const cors = require("cors"); const swagger = require("./config/swagger"); const rawBody = require('raw-body') const uuidv4 = require("uuid").v4; @@ -25,6 +26,10 @@ const fastify = require("fastify")({ // const Fastify = require("fastify"); +fastify.register(cors, { + origin: 'http://localhost:3001', // Allow only your frontend URL + methods: ['GET', 'POST', 'PUT', 'DELETE'], // Allowed HTTP methods +}); // const Fastify = require("fastify");