From a3a67a418d9d0a1fb379b08a8516d36b30b12158 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 24 Feb 2025 14:48:07 +0530 Subject: [PATCH] forced manual notifications allow and disallow --- src/controllers/tanksController.js | 51 ++++++++++++++++++++++++++++-- src/models/User.js | 2 ++ src/routes/tanksRoute.js | 18 +++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index daa66826..fb00f2b6 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6339,19 +6339,45 @@ client.on('message', async (topic, message) => { // Find the inputConnection for the motor and update motor status const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name + const user = await User.findOne({ customerId: motorTank.customerId }); // Fetch user by customerId + const allowNotifications = user?.manualStartAndStopNotify ?? true; // Default to true if not set + if (inputConnection) { inputConnection.motor_status = status; // Update motor status - if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") { + const tankName = motorTank.tankName; + if (allowNotifications && inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") { const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); inputConnection.motor_stop_status = "2"; inputConnection.motor_on_type = "forced_manual"; inputConnection.startTime = currentTime; + + eventEmitter.emit( + "sendMotorStartNotification", + fcmToken, // FCM tokens + hw_Id, // Motor ID + inputConnection.water_level || 0, // Water level + motorTank.blockName || "N/A", // Block name + tankName, // Tank name + inputConnection.motor_on_type, // Motor on type + "threshold", // Stop criteria + manual_threshold_time // Threshold time in mins + ); } - if (inputConnection.motor_stop_status === "2" && status === 1) { + if (allowNotifications && inputConnection.motor_stop_status === "2" && status === 1) { const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); inputConnection.motor_stop_status = "1"; inputConnection.stopTime = currentTime; + + eventEmitter.emit( + "sendMotorStopNotification", + fcmToken, // FCM tokens + hw_Id, // Motor ID + inputConnection.water_level || 0, // Water level + motorTank.blockName || "N/A", // Block name + tankName, // Tank name + inputConnection.motor_on_type // Motor on type + ); } await motorTank.save(); // Save the updated tank @@ -6964,6 +6990,25 @@ exports.sendUserSetCriticallyLowWaterNotificationsSwitch = async (request, reply } }; +exports.sendUserManualStartAndStop = async (request, reply) => { + const { customerId, manualStartAndStopNotify } = request.body; + + try { + const user = await User.findOneAndUpdate( + { customerId }, + { manualStartAndStopNotify}, + { new: true, upsert: true } // Create user if not exists + ); + + console.log(`User ${customerId} updated: Allowed - ${manualStartAndStopNotify}`); + + 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 { // const now = moment(); @@ -7229,7 +7274,7 @@ cron.schedule('* * * * *', async () => { timezone: "Asia/Kolkata", }); - +//run the every one hour cron.schedule('0 * * * *', async () => { console.log("Checking for user notification times..."); await calculateLowWaterLevelAndNotify(); diff --git a/src/models/User.js b/src/models/User.js index abce4d82..61f84f4e 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -136,6 +136,8 @@ const userSchema = new mongoose.Schema( allowNotifications: { type: Boolean, default: true }, lowWaterAlert: { type: Boolean, default: true }, criticalLowWaterAlert: { type: Boolean, default: true }, + manualStartAndStopNotify: { type: Boolean, default: true }, + createdAt: { type: Date, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index ed534948..f1c55078 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1380,6 +1380,24 @@ module.exports = function (fastify, opts, next) { }, handler: tanksController.sendUserSetCriticallyLowWaterNotificationsSwitch, }); + fastify.route({ + method: "POST", + url: "/api/sendNotificationManualStartAndStop", + schema: { + tags: ["Tank"], + summary: "This is for Send Manual start and stop alert notification", + body: { + type: "object", + properties: { + customerId: { type: "string" }, + manualStartAndStopNotify: { type: "boolean" }, + }, + required: ["customerId", "manualStartAndStopNotify"], // Ensures all fields are required + }, + security: [{ basicAuth: [] }], + }, + handler: tanksController.sendUserManualStartAndStop, + }); fastify.route({ method: "POST",