From 92c9f08c37d1bf944356156bb5aeb1b185877d96 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 16 Oct 2024 14:05:41 +0530 Subject: [PATCH] consumptions of particular tank --- src/controllers/tanksController.js | 69 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 37 ++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index aff63e2b..b52a17fd 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -4408,5 +4408,74 @@ client.on('message', async (topic, message) => { // }; +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; + + // Find the tank by customerId, tankLocation, and tankName + const tank = await Tank.findOne({ + customerId, + tankLocation: tankLocation || "overhead", // Default to "overhead" if not provided + tankName, + }); + + if (!tank) { + return reply.status(404).send({ + status_code: 404, + message: "Tank not found", + }); + } + + 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); + + // Find consumption records for the tank between the given dates + const tankConsumptions = await TankConsumptionOriginalSchema.find({ + customerId, + tankName, + tankLocation: tankLocation, + time: { + $gte: start, + $lte: end, + }, + + }); + + // Calculate total consumption from records + const total_consumption_from_records = tankConsumptions.reduce((acc, record) => { + return acc + parseInt(record.consumption, 10); + }, 0); + + // Calculate final consumption + const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; + + // Prepare response data + const tankData = { + tankname: tank.tankName, + totalConsumption: consumption, + block: tank.blockName, + TypeofWater: tank.typeOfWater, + location: tank.tankLocation, + capacity: tank.capacity, + waterlevel: tank.waterlevel, + }; + + // Send the response, including both total consumption and tankConsumptions data + reply.send({ + status_code: 200, + tankData, + totalConsumption: consumption, + consumptionRecords: tankConsumptions, // Add the consumption records here + }); + } catch (err) { + throw boom.boomify(err); + } +}; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 4aebc5fe..fdb84428 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1001,6 +1001,43 @@ module.exports = function (fastify, opts, next) { handler: tanksController.getBlockData, }); + fastify.route({ + method: "PUT", + url: "/api/consumptionofparticulartank/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for getting consumption of a particular tank", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "Customer ID", + }, + }, + }, + body: { + type: "object", + properties: { + tankName: { type: "string", description: "Tank name" }, + tankLocation: { type: "string", description: "Tank location" }, + startDate: { type: "string", description: "Start date" }, + stopDate: { type: "string", description: "Stop date" }, + block: { type: "string", description: "Block name" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.consumptionofparticulartank, + }); + + next(); }