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