notification changes and forgot password chnages

master
Bhaskar 9 months ago
parent a87c730650
commit b58c116bca

@ -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";

@ -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.",

Loading…
Cancel
Save