diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fb00f2b6..b8e62aac 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -4281,6 +4281,8 @@ const motorActionAuto = async (req, reply) => { let motorStopStatus; const tank = await Tank.findOne({ customerId, "connections.inputConnections.motor_id": motorId }); + const user = await User.findOne({ customerId }); + const allowNotifications = user?.automaticStartAndStopNotify ?? true; // Default to true if not set if (!tank) { throw new Error("Tank not found for the provided motor ID."); @@ -4302,16 +4304,18 @@ const motorActionAuto = async (req, reply) => { } ); - eventEmitter.emit( - "motorStartAutomatic", - fcmTokens, - tankName, - blockName, - startTime, - "Automatic", - typeOfWater, - threshold - ); + if (allowNotifications) { + eventEmitter.emit( + "motorStartAutomatic", + fcmTokens, + tankName, + blockName, + startTime, + "Automatic", + typeOfWater, + threshold + ); + } } @@ -4331,16 +4335,17 @@ const motorActionAuto = async (req, reply) => { const formattedDate = currentDateTime.toLocaleDateString(); const formattedTime = currentDateTime.toLocaleTimeString(); - const stopMessage = - `🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` + - `🛢️ Tank Name: '${tankName}'\n` + - `🏢 Block Name: '${blockName}'\n` + - `🕒 Pump stopped at: ${stopTime}\n` + - `⏳ Operation Duration: ${threshold} `; - - // Send stop notification - await sendNotification(fcmTokens, "Arminta Water Management", stopMessage); + if (allowNotifications) { + const stopMessage = + `🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` + + `🛢️ Tank Name: '${tankName}'\n` + + `🏢 Block Name: '${blockName}'\n` + + `🕒 Pump stopped at: ${stopTime}\n` + + `⏳ Operation Duration: ${threshold} `; + // Send stop notification + await sendNotification(fcmTokens, "Arminta Water Management", stopMessage); + } } @@ -7009,6 +7014,25 @@ exports.sendUserManualStartAndStop = async (request, reply) => { } }; +exports.sendUserAutomaticStartAndStop = async (request, reply) => { + const { customerId, automaticStartAndStopNotify } = request.body; + + try { + const user = await User.findOneAndUpdate( + { customerId }, + { automaticStartAndStopNotify}, + { new: true, upsert: true } // Create user if not exists + ); + + console.log(`User ${customerId} updated: Allowed - ${automaticStartAndStopNotify}`); + + 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(); diff --git a/src/models/User.js b/src/models/User.js index 61f84f4e..cbd5613f 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -137,6 +137,7 @@ const userSchema = new mongoose.Schema( lowWaterAlert: { type: Boolean, default: true }, criticalLowWaterAlert: { type: Boolean, default: true }, manualStartAndStopNotify: { type: Boolean, default: true }, + automaticStartAndStopNotify: { type: Boolean, default: true }, createdAt: { diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index f1c55078..1a223ef5 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1398,6 +1398,24 @@ module.exports = function (fastify, opts, next) { }, handler: tanksController.sendUserManualStartAndStop, }); + fastify.route({ + method: "POST", + url: "/api/sendNotificationAutomaticStartAndStop", + schema: { + tags: ["Tank"], + summary: "This is for Send Automatic start and stop alert notification", + body: { + type: "object", + properties: { + customerId: { type: "string" }, + automaticStartAndStopNotify: { type: "boolean" }, + }, + required: ["customerId", "automaticStartAndStopNotify"], // Ensures all fields are required + }, + security: [{ basicAuth: [] }], + }, + handler: tanksController.sendUserAutomaticStartAndStop, + }); fastify.route({ method: "POST",