consumptions of particular tank

master
Varun 12 months ago
parent a1d45090b2
commit 92c9f08c37

@ -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);
}
};

@ -1001,6 +1001,43 @@ module.exports = function (fastify, opts, next) {
handler: tanksController.getBlockData, 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(); next();
} }

Loading…
Cancel
Save