From b58c116bcab1ac09af917bcbf1aead6ebe6b2ad6 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 9 Jan 2025 15:01:24 +0530 Subject: [PATCH 1/4] notification changes and forgot password chnages --- src/controllers/tanksController.js | 80 +++++++++++++++--------------- src/handlers/userHandler.js | 19 +++++-- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c5365f45..32934939 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1565,14 +1565,14 @@ admin.initializeApp({ // }); eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { - const message = `Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; + const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; await sendNotification(fcmTokens, 'Motor Started', message); }); // Emit motor stop event with motorId eventEmitter.on('motorStop', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName,stopTime,motorOnType) => { - const message = `Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; + const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; await sendNotification(fcmTokens, 'Motor Stopped', message); }); @@ -2027,50 +2027,50 @@ exports.motorAction = async (req, reply) => { const currentWaterLevel = parseInt(receiverTank.waterlevel, 10); const waterLevelThresholds = { low: 30, veryLow: 20, criticallyLow: 10 }; - // Check if the water level is below any of the thresholds - if (currentWaterLevel < waterLevelThresholds.criticallyLow) { - if (!receiverTank.notificationSentCritical) { - eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, receiverTank); - await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCritical: true } }); - } - } else if (currentWaterLevel < waterLevelThresholds.veryLow) { - if (!receiverTank.notificationSentVeryLow) { - eventEmitter.emit('sendVeryLowWaterNotification', fcmToken, receiverTank); - await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryLow: true } }); - } - } else if (currentWaterLevel < waterLevelThresholds.low) { - if (!receiverTank.notificationSentLow) { - eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank); - await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentLow: true } }); - } - } + // // Check if the water level is below any of the thresholds + // if (currentWaterLevel < waterLevelThresholds.criticallyLow) { + // if (!receiverTank.notificationSentCritical) { + // eventEmitter.emit('sendCriticalLowWaterNotification', fcmToken, receiverTank); + // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCritical: true } }); + // } + // } else if (currentWaterLevel < waterLevelThresholds.veryLow) { + // if (!receiverTank.notificationSentVeryLow) { + // eventEmitter.emit('sendVeryLowWaterNotification', fcmToken, receiverTank); + // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryLow: true } }); + // } + // } else if (currentWaterLevel < waterLevelThresholds.low) { + // if (!receiverTank.notificationSentLow) { + // eventEmitter.emit('sendLowWaterNotification', fcmToken, receiverTank); + // await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentLow: true } }); + // } + // } // Check for critical high water level -if (currentWaterLevel >= criticalHighWaterThreshold) { - if (!receiverTank.notificationSentCriticalHigh) { - eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, receiverTank); - await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCriticalHigh: true } }); - } -} -// Check for very high water level -else if (currentWaterLevel >= veryHighWaterThreshold) { - if (!receiverTank.notificationSentVeryHigh) { - eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, receiverTank); - await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryHigh: true } }); - } -} -// Check for high water level -else if (currentWaterLevel >= highWaterThreshold) { - if (!receiverTank.notificationSentHigh) { - eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); - await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentHigh: true } }); - } -} +// if (currentWaterLevel >= criticalHighWaterThreshold) { +// if (!receiverTank.notificationSentCriticalHigh) { +// eventEmitter.emit('sendCriticalHighWaterNotification', fcmToken, receiverTank); +// await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentCriticalHigh: true } }); +// } +// } +// // Check for very high water level +// else if (currentWaterLevel >= veryHighWaterThreshold) { +// if (!receiverTank.notificationSentVeryHigh) { +// eventEmitter.emit('sendVeryHighWaterNotification', fcmToken, receiverTank); +// await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentVeryHigh: true } }); +// } +// } +// // Check for high water level +// else if (currentWaterLevel >= highWaterThreshold) { +// if (!receiverTank.notificationSentHigh) { +// eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); +// await Tank.updateOne({ customerId, tankName: receiverTank.tankName }, { $set: { notificationSentHigh: true } }); +// } +// } // Determine the motor stop status based on the action let motorStopStatus; const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const stopTime = req.body.stopTime || new Date().toISOString(); - const motorOnType = req.body.motor_on_type || "manual"; + const motorOnType = req.body.motor_on_type || "application"; if (action === "start") { motorStopStatus = "2"; diff --git a/src/handlers/userHandler.js b/src/handlers/userHandler.js index 0b91141d..eabf6fb1 100644 --- a/src/handlers/userHandler.js +++ b/src/handlers/userHandler.js @@ -5,6 +5,8 @@ const Message = require("../models/Message"); const generator = require("generate-password"); const bcrypt = require("bcrypt"); const saltRounds = 10; +const jwt = require('jsonwebtoken') +const JWT_SECRET = 'your-secret-key'; // External Dependancies // offers http-friendly error objects. @@ -365,6 +367,7 @@ exports.verifyPhone = async (req, reply) => { } }; + exports.changePassword = async (req, reply) => { try { const { phone, newPassword, confirmPassword } = req.body; @@ -408,20 +411,28 @@ exports.changePassword = async (req, reply) => { // Check the result of the update operation if (updateResult.nModified > 0) { - // Fetch the updated user data - const updatedUser = await User.findOne({ phone }); + // Fetch the updated user data (excluding password) + const updatedUser = await User.findOne({ phone }).select('-services.password.bcrypt'); + + // Generate a new token for the user + const token = jwt.sign( + { id: updatedUser._id, phone: updatedUser.phone }, // You can include more user details if needed + JWT_SECRET, // Use your secret key from environment variables + { expiresIn: '1h' } // Token expiration time + ); return reply.send({ - armintatankdata: { + simplydata: { error: false, passwordChanged: true, userData: updatedUser, // Include updated user data + token, // Include the token in the response message: "Password updated successfully.", }, }); } else { return reply.send({ - armintatankdata: { + simplydata: { error: true, code: 10011, message: "Failed to update the password. Try again.", From 886d2971ec175d1d1269fa332212afb877c2962e Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 9 Jan 2025 16:26:53 +0530 Subject: [PATCH 2/4] chnages --- src/controllers/tanksController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 32934939..8aa0519a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1571,11 +1571,12 @@ eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, // Emit motor stop event with motorId -eventEmitter.on('motorStop', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName,stopTime,motorOnType) => { +eventEmitter.on('motorStop', async (fcmTokens, motorId, waterLevel, blockName, tankName, stopTime, motorOnType) => { const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; await sendNotification(fcmTokens, 'Motor Stopped', message); }); + // Event listener to handle notification eventEmitter.on('sendLowWaterNotification', async (fcmToken, tankInfo) => { const message = formatWaterLevelMessage(tankInfo, 'low'); From 718a45413eee0ebc2f5e2e2df9e0d83b511e85b3 Mon Sep 17 00:00:00 2001 From: gitadmin Date: Thu, 9 Jan 2025 17:28:04 +0530 Subject: [PATCH 3/4] dblocal --- src/config/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/config.js b/src/config/config.js index e165c6a9..f5be2f9e 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -21,10 +21,10 @@ var termination = chalk.bold.magenta; // TODO // Need to read database url from a environment variable. // const databaseURL = "mongodb://armintatankdbuser:armintatank1@35.207.198.4:27017/arminta-tank-db; -const databaseURLNew = "mongodb://35.207.198.4:27017/arminta-tank-db"; +//const databaseURLNew = "mongodb://35.207.198.4:27017/arminta-tank-db"; //const databaseURLNew = "mongodb://35.200.129.165:27017/health-care-db"; -// const databaseURLNew = "mongodb://127.0.0.1:27017/arminta-tank-db"; +const databaseURLNew = "mongodb://127.0.0.1:27017/arminta-tank-db"; // Next line not used , or no need to pass default db in the mongo connection url. const defaultDatabase = "arminta-tank-db"; From 346d766977fc1d83b375aea1c26ef2a90a95b9c1 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 9 Jan 2025 17:59:39 +0530 Subject: [PATCH 4/4] notification message --- src/controllers/tanksController.js | 56 +++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 8aa0519a..ddaa05c5 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1564,16 +1564,57 @@ admin.initializeApp({ // await sendNotification(fcmTokens, 'Motor Started', `Motor ID: ${motorId} started successfully at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`); // }); +// eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { +// const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; +// await sendNotification(fcmTokens, 'Motor Started', message); +// }); + +// eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { +// const message = `Tank Name : '${tankName}' Pump Starts at '${startTime}' by '${motorOnType}' will stop at '${stopCriteria} mins'`; +// await sendNotification(fcmTokens, 'Arminta Water Management', message); +// }); + eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria) => { - const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' started at ${startTime} by '${motorOnType}' mode and will stop after ${stopCriteria}. Current Water Level: ${waterLevel} Ltrs.`; - await sendNotification(fcmTokens, 'Motor Started', message); + try { + // Retrieve the user information + const users = await User.find({ fcmId: { $in: fcmTokens } }); + console.log("users",users) + const userNames = users.map(user => user.username).join(', '); + console.log("userNames",userNames) + + // Prepare the message + const message = `Tank Name: '${tankName}', Pump started at '${startTime}' by Initiated by user(s): ${userNames} '${motorOnType}' and will stop after '${stopCriteria}'`; + + // Send the notification + await sendNotification(fcmTokens, 'Arminta Water Management', message); + } catch (error) { + console.error('Error in motorStart event:', error); + } }); // Emit motor stop event with motorId -eventEmitter.on('motorStop', async (fcmTokens, motorId, waterLevel, blockName, tankName, stopTime, motorOnType) => { - const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; - await sendNotification(fcmTokens, 'Motor Stopped', message); +// eventEmitter.on('motorStop', async (fcmTokens, motorId, waterLevel, blockName, tankName, stopTime, motorOnType) => { +// const message = `MotorId '${motorId}' Water supply from '${blockName}' to '${tankName}' stopped at ${stopTime} by '${motorOnType}' mode. Current Water Level: ${waterLevel} Ltrs.`; +// await sendNotification(fcmTokens, 'Motor Stopped', message); +// }); + +eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => { + try { + // Retrieve the user information + const users = await User.find({ fcmId: { $in: fcmTokens } }); + console.log("users",users) + const userNames = users.map(user => user.username).join(', '); + console.log("userNames",userNames) + + // Prepare the message + const message = `Tank Name: '${tankName}', Pump stopped at '${stopTime}' by Initiated by user(s): ${userNames} '${motorOnType}'`; + + // Send the notification + await sendNotification(fcmTokens, 'Arminta Water Management', message); + } catch (error) { + console.error('Error in motorStart event:', error); + } }); @@ -2071,7 +2112,7 @@ exports.motorAction = async (req, reply) => { const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const stopTime = req.body.stopTime || new Date().toISOString(); - const motorOnType = req.body.motor_on_type || "application"; + const motorOnType = req.body.motor_on_type || "app"; if (action === "start") { motorStopStatus = "2"; @@ -2105,9 +2146,6 @@ exports.motorAction = async (req, reply) => { eventEmitter.emit( "motorStop", fcmToken, - motorId, - currentWaterLevel, - blockName, tankName, stopTime, motorOnType