From 0fef71392c1a6aea2c02b48bc37fb097b0e020ad Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 25 Feb 2025 12:02:54 +0530 Subject: [PATCH] commit --- src/controllers/tanksController.js | 248 ++++++++++++++--------------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 13da76bf..20f73320 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7154,143 +7154,143 @@ const calculateWaterLevelAndNotify = async () => { console.error("Error in water level calculation:", err); } }; -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; +// const calculateLowWaterLevelAndNotify = async () => { +// try { +// const now = moment(); +// const currentTime = now.format("HH:mm"); // Current time in HH:mm format - if (!Array.isArray(fcmIds) || fcmIds.length === 0) { - console.log(`No valid FCM tokens for customer ID: ${customerId}`); - continue; - } +// console.log(`Current time: ${currentTime}`); - // Get tanks associated with the user - const tanks = await Tank.find({ customerId }); +// // Get all users who have allowed notifications and have set a notification time +// const users = await User.find({ lowWaterAlert: true }); - for (const tank of tanks) { - const { - tankName, - tankLocation, - typeOfWater, - capacity, - waterlevel, - waterlevel_at_midnight, - } = tank; +// if (users.length === 0) { +// console.log("No users to notify at this time."); +// return; +// } - // Remove commas before parsing numbers - const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; - const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; +// for (const user of users) { +// const { customerId, fcmIds } = user; - if (tankCapacity === 0) { - console.log(`Skipping tank ${tankName} due to zero capacity`); - continue; - } +// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { +// console.log(`No valid FCM tokens for customer ID: ${customerId}`); +// continue; +// } - const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); +// // 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; + +// if (tankCapacity === 0) { +// console.log(`Skipping tank ${tankName} due to zero capacity`); +// continue; +// } - // Send notification only if water level is below 15% - if (currentWaterLevelPercentage < 15) { - let notificationBody = - `🛢️ Tank Name: ${tankName}\n` + - `🏢 Location: ${tankLocation}\n` + - `💧 Type of Water: ${typeOfWater}\n` + - `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; +// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); - await sendNotification(customerId, fcmIds, "Low Water Level Alert", notificationBody); - console.log("Notification sent for tank:", tankName); - } else { - console.log(`Skipping notification for tank ${tankName}, water level is above 15%`); - } - } - } +// // Send notification only if water level is below 15% +// if (currentWaterLevelPercentage < 15) { +// let notificationBody = +// `🛢️ Tank Name: ${tankName}\n` + +// `🏢 Location: ${tankLocation}\n` + +// `💧 Type of Water: ${typeOfWater}\n` + +// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; - console.log("Water level notifications processed."); - } catch (err) { - console.error("Error in water level calculation:", err); - } -}; -const calculateCriticalLowWaterLevelAndNotify = async () => { - try { - const now = moment(); - const currentTime = now.format("HH:mm"); // Current time in HH:mm format - - console.log(`Current time: ${currentTime}`); +// await sendNotification(customerId, fcmIds, "Low Water Level Alert", notificationBody); +// console.log("Notification sent for tank:", tankName); +// } else { +// console.log(`Skipping notification for tank ${tankName}, water level is above 15%`); +// } +// } +// } - // Get all users who have allowed critical low water notifications - const users = await User.find({ criticalLowWaterAlert: true }); +// console.log("Water level notifications processed."); +// } catch (err) { +// console.error("Error in water level calculation:", err); +// } +// }; +// const calculateCriticalLowWaterLevelAndNotify = async () => { +// try { +// const now = moment(); +// const currentTime = now.format("HH:mm"); // Current time in HH:mm format - if (users.length === 0) { - console.log("No users to notify at this time."); - return; - } +// console.log(`Current time: ${currentTime}`); - for (const user of users) { - const { customerId, fcmIds } = user; +// // Get all users who have allowed critical low water notifications +// const users = await User.find({ criticalLowWaterAlert: true }); - if (!Array.isArray(fcmIds) || fcmIds.length === 0) { - console.log(`No valid FCM tokens for customer ID: ${customerId}`); - continue; - } +// if (users.length === 0) { +// console.log("No users to notify at this time."); +// return; +// } - // Get tanks associated with the user - const tanks = await Tank.find({ customerId }); +// for (const user of users) { +// const { customerId, fcmIds } = user; - for (const tank of tanks) { - const { - tankName, - tankLocation, - typeOfWater, - capacity, - waterlevel, - } = tank; +// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { +// console.log(`No valid FCM tokens for customer ID: ${customerId}`); +// continue; +// } - // Remove commas before parsing numbers - const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; - const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; +// // Get tanks associated with the user +// const tanks = await Tank.find({ customerId }); + +// for (const tank of tanks) { +// const { +// tankName, +// tankLocation, +// typeOfWater, +// capacity, +// waterlevel, +// } = tank; + +// // Remove commas before parsing numbers +// const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; +// const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; + +// if (tankCapacity === 0) { +// console.log(`Skipping tank ${tankName} due to zero capacity`); +// continue; +// } - if (tankCapacity === 0) { - console.log(`Skipping tank ${tankName} due to zero capacity`); - continue; - } +// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); - const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); +// // Send notification only if water level is below 10% +// if (currentWaterLevelPercentage < 10) { +// let notificationBody = +// `🚨 *Critical Low Water Alert!*\n\n` + +// `🛢️ *Tank Name:* ${tankName}\n` + +// `🏢 *Location:* ${tankLocation}\n` + +// `💧 *Type of Water:* ${typeOfWater}\n` + +// `🔴 *Current Water Level:* ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n\n` + +// `⚠️ Immediate action is recommended to avoid water shortage.`; - // Send notification only if water level is below 10% - if (currentWaterLevelPercentage < 10) { - let notificationBody = - `🚨 *Critical Low Water Alert!*\n\n` + - `🛢️ *Tank Name:* ${tankName}\n` + - `🏢 *Location:* ${tankLocation}\n` + - `💧 *Type of Water:* ${typeOfWater}\n` + - `🔴 *Current Water Level:* ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n\n` + - `⚠️ Immediate action is recommended to avoid water shortage.`; - - await sendNotification(customerId, fcmIds, "Critical Low Water Level Alert", notificationBody); - console.log(`Critical low water level notification sent for tank: ${tankName}`); - } else { - console.log(`Skipping tank ${tankName}, water level is above 10%`); - } - } - } +// await sendNotification(customerId, fcmIds, "Critical Low Water Level Alert", notificationBody); +// console.log(`Critical low water level notification sent for tank: ${tankName}`); +// } else { +// console.log(`Skipping tank ${tankName}, water level is above 10%`); +// } +// } +// } - console.log("Critical low water level notifications processed."); - } catch (err) { - console.error("Error in critical water level calculation:", err); - } -}; +// console.log("Critical low water level notifications processed."); +// } catch (err) { +// console.error("Error in critical water level calculation:", err); +// } +// }; @@ -7303,13 +7303,13 @@ cron.schedule('* * * * *', async () => { }); //run the every one hour -cron.schedule('0 */3 * * *', async () => { - console.log("Checking for user notification times..."); - await calculateLowWaterLevelAndNotify(); - await calculateCriticalLowWaterLevelAndNotify(); -}, { - timezone: "Asia/Kolkata", -}); +// cron.schedule('0 */3 * * *', 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