master^2
Bhaskar 8 months ago
parent 5d3017807c
commit 0fef71392c

@ -7154,143 +7154,143 @@ const calculateWaterLevelAndNotify = async () => {
console.error("Error in water level calculation:", err); console.error("Error in water level calculation:", err);
} }
}; };
const calculateLowWaterLevelAndNotify = async () => { // const calculateLowWaterLevelAndNotify = async () => {
try { // try {
const now = moment(); // const now = moment();
const currentTime = now.format("HH:mm"); // Current time in HH:mm format // 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 notifications and have set a notification time
const users = await User.find({ lowWaterAlert: true }); // const users = await User.find({ lowWaterAlert: 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, // 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;
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);
// Send notification only if water level is below 15% // // Send notification only if water level is below 15%
if (currentWaterLevelPercentage < 15) { // if (currentWaterLevelPercentage < 15) {
let notificationBody = // let notificationBody =
`🛢️ Tank Name: ${tankName}\n` + // `🛢️ Tank Name: ${tankName}\n` +
`🏢 Location: ${tankLocation}\n` + // `🏢 Location: ${tankLocation}\n` +
`💧 Type of Water: ${typeOfWater}\n` + // `💧 Type of Water: ${typeOfWater}\n` +
`Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`; // `Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`;
await sendNotification(customerId, fcmIds, "Low Water Level Alert", notificationBody); // await sendNotification(customerId, fcmIds, "Low Water Level Alert", notificationBody);
console.log("Notification sent for tank:", tankName); // console.log("Notification sent for tank:", tankName);
} else { // } else {
console.log(`Skipping notification for tank ${tankName}, water level is above 15%`); // console.log(`Skipping notification for tank ${tankName}, water level is above 15%`);
} // }
} // }
} // }
console.log("Water level notifications processed."); // console.log("Water level notifications processed.");
} catch (err) { // } catch (err) {
console.error("Error in water level calculation:", err); // console.error("Error in water level calculation:", err);
} // }
}; // };
const calculateCriticalLowWaterLevelAndNotify = async () => { // const calculateCriticalLowWaterLevelAndNotify = async () => {
try { // try {
const now = moment(); // const now = moment();
const currentTime = now.format("HH:mm"); // Current time in HH:mm format // 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 critical low water notifications // // 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,
} = 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;
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);
// Send notification only if water level is below 10% // // Send notification only if water level is below 10%
if (currentWaterLevelPercentage < 10) { // if (currentWaterLevelPercentage < 10) {
let notificationBody = // let notificationBody =
`🚨 *Critical Low Water Alert!*\n\n` + // `🚨 *Critical Low Water Alert!*\n\n` +
`🛢️ *Tank Name:* ${tankName}\n` + // `🛢️ *Tank Name:* ${tankName}\n` +
`🏢 *Location:* ${tankLocation}\n` + // `🏢 *Location:* ${tankLocation}\n` +
`💧 *Type of Water:* ${typeOfWater}\n` + // `💧 *Type of Water:* ${typeOfWater}\n` +
`🔴 *Current Water Level:* ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n\n` + // `🔴 *Current Water Level:* ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n\n` +
`⚠️ Immediate action is recommended to avoid water shortage.`; // `⚠️ Immediate action is recommended to avoid water shortage.`;
await sendNotification(customerId, fcmIds, "Critical Low Water Level Alert", notificationBody); // await sendNotification(customerId, fcmIds, "Critical Low Water Level Alert", notificationBody);
console.log(`Critical low water level notification sent for tank: ${tankName}`); // console.log(`Critical low water level notification sent for tank: ${tankName}`);
} else { // } else {
console.log(`Skipping tank ${tankName}, water level is above 10%`); // console.log(`Skipping tank ${tankName}, water level is above 10%`);
} // }
} // }
} // }
console.log("Critical low water level notifications processed."); // console.log("Critical low water level notifications processed.");
} catch (err) { // } catch (err) {
console.error("Error in critical water level calculation:", err); // console.error("Error in critical water level calculation:", err);
} // }
}; // };
@ -7303,13 +7303,13 @@ cron.schedule('* * * * *', async () => {
}); });
//run the every one hour //run the every one hour
cron.schedule('0 */3 * * *', async () => { // cron.schedule('0 */3 * * *', 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

Loading…
Cancel
Save