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}`);
// 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(`Current time: ${currentTime}`);
console.log(`No valid FCM tokens for customer ID: ${customerId}`);
continue;
}
// Get tanks associated with the user // // Get all users who have allowed notifications and have set a notification time
const tanks = await Tank.find({ customerId }); // const users = await User.find({ lowWaterAlert: true });
for (const tank of tanks) { // if (users.length === 0) {
const { // console.log("No users to notify at this time.");
tankName, // return;
tankLocation, // }
typeOfWater,
capacity,
waterlevel,
waterlevel_at_midnight,
} = tank;
// Remove commas before parsing numbers // for (const user of users) {
const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; // const { customerId, fcmIds } = user;
const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0;
if (tankCapacity === 0) { // if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
console.log(`Skipping tank ${tankName} due to zero capacity`); // console.log(`No valid FCM tokens for customer ID: ${customerId}`);
continue; // 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% // const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
if (currentWaterLevelPercentage < 15) {
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, "Low Water Level Alert", notificationBody); // // Send notification only if water level is below 15%
console.log("Notification sent for tank:", tankName); // if (currentWaterLevelPercentage < 15) {
} else { // let notificationBody =
console.log(`Skipping notification for tank ${tankName}, water level is above 15%`); // `🛢️ 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 () => { // }
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 critical low water notifications // console.log("Water level notifications processed.");
const users = await User.find({ criticalLowWaterAlert: true }); // } 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(`Current time: ${currentTime}`);
console.log("No users to notify at this time.");
return;
}
for (const user of users) { // // Get all users who have allowed critical low water notifications
const { customerId, fcmIds } = user; // const users = await User.find({ criticalLowWaterAlert: true });
if (!Array.isArray(fcmIds) || fcmIds.length === 0) { // if (users.length === 0) {
console.log(`No valid FCM tokens for customer ID: ${customerId}`); // console.log("No users to notify at this time.");
continue; // return;
} // }
// Get tanks associated with the user // for (const user of users) {
const tanks = await Tank.find({ customerId }); // const { customerId, fcmIds } = user;
for (const tank of tanks) { // if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
const { // console.log(`No valid FCM tokens for customer ID: ${customerId}`);
tankName, // continue;
tankLocation, // }
typeOfWater,
capacity,
waterlevel,
} = tank;
// Remove commas before parsing numbers // // Get tanks associated with the user
const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; // const tanks = await Tank.find({ customerId });
const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0;
// 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) { // const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
console.log(`Skipping tank ${tankName} due to zero capacity`);
continue;
}
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% // await sendNotification(customerId, fcmIds, "Critical Low Water Level Alert", notificationBody);
if (currentWaterLevelPercentage < 10) { // console.log(`Critical low water level notification sent for tank: ${tankName}`);
let notificationBody = // } else {
`🚨 *Critical Low Water Alert!*\n\n` + // console.log(`Skipping tank ${tankName}, water level is above 10%`);
`🛢️ *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%`);
}
}
}
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