low and critical low notification

master^2
Bhaskar 9 months ago
parent 22983aea4e
commit cc6ed915c2

@ -7154,140 +7154,144 @@ const calculateWaterLevelAndNotify = async () => {
console.error("Error in water level calculation:", err); 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
// const calculateLowWaterLevelAndNotify = async () => { console.log(`Current time: ${currentTime}`);
// 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) { // Get all users who have allowed notifications and have set a notification time
// console.log("No users to notify at this time."); const users = await User.find({ lowWaterAlert: true });
// return;
// }
// for (const user of users) { if (users.length === 0) {
// const { customerId, fcmIds } = user; console.log("No users to notify at this time.");
return;
}
// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { for (const user of users) {
// console.log(`No valid FCM tokens for customer ID: ${customerId}`); const { customerId, fcmIds } = user;
// continue;
// }
// // Get tanks associated with the user if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
// const tanks = await Tank.find({ customerId }); console.log(`No valid FCM tokens for customer ID: ${customerId}`);
continue;
}
// for (const tank of tanks) { // Get tanks associated with the user
// const { const tanks = await Tank.find({ customerId });
// tankName,
// tankLocation,
// typeOfWater,
// capacity,
// waterlevel,
// waterlevel_at_midnight,
// } = tank;
// // Remove commas before parsing numbers for (const tank of tanks) {
// const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; const {
// const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; tankName,
// const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; tankLocation,
typeOfWater,
capacity,
waterlevel,
waterlevel_at_midnight,
} = tank;
// if (tankCapacity === 0) { // Remove commas before parsing numbers
// console.log(`Skipping tank ${tankName} due to zero capacity`); const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0;
// continue; const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0;
// }
// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); if (tankCapacity === 0) {
// const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; console.log(`Skipping tank ${tankName} due to zero capacity`);
// const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); continue;
}
// let notificationBody = const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
// `🛢️ Tank Name: ${tankName}\n` +
// `🏢 Location: ${tankLocation}\n` +
// `💧 Type of Water: ${typeOfWater}\n` +
// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`;
// await sendNotification(customerId, fcmIds, "Low Water Level Update", notificationBody); // Send notification only if water level is below 15%
// console.log("Notification sent for tank:", tankName); 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."); await sendNotification(customerId, fcmIds, "Low Water Level Alert", notificationBody);
// } catch (err) { console.log("Notification sent for tank:", tankName);
// console.error("Error in water level calculation:", err); } else {
// } console.log(`Skipping notification for tank ${tankName}, water level is above 15%`);
// }; }
}
}
// const calculateCriticalLowWaterLevelAndNotify = async () => { console.log("Water level notifications processed.");
// try { } catch (err) {
// const now = moment(); console.error("Error in water level calculation:", err);
// const currentTime = now.format("HH:mm"); // Current time in HH:mm format }
};
const calculateCriticalLowWaterLevelAndNotify = async () => {
try {
const now = moment();
const currentTime = now.format("HH:mm"); // Current time in HH:mm format
// console.log(`Current time: ${currentTime}`); console.log(`Current time: ${currentTime}`);
// // Get all users who have allowed notifications and have set a notification time // Get all users who have allowed critical low water notifications
// const users = await User.find({ criticalLowWaterAlert: true}); const users = await User.find({ criticalLowWaterAlert: true });
// if (users.length === 0) { if (users.length === 0) {
// console.log("No users to notify at this time."); console.log("No users to notify at this time.");
// return; return;
// } }
// for (const user of users) { for (const user of users) {
// const { customerId, fcmIds } = user; const { customerId, fcmIds } = user;
// if (!Array.isArray(fcmIds) || fcmIds.length === 0) { if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
// console.log(`No valid FCM tokens for customer ID: ${customerId}`); console.log(`No valid FCM tokens for customer ID: ${customerId}`);
// continue; continue;
// } }
// // Get tanks associated with the user // Get tanks associated with the user
// const tanks = await Tank.find({ customerId }); const tanks = await Tank.find({ customerId });
// for (const tank of tanks) { for (const tank of tanks) {
// const { const {
// tankName, tankName,
// tankLocation, tankLocation,
// typeOfWater, typeOfWater,
// capacity, capacity,
// waterlevel, waterlevel,
// waterlevel_at_midnight, } = tank;
// } = tank;
// // Remove commas before parsing numbers // Remove commas before parsing numbers
// const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0;
// const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0;
// const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0;
// if (tankCapacity === 0) { if (tankCapacity === 0) {
// console.log(`Skipping tank ${tankName} due to zero capacity`); console.log(`Skipping tank ${tankName} due to zero capacity`);
// continue; continue;
// } }
// const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2); const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
// const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel;
// const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2);
// let notificationBody = // Send notification only if water level is below 10%
// `🛢️ Tank Name: ${tankName}\n` + if (currentWaterLevelPercentage < 10) {
// `🏢 Location: ${tankLocation}\n` + let notificationBody =
// `💧 Type of Water: ${typeOfWater}\n` + `🚨 *Critical Low Water Alert!*\n\n` +
// `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\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 Update", notificationBody); console.log("Critical low water level notifications processed.");
// console.log("Notification sent for tank:", tankName); } catch (err) {
// } console.error("Error in critical 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 // Run the function every minute to check if any user needs a notification
@ -7299,13 +7303,13 @@ cron.schedule('* * * * *', async () => {
}); });
//run the every one hour //run the every one hour
// cron.schedule('0 * * * *', async () => { cron.schedule('0 * * * *', async () => {
// console.log("Checking for user notification times..."); console.log("Checking for user notification times...");
// await calculateLowWaterLevelAndNotify(); await calculateLowWaterLevelAndNotify();
// await calculateCriticalLowWaterLevelAndNotify(); await calculateCriticalLowWaterLevelAndNotify();
// }, { }, {
// timezone: "Asia/Kolkata", timezone: "Asia/Kolkata",
// }); });
// Schedule notifications at 6 AM, 12 PM, 6 PM, and 12 AM // Schedule notifications at 6 AM, 12 PM, 6 PM, and 12 AM
// cron.schedule( // cron.schedule(

Loading…
Cancel
Save