From 0bc54f6b07ee899198a27261e452bfef865e1354 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 25 Feb 2025 10:09:53 +0530 Subject: [PATCH] low water notification comment --- src/controllers/tanksController.js | 238 ++++++++++++++--------------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b8e62aac..9b3a9bef 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7155,139 +7155,139 @@ const calculateWaterLevelAndNotify = async () => { } }; -const calculateLowWaterLevelAndNotify = async () => { - try { - const now = moment(); - const currentTime = now.format("HH:mm"); // Current time in HH:mm format - - console.log(`Current time: ${currentTime}`); - - // Get all users who have allowed notifications and have set a notification time - const users = await User.find({ lowWaterAlert: true}); - - if (users.length === 0) { - console.log("No users to notify at this time."); - return; - } - - for (const user of users) { - const { customerId, fcmIds } = user; - - if (!Array.isArray(fcmIds) || fcmIds.length === 0) { - console.log(`No valid FCM tokens for customer ID: ${customerId}`); - continue; - } +// const calculateLowWaterLevelAndNotify = async () => { +// try { +// const now = moment(); +// const currentTime = now.format("HH:mm"); // Current time in HH:mm format - // Get tanks associated with the user - const tanks = await Tank.find({ customerId }); +// console.log(`Current time: ${currentTime}`); - for (const tank of tanks) { - const { - tankName, - tankLocation, - typeOfWater, - capacity, - waterlevel, - waterlevel_at_midnight, - } = tank; +// // Get all users who have allowed notifications and have set a notification time +// const users = await User.find({ lowWaterAlert: true}); - // Remove commas before parsing numbers - const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; - const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; - const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; - - if (tankCapacity === 0) { - console.log(`Skipping tank ${tankName} due to zero capacity`); - continue; - } +// if (users.length === 0) { +// console.log("No users to notify at this time."); +// return; +// } - const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); - const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; - const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); +// for (const user of users) { +// const { customerId, fcmIds } = user; - let notificationBody = - `🛢️ Tank Name: ${tankName}\n` + - `🏢 Location: ${tankLocation}\n` + - `💧 Type of Water: ${typeOfWater}\n` + - `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; +// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { +// console.log(`No valid FCM tokens for customer ID: ${customerId}`); +// continue; +// } - await sendNotification(customerId, fcmIds, "Low Water Level Update", notificationBody); - console.log("Notification sent for tank:", tankName); - } - } +// // Get tanks associated with the user +// const tanks = await Tank.find({ customerId }); + +// for (const tank of tanks) { +// const { +// tankName, +// tankLocation, +// typeOfWater, +// capacity, +// waterlevel, +// waterlevel_at_midnight, +// } = tank; + +// // Remove commas before parsing numbers +// const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; +// const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; +// const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; + +// if (tankCapacity === 0) { +// console.log(`Skipping tank ${tankName} due to zero capacity`); +// continue; +// } - console.log("Water level notifications processed."); - } catch (err) { - console.error("Error in water level calculation:", err); - } -}; +// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); +// const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; +// const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); -const calculateCriticalLowWaterLevelAndNotify = async () => { - try { - const now = moment(); - const currentTime = now.format("HH:mm"); // Current time in HH:mm format +// let notificationBody = +// `🛢️ Tank Name: ${tankName}\n` + +// `🏢 Location: ${tankLocation}\n` + +// `💧 Type of Water: ${typeOfWater}\n` + +// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; - console.log(`Current time: ${currentTime}`); +// await sendNotification(customerId, fcmIds, "Low Water Level Update", notificationBody); +// console.log("Notification sent for tank:", tankName); +// } +// } - // Get all users who have allowed notifications and have set a notification time - const users = await User.find({ criticalLowWaterAlert: true}); +// console.log("Water level notifications processed."); +// } catch (err) { +// console.error("Error in water level calculation:", err); +// } +// }; - if (users.length === 0) { - console.log("No users to notify at this time."); - return; - } +// const calculateCriticalLowWaterLevelAndNotify = async () => { +// try { +// const now = moment(); +// const currentTime = now.format("HH:mm"); // Current time in HH:mm format - for (const user of users) { - const { customerId, fcmIds } = user; +// console.log(`Current time: ${currentTime}`); - if (!Array.isArray(fcmIds) || fcmIds.length === 0) { - console.log(`No valid FCM tokens for customer ID: ${customerId}`); - continue; - } +// // Get all users who have allowed notifications and have set a notification time +// const users = await User.find({ criticalLowWaterAlert: true}); - // Get tanks associated with the user - const tanks = await Tank.find({ customerId }); +// if (users.length === 0) { +// console.log("No users to notify at this time."); +// return; +// } - for (const tank of tanks) { - const { - tankName, - tankLocation, - typeOfWater, - capacity, - waterlevel, - waterlevel_at_midnight, - } = tank; +// for (const user of users) { +// const { customerId, fcmIds } = user; - // Remove commas before parsing numbers - const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; - const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; - const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; +// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { +// console.log(`No valid FCM tokens for customer ID: ${customerId}`); +// continue; +// } - if (tankCapacity === 0) { - console.log(`Skipping tank ${tankName} due to zero capacity`); - continue; - } +// // Get tanks associated with the user +// const tanks = await Tank.find({ customerId }); + +// for (const tank of tanks) { +// const { +// tankName, +// tankLocation, +// typeOfWater, +// capacity, +// waterlevel, +// waterlevel_at_midnight, +// } = tank; + +// // Remove commas before parsing numbers +// const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; +// const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; +// const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; + +// if (tankCapacity === 0) { +// console.log(`Skipping tank ${tankName} due to zero capacity`); +// continue; +// } - const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); - const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; - const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); +// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); +// const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; +// const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); - let notificationBody = - `🛢️ Tank Name: ${tankName}\n` + - `🏢 Location: ${tankLocation}\n` + - `💧 Type of Water: ${typeOfWater}\n` + - `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; +// let notificationBody = +// `🛢️ Tank Name: ${tankName}\n` + +// `🏢 Location: ${tankLocation}\n` + +// `💧 Type of Water: ${typeOfWater}\n` + +// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; - await sendNotification(customerId, fcmIds, "Critical Low Water Level Update", notificationBody); - console.log("Notification sent for tank:", tankName); - } - } +// await sendNotification(customerId, fcmIds, "Critical Low Water Level Update", notificationBody); +// console.log("Notification sent for tank:", tankName); +// } +// } - console.log("Water level notifications processed."); - } catch (err) { - console.error("Error in water level calculation:", err); - } -}; +// console.log("Water level notifications processed."); +// } catch (err) { +// console.error("Error in water level calculation:", err); +// } +// }; // Run the function every minute to check if any user needs a notification @@ -7299,13 +7299,13 @@ cron.schedule('* * * * *', async () => { }); //run the every one hour -cron.schedule('0 * * * *', async () => { - console.log("Checking for user notification times..."); - await calculateLowWaterLevelAndNotify(); - await calculateCriticalLowWaterLevelAndNotify(); -}, { - timezone: "Asia/Kolkata", -}); +// cron.schedule('0 * * * *', async () => { +// console.log("Checking for user notification times..."); +// await calculateLowWaterLevelAndNotify(); +// await calculateCriticalLowWaterLevelAndNotify(); +// }, { +// timezone: "Asia/Kolkata", +// }); // Schedule notifications at 6 AM, 12 PM, 6 PM, and 12 AM // cron.schedule(