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