diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index bec633d9..09bbee2a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1051,6 +1051,7 @@ let supplier_tanks = []; + exports.consumption = async (request, reply) => { try { const { customerId } = request.params; @@ -1065,11 +1066,18 @@ exports.consumption = async (request, reply) => { // Construct the query object const tankQuery = { customerId, tankLocation: "overhead" }; 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 tankData = []; const tankconsumptionData = []; + let totalConsumptionForSelectedBlockAndTypeOfWater = 0; let totalBoreConsumptionForSelectedBlockAndTypeOfWater = 0; let totalBoreCapacityForSelectedBlockAndTypeOfWater = 0; let totalDrinkingConsumptionForSelectedBlockAndTypeOfWater = 0; @@ -1087,7 +1095,8 @@ exports.consumption = async (request, reply) => { tankName: tank.tankName, tankLocation: tank.tankLocation, ...(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) => { @@ -1100,13 +1109,13 @@ exports.consumption = async (request, reply) => { }, 0); const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; - + totalConsumptionForSelectedBlockAndTypeOfWater += consumption; + // 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; totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity; - } - if (tank.typeOfWater === "drinking") { + } else if (tank.typeOfWater === "drinking" || tank.typeOfWater === "Drinking Water") { totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption; totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity; } @@ -1127,7 +1136,7 @@ exports.consumption = async (request, reply) => { }); } - // Calculate total consumption percentage + // Calculate total consumption percentages const boreConsumptionPercentage = totalBoreCapacityForSelectedBlockAndTypeOfWater ? ((totalBoreConsumptionForSelectedBlockAndTypeOfWater / totalBoreCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2) : 0; @@ -1141,7 +1150,10 @@ exports.consumption = async (request, reply) => { ? boreConsumptionPercentage : typeofwater === "drinking" ? drinkingConsumptionPercentage - : 0; + : ((totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater) / + (totalBoreCapacityForSelectedBlockAndTypeOfWater + totalDrinkingCapacityForSelectedBlockAndTypeOfWater) * + 100 + ).toFixed(2); // Include the total consumption in the response const response = { @@ -1149,11 +1161,21 @@ exports.consumption = async (request, reply) => { tankData, consumptiorecordsdatewise: tankconsumptionData, totalConsumptionPercentage, - [`total consumption of ${typeofwater} and selected block`]: typeofwater === "bore" - ? totalBoreConsumptionForSelectedBlockAndTypeOfWater - : typeofwater === "drinking" - ? totalDrinkingConsumptionForSelectedBlockAndTypeOfWater - : totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, + ...(typeofwater === "all" + ? { + totalConsumptionPercentageForBore: boreConsumptionPercentage, + totalConsumptionPercentageForDrinking: drinkingConsumptionPercentage, + totalConsumptionForBore: totalBoreConsumptionForSelectedBlockAndTypeOfWater, + totalConsumptionForDrinking: totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, + } + : { + [`total consumption of ${typeofwater} and selected block`]: + typeofwater === "bore" + ? totalBoreConsumptionForSelectedBlockAndTypeOfWater + : totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, + } + ), + [`total consumption of ${typeofwater} and selected block`]: totalConsumptionForSelectedBlockAndTypeOfWater }; reply.send(response); @@ -1163,6 +1185,7 @@ exports.consumption = async (request, reply) => { }; + exports.consumptiondatewiseofalltanks = async (request, reply) => { try { const { customerId } = request.params; @@ -1814,7 +1837,7 @@ eventEmitter.on( // Retrieve the user information const users = await User.find({ fcmIds: { $in: fcmTokens } }); const userNames = users.map(user => user.username).join(', '); - const startMethod = motorOnType === "Forced Manual"; + const startMethod = motorOnType.toUpperCase() === "Forced Manual"; // Prepare the message const message = @@ -1842,14 +1865,14 @@ eventEmitter.on( // Retrieve the user information const users = await User.find({ fcmIds: { $in: fcmTokens } }); const userNames = users.map(user => user.username).join(', '); - const stopMethod = motorOnType === "Forced Manual"; + const stopMethod = motorOnType.toUpperCase() === "Forced Manual"; // Prepare the message const message = `🚰 Tank Name: '${tankName}'\n` + `🕒 Pump stopped at: '${stopTime}'\n` + `👤 Initiated by: ${userNames}\n` + - `Motor Off Type: '${stopMethod}'\n`; + `🔄 Pump stopped by: '${stopMethod}'\n`; // Send the notification await sendNotification(fcmTokens, 'Motor Stopped 🛑', message); @@ -2436,7 +2459,7 @@ exports.motorAction = async (req, reply) => { 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 stopTime = req.body.stopTime - const motorOnType = req.body.motor_on_type; + const motorOnType = "manual"; const manual_threshold_time = req.body.manual_threshold_time; let hasNotifiedStart = false; let hasNotifiedStop = false; @@ -2477,7 +2500,7 @@ exports.motorAction = async (req, reply) => { { $set: { "connections.inputConnections.$.motor_stop_status": "2", "connections.inputConnections.$.manual_threshold_time": manual_threshold_time, "connections.inputConnections.$.threshold_type": "time", - "connections.inputConnections.$.motor_on_type": motorOnType } } + "connections.inputConnections.$.motor_on_type": "manual" } } ); reply.code(200).send({ message: "Motor started successfully." });