ashok 10 months ago
commit 217a1aa89e

@ -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 {

@ -1328,32 +1328,22 @@ module.exports = function (fastify, opts, next) {
url: "/api/sendNotificationDaily",
schema: {
tags: ["Tank"],
summary: "This is for time based notification",
summary: "This is for time-based notification",
body: {
type: "object",
properties: {
customerId: {
type: "string",
},
notificationTime: {
type: "string",
},
// allowNotifications: {
// type: "boolean"
// }
customerId: { type: "string" },
notificationTime: { type: "string" },
allowNotifications: { type: "boolean" },
},
required: ["customerId", "notificationTime", "allowNotifications"], // Ensures all fields are required
},
security: [
{
basicAuth: [],
},
],
security: [{ basicAuth: [] }],
},
//preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.sendUserSetNotifications,
});
fastify.route({
method: "POST",
url: "/api/sendNotificationDailyPreference",

Loading…
Cancel
Save