From 41c23deb74a1de809c65c2ca97f62c6c40defd8e Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 28 Oct 2024 13:10:03 +0530 Subject: [PATCH] added getting datewise consumption records of all tanks --- src/controllers/tanksController.js | 84 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 39 +++++++++++++- 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5d192f8d..703650eb 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1025,6 +1025,90 @@ exports.consumption = async (request, reply) => { } }; +exports.consumptiondatewiseofalltanks = async (request, reply) => { + try { + const { customerId } = request.params; + const { startDate, stopDate, block } = request.body; + let { typeofwater } = request.body; + + // Convert typeofwater to lowercase + typeofwater = typeofwater.toLowerCase(); + const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); + const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); + + // Construct the query object based on block and typeofwater inputs + const tankQuery = { customerId, tankLocation: "overhead" }; + + if (block !== "All") { + tankQuery.blockName = block; + } + if (typeofwater !== "all") { + tankQuery.typeOfWater = typeofwater; + } + + const tanks = await Tank.find(tankQuery); + const tankconsumptionData = {}; + 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); + const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); + const tankname = tank.tankName; + + const tankConsumptions = await TankConsumptionOriginalSchema.find({ + customerId, + tankName: tankname, + tankLocation: tank.tankLocation, + ...(block !== "All" && { block: tank.blockName }), + ...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }) + }); + + const filteredConsumptions = tankConsumptions.filter((record) => { + const recordTime = moment(record.time, "DD-MMM-YYYY - HH:mm").toDate(); + return recordTime >= start && recordTime <= end; + }); + + const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => { + return acc + parseInt(record.consumption, 10); + }, 0); + + const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; + totalConsumptionForSelectedBlockAndTypeOfWater += consumption; + + // Organize data by date + for (const record of filteredConsumptions) { + const recordDate = moment(record.time, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); + + if (!tankconsumptionData[recordDate]) { + tankconsumptionData[recordDate] = { + date: recordDate, + consumptionRecordsdatewise: [] + }; + } + + tankconsumptionData[recordDate].consumptionRecordsdatewise.push({ + tankName: tankname, + consumption: record.consumption, + time: record.time + }); + } + } + + // Convert tankconsumptionData object to an array of dates for the response + const responseData = Object.values(tankconsumptionData); + + const response = { + status_code: 200, + consumptiorecordsdatewise: responseData, + [`total consumption of ${typeofwater} and selected block`]: totalConsumptionForSelectedBlockAndTypeOfWater + }; + + reply.send(response); + } catch (err) { + throw boom.boomify(err); + } +}; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index fdb84428..4b312761 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -405,7 +405,44 @@ module.exports = function (fastify, opts, next) { - + fastify.route({ + method: "PUT", + url: "/api/consumptiondatewiseofalltanks/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for getting consumption date wise of all tanks", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + body: { + type: "object", + // required: ['phone'], + properties: { + startDate:{ type: "string" }, + stopDate:{type:"string"}, + block:{type:"string"}, + typeofwater:{type:"string"}, + + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.consumptiondatewiseofalltanks, + });