ashok 9 months ago
commit 1d99176494

@ -7155,139 +7155,139 @@ const calculateWaterLevelAndNotify = async () => {
} }
}; };
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(`No valid FCM tokens for customer ID: ${customerId}`);
continue;
}
// Get tanks associated with the user // console.log(`Current time: ${currentTime}`);
const tanks = await Tank.find({ customerId });
for (const tank of tanks) { // // Get all users who have allowed notifications and have set a notification time
const { // const users = await User.find({ lowWaterAlert: true});
tankName,
tankLocation,
typeOfWater,
capacity,
waterlevel,
waterlevel_at_midnight,
} = tank;
// Remove commas before parsing numbers // if (users.length === 0) {
const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; // console.log("No users to notify at this time.");
const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; // return;
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); // for (const user of users) {
const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; // const { customerId, fcmIds } = user;
const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2);
let notificationBody = // if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
`🛢️ Tank Name: ${tankName}\n` + // console.log(`No valid FCM tokens for customer ID: ${customerId}`);
`🏢 Location: ${tankLocation}\n` + // continue;
`💧 Type of Water: ${typeOfWater}\n` + // }
`Current Water Level: ${currentWaterLevel} liters (${currentWaterLevelPercentage}%)\n`;
await sendNotification(customerId, fcmIds, "Low Water Level Update", notificationBody); // // Get tanks associated with the user
console.log("Notification sent for tank:", tankName); // 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."); // const currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
} catch (err) { // const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel;
console.error("Error in water level calculation:", err); // const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2);
}
};
const calculateCriticalLowWaterLevelAndNotify = async () => { // let notificationBody =
try { // `🛢️ Tank Name: ${tankName}\n` +
const now = moment(); // `🏢 Location: ${tankLocation}\n` +
const currentTime = now.format("HH:mm"); // Current time in HH:mm format // `💧 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 // console.log("Water level notifications processed.");
const users = await User.find({ criticalLowWaterAlert: true}); // } catch (err) {
// console.error("Error in water level calculation:", err);
// }
// };
if (users.length === 0) { // const calculateCriticalLowWaterLevelAndNotify = async () => {
console.log("No users to notify at this time."); // try {
return; // const now = moment();
} // const currentTime = now.format("HH:mm"); // Current time in HH:mm format
for (const user of users) { // console.log(`Current time: ${currentTime}`);
const { customerId, fcmIds } = user;
if (!Array.isArray(fcmIds) || fcmIds.length === 0) { // // Get all users who have allowed notifications and have set a notification time
console.log(`No valid FCM tokens for customer ID: ${customerId}`); // const users = await User.find({ criticalLowWaterAlert: true});
continue;
}
// Get tanks associated with the user // if (users.length === 0) {
const tanks = await Tank.find({ customerId }); // console.log("No users to notify at this time.");
// return;
// }
for (const tank of tanks) { // for (const user of users) {
const { // const { customerId, fcmIds } = user;
tankName,
tankLocation,
typeOfWater,
capacity,
waterlevel,
waterlevel_at_midnight,
} = tank;
// Remove commas before parsing numbers // if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
const tankCapacity = parseFloat(capacity.replace(/,/g, '')) || 0; // console.log(`No valid FCM tokens for customer ID: ${customerId}`);
const currentWaterLevel = parseFloat(waterlevel.replace(/,/g, '')) || 0; // continue;
const midnightWaterLevel = parseFloat(waterlevel_at_midnight.replace(/,/g, '')) || 0; // }
if (tankCapacity === 0) { // // Get tanks associated with the user
console.log(`Skipping tank ${tankName} due to zero capacity`); // const tanks = await Tank.find({ customerId });
continue;
} // 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 currentWaterLevelPercentage = ((currentWaterLevel / tankCapacity) * 100).toFixed(2);
const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel; // const waterUsedSinceMidnight = midnightWaterLevel - currentWaterLevel;
const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2); // const waterUsedPercentageSinceMidnight = ((waterUsedSinceMidnight / tankCapacity) * 100).toFixed(2);
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, "Critical Low Water Level Update", notificationBody); // await sendNotification(customerId, fcmIds, "Critical Low Water Level Update", notificationBody);
console.log("Notification sent for tank:", tankName); // console.log("Notification sent for tank:", tankName);
} // }
} // }
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);
} // }
}; // };
// 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 +7299,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