diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b980debe..0d7bdcba 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -11,6 +11,8 @@ const cron = require('node-cron'); const moment = require('moment'); const EventEmitter = require('events'); +EventEmitter.defaultMaxListeners = 50; // Increase listener limit + const eventEmitter = new EventEmitter(); async function deleteOldRecords() { const SEVEN_DAYS_IN_MILLISECONDS = 7 * 24 * 60 * 60 * 1000; @@ -2424,7 +2426,11 @@ const sendNotification = async (customerId, fcmIds, title, body) => { data: { target: "/tank_levels" }, }); - console.log(`Notification sent successfully to token: ${token}`, response); + console.log(`Notification sent successfully to token: ${token}`); + console.log("FCM Response:", response); // Log the full response + console.log(`Title: ${title}, Body: ${body}`); // Log title and body before sending + + } catch (error) { console.error(`Failed to send notification to token: ${token}`, error); @@ -6928,4 +6934,17 @@ exports.listofactiveandinactivetankstatus = async (req, reply) => { console.error("Error fetching tank list:", error); return reply.code(500).send({ message: "Internal Server Error" }); } -}; \ No newline at end of file +}; + + +exports.notificationTiming = async (req, reply) => { + const { customerId, notificationPreference } = req.body; + + if (!["never", "always", "6_hours", "8_hours", "1_month"].includes(notificationPreference)) { + return reply.status(400).send({ message: "Invalid preference" }); + } + + await User.updateOne({ customerId }, { notificationPreference }); + + return reply.send({ message: "Preference updated successfully" }); +} \ No newline at end of file diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index d5709a25..41bfde44 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1354,6 +1354,38 @@ module.exports = function (fastify, opts, next) { handler: tanksController.sendUserSetNotifications, }); + fastify.route({ + method: "POST", + url: "/api/sendNotificationDailyPreference", + schema: { + tags: ["Tank"], + summary: "This is for time based notification preferences", + body: { + type: "object", + properties: { + customerId: { + type: "string", + + }, + notificationPreference: { + type: "string", + }, + // allowNotifications: { + // type: "boolean" + // } + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.notificationTiming, + }); + + fastify.route({ method: "GET", url: "/api/listofactiveandinactivetankstatus/:customerId",