ashok 9 months ago
commit 1d99176494

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

Loading…
Cancel
Save