From 72a278ae6c9413cbb01a8d080b928cb6abe5f9c2 Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 27 Jan 2025 15:25:32 +0530 Subject: [PATCH 1/3] changes --- src/controllers/tanksController.js | 46 +++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b9297aa3..af3dbde7 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1065,7 +1065,13 @@ 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 = []; @@ -1087,7 +1093,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) => { @@ -1102,11 +1109,10 @@ exports.consumption = async (request, reply) => { 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 - 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 +1133,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 +1147,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 +1158,19 @@ 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, + }), }; reply.send(response); @@ -1163,6 +1180,7 @@ exports.consumption = async (request, reply) => { }; + exports.consumptiondatewiseofalltanks = async (request, reply) => { try { 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 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; @@ -2402,7 +2420,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" } } ); // await Tank.updateOne( From a7488d4236b261eb7046b1938d0774ccc1addcbb Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 27 Jan 2025 15:29:57 +0530 Subject: [PATCH 2/3] changes --- src/controllers/tanksController.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 4b0b8e4c..d0926021 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; @@ -1622,15 +1623,15 @@ eventEmitter.on( console.log("users", users); const userNames = users.map(user => user.username).join(', '); console.log("userNames", userNames); - const startMethod = motorOnType === "APP" ? "via the App" : "Manual"; + const startMethod = motorOnType.toUpperCase() === "APP" ? "via the App" : "Manual"; // Prepare the message const message = - `🚰 Tank Name: '${tankName}'\n` + - `🕒 Pump started at: '${startTime}'\n` + - `👤 Initiated by : ${userNames}\n` + + `Tank Name: '${tankName}'\n` + + `Pump started at: '${startTime}'\n` + + `Initiated by : ${userNames}\n` + // `Pump started by: '${motorOnType.toUpperCase()}'\n` + - `🔄 Pump started by: '${startMethod}'\n` + + `Pump started by: '${startMethod}'\n` + `Will stop at after: '${manual_threshold_time}' mins`; // Send the notification @@ -1650,14 +1651,14 @@ eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) const userNames = users.map(user => user.username).join(', '); console.log("userNames",userNames) - const stopMethod = motorOnType === "APP" ? "via the App" : "manual"; + const stopMethod = motorOnType.toUpperCase() === "APP" ? "via the App" : "manual"; // Prepare the message // const message = `Tank Name: '${tankName}', Pump stopped at '${stopTime}' by Initiated by user(s): ${userNames} '${motorOnType}'`; const message = - `🚰 Tank Name: '${tankName}'\n` + - `🕒 Pump stopped at: '${stopTime}'\n` + - `👤 Initiated by : ${userNames}\n` + + `Tank Name: '${tankName}'\n` + + `Pump stopped at: '${stopTime}'\n` + + `Initiated by : ${userNames}\n` + // `Motor Off Type: '${motorOnType}'`; `Motor Off Type: '${stopMethod}'`; @@ -1755,7 +1756,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 = @@ -1783,14 +1784,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); @@ -2410,7 +2411,7 @@ exports.motorAction = async (req, reply) => { blockName, tankName, startTime, - "APP", + motorOnType, manual_threshold_time ); notificationSentStatus.motorStart = true; // Set flag to true to prevent duplicate notifications @@ -2458,7 +2459,7 @@ exports.motorAction = async (req, reply) => { fcmToken, tankName, stopTime, - "APP" + motorOnType ); notificationSentStatus.motorStop = true; // Set flag to true to prevent duplicate notifications } From edeae05743e62b57a91be3bb948e36a50568cccc Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 27 Jan 2025 15:46:59 +0530 Subject: [PATCH 3/3] changes --- src/controllers/tanksController.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d0926021..70c4ad46 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1077,6 +1077,7 @@ exports.consumption = async (request, reply) => { const tanks = await Tank.find(tankQuery); const tankData = []; const tankconsumptionData = []; + let totalConsumptionForSelectedBlockAndTypeOfWater = 0; let totalBoreConsumptionForSelectedBlockAndTypeOfWater = 0; let totalBoreCapacityForSelectedBlockAndTypeOfWater = 0; let totalDrinkingConsumptionForSelectedBlockAndTypeOfWater = 0; @@ -1108,7 +1109,8 @@ 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" || tank.typeOfWater === "Bore Water") { totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption; @@ -1171,7 +1173,9 @@ exports.consumption = async (request, reply) => { typeofwater === "bore" ? totalBoreConsumptionForSelectedBlockAndTypeOfWater : totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, - }), + } + ), + [`total consumption of ${typeofwater} and selected block`]: totalConsumptionForSelectedBlockAndTypeOfWater }; reply.send(response);