automatic motor start and stop notification

master^2
Bhaskar 7 months ago
parent 488c1d15d0
commit 66d7feeb71

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

@ -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: {

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

Loading…
Cancel
Save