|
|
|
|
@ -6908,27 +6908,23 @@ const calculateConsumptionAndNotify = async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.sendUserSetNotifications = async (request, reply) => {
|
|
|
|
|
const { customerId, notificationTime } = request.body;
|
|
|
|
|
const { customerId, notificationTime, allowNotifications } = request.body;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const user = await User.findOneAndUpdate(
|
|
|
|
|
{ customerId },
|
|
|
|
|
{ notificationTime },
|
|
|
|
|
{ new: true, upsert: true } // Create if not exists
|
|
|
|
|
{ notificationTime, allowNotifications },
|
|
|
|
|
{ new: true, upsert: true } // Create user if not exists
|
|
|
|
|
);
|
|
|
|
|
await calculateWaterLevelAndNotify(); // This will only notify if notifications are allowed
|
|
|
|
|
|
|
|
|
|
// Optionally, call the notification calculation function after setting the time
|
|
|
|
|
// if (allowNotifications) {
|
|
|
|
|
// await calculateWaterLevelAndNotify(); // This will only notify if notifications are allowed
|
|
|
|
|
// }
|
|
|
|
|
console.log(`User ${customerId} updated: Notification Time - ${notificationTime}, Allowed - ${allowNotifications}`);
|
|
|
|
|
|
|
|
|
|
return reply.send({ success: true, user });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error setting notification time:", error);
|
|
|
|
|
return reply.status(500).send({ success: false, message: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
// try {
|
|
|
|
|
@ -6984,6 +6980,7 @@ exports.sendUserSetNotifications = async (request, reply) => {
|
|
|
|
|
// console.error("Error in water level calculation:", err);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const now = moment();
|
|
|
|
|
@ -6991,15 +6988,22 @@ const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
|
|
|
|
|
console.log(`Current time: ${currentTime}`);
|
|
|
|
|
|
|
|
|
|
// Get all users who have allowed notifications
|
|
|
|
|
const users = await User.find({ });
|
|
|
|
|
// Get all users who have allowed notifications and have set a notification time
|
|
|
|
|
const users = await User.find({ allowNotifications: true, notificationTime: currentTime });
|
|
|
|
|
|
|
|
|
|
if (users.length === 0) {
|
|
|
|
|
console.log("No users to notify at this time.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Iterate over each user
|
|
|
|
|
for (const user of users) {
|
|
|
|
|
const { customerId, notificationTime, fcmIds } = user;
|
|
|
|
|
const { customerId, fcmIds } = user;
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(fcmIds) || fcmIds.length === 0) {
|
|
|
|
|
console.log(`No valid FCM tokens for customer ID: ${customerId}`);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the user's notification time matches the current time
|
|
|
|
|
if (currentTime === notificationTime) {
|
|
|
|
|
// Get tanks associated with the user
|
|
|
|
|
const tanks = await Tank.find({ customerId });
|
|
|
|
|
|
|
|
|
|
@ -7037,7 +7041,6 @@ const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
console.log("Notification sent for tank:", tankName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("Water level notifications processed.");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
@ -7045,6 +7048,13 @@ const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Run the function every minute to check if any user needs a notification
|
|
|
|
|
cron.schedule('* * * * *', async () => {
|
|
|
|
|
console.log("Checking for user notification times...");
|
|
|
|
|
await calculateWaterLevelAndNotify();
|
|
|
|
|
}, {
|
|
|
|
|
timezone: "Asia/Kolkata",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -7062,12 +7072,12 @@ const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// Schedule a function to run every minute
|
|
|
|
|
cron.schedule('* * * * *', async () => {
|
|
|
|
|
console.log("Checking for user notification times...");
|
|
|
|
|
await calculateWaterLevelAndNotify();
|
|
|
|
|
}, {
|
|
|
|
|
timezone: "Asia/Kolkata", // Specify the timezone
|
|
|
|
|
});
|
|
|
|
|
// cron.schedule('* * * * *', async () => {
|
|
|
|
|
// console.log("Checking for user notification times...");
|
|
|
|
|
// await calculateWaterLevelAndNotify();
|
|
|
|
|
// }, {
|
|
|
|
|
// timezone: "Asia/Kolkata", // Specify the timezone
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// const updateStopTimeFormat = async () => {
|
|
|
|
|
// try {
|
|
|
|
|
|