master^2
Varun 8 months ago
parent 3979e22b7f
commit 72a278ae6c

@ -1065,7 +1065,13 @@ exports.consumption = async (request, reply) => {
// Construct the query object // Construct the query object
const tankQuery = { customerId, tankLocation: "overhead" }; const tankQuery = { customerId, tankLocation: "overhead" };
if (block !== "All") tankQuery.blockName = block; if (block !== "All") tankQuery.blockName = block;
if (typeofwater !== "all") tankQuery.typeOfWater = typeofwater;
// Add typeofwater filter
if (typeofwater === "bore") {
tankQuery.typeOfWater = { $in: ["bore", "Bore Water"] };
} else if (typeofwater === "drinking") {
tankQuery.typeOfWater = { $in: ["drinking", "Drinking Water"] };
}
const tanks = await Tank.find(tankQuery); const tanks = await Tank.find(tankQuery);
const tankData = []; const tankData = [];
@ -1087,7 +1093,8 @@ exports.consumption = async (request, reply) => {
tankName: tank.tankName, tankName: tank.tankName,
tankLocation: tank.tankLocation, tankLocation: tank.tankLocation,
...(block !== "All" && { block: tank.blockName }), ...(block !== "All" && { block: tank.blockName }),
...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }), ...(typeofwater === "bore" && { typeofwater: { $in: ["bore", "Bore Water"] } }),
...(typeofwater === "drinking" && { typeofwater: { $in: ["drinking", "Drinking Water"] } }),
}); });
const filteredConsumptions = tankConsumptions.filter((record) => { const filteredConsumptions = tankConsumptions.filter((record) => {
@ -1102,11 +1109,10 @@ exports.consumption = async (request, reply) => {
const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records;
// Add to the total consumption and capacities based on water type // Add to the total consumption and capacities based on water type
if (tank.typeOfWater === "bore") { if (tank.typeOfWater === "bore" || tank.typeOfWater === "Bore Water") {
totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption; totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption;
totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity; totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity;
} } else if (tank.typeOfWater === "drinking" || tank.typeOfWater === "Drinking Water") {
if (tank.typeOfWater === "drinking") {
totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption; totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption;
totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity; totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity;
} }
@ -1127,7 +1133,7 @@ exports.consumption = async (request, reply) => {
}); });
} }
// Calculate total consumption percentage // Calculate total consumption percentages
const boreConsumptionPercentage = totalBoreCapacityForSelectedBlockAndTypeOfWater const boreConsumptionPercentage = totalBoreCapacityForSelectedBlockAndTypeOfWater
? ((totalBoreConsumptionForSelectedBlockAndTypeOfWater / totalBoreCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2) ? ((totalBoreConsumptionForSelectedBlockAndTypeOfWater / totalBoreCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2)
: 0; : 0;
@ -1141,7 +1147,10 @@ exports.consumption = async (request, reply) => {
? boreConsumptionPercentage ? boreConsumptionPercentage
: typeofwater === "drinking" : typeofwater === "drinking"
? drinkingConsumptionPercentage ? drinkingConsumptionPercentage
: 0; : ((totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater) /
(totalBoreCapacityForSelectedBlockAndTypeOfWater + totalDrinkingCapacityForSelectedBlockAndTypeOfWater) *
100
).toFixed(2);
// Include the total consumption in the response // Include the total consumption in the response
const response = { const response = {
@ -1149,11 +1158,19 @@ exports.consumption = async (request, reply) => {
tankData, tankData,
consumptiorecordsdatewise: tankconsumptionData, consumptiorecordsdatewise: tankconsumptionData,
totalConsumptionPercentage, totalConsumptionPercentage,
[`total consumption of ${typeofwater} and selected block`]: typeofwater === "bore" ...(typeofwater === "all"
? totalBoreConsumptionForSelectedBlockAndTypeOfWater ? {
: typeofwater === "drinking" totalConsumptionPercentageForBore: boreConsumptionPercentage,
? totalDrinkingConsumptionForSelectedBlockAndTypeOfWater totalConsumptionPercentageForDrinking: drinkingConsumptionPercentage,
: totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, totalConsumptionForBore: totalBoreConsumptionForSelectedBlockAndTypeOfWater,
totalConsumptionForDrinking: totalDrinkingConsumptionForSelectedBlockAndTypeOfWater,
}
: {
[`total consumption of ${typeofwater} and selected block`]:
typeofwater === "bore"
? totalBoreConsumptionForSelectedBlockAndTypeOfWater
: totalDrinkingConsumptionForSelectedBlockAndTypeOfWater,
}),
}; };
reply.send(response); reply.send(response);
@ -1163,6 +1180,7 @@ exports.consumption = async (request, reply) => {
}; };
exports.consumptiondatewiseofalltanks = async (request, reply) => { exports.consumptiondatewiseofalltanks = async (request, reply) => {
try { try {
const { customerId } = request.params; const { customerId } = request.params;
@ -2347,7 +2365,7 @@ exports.motorAction = async (req, reply) => {
const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing
const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing
const stopTime = req.body.stopTime const stopTime = req.body.stopTime
const motorOnType = req.body.motor_on_type; const motorOnType = "manual";
const manual_threshold_time = req.body.manual_threshold_time; const manual_threshold_time = req.body.manual_threshold_time;
let hasNotifiedStart = false; let hasNotifiedStart = false;
let hasNotifiedStop = false; let hasNotifiedStop = false;
@ -2402,7 +2420,7 @@ exports.motorAction = async (req, reply) => {
{ $set: { "connections.inputConnections.$.motor_stop_status": "2", { $set: { "connections.inputConnections.$.motor_stop_status": "2",
"connections.inputConnections.$.manual_threshold_time": manual_threshold_time, "connections.inputConnections.$.manual_threshold_time": manual_threshold_time,
"connections.inputConnections.$.threshold_type": "time", "connections.inputConnections.$.threshold_type": "time",
"connections.inputConnections.$.motor_on_type": motorOnType } } "connections.inputConnections.$.motor_on_type": "manual" } }
); );
// await Tank.updateOne( // await Tank.updateOne(

Loading…
Cancel
Save