forced manual notifications allow and disallow

master^2
Bhaskar 7 months ago
parent ec8e9bab1f
commit a3a67a418d

@ -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();

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

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

Loading…
Cancel
Save