automatic motor start and stop notification

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

@ -4281,6 +4281,8 @@ const motorActionAuto = async (req, reply) => {
let motorStopStatus; let motorStopStatus;
const tank = await Tank.findOne({ customerId, "connections.inputConnections.motor_id": motorId }); 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) { if (!tank) {
throw new Error("Tank not found for the provided motor ID."); throw new Error("Tank not found for the provided motor ID.");
@ -4302,16 +4304,18 @@ const motorActionAuto = async (req, reply) => {
} }
); );
eventEmitter.emit( if (allowNotifications) {
"motorStartAutomatic", eventEmitter.emit(
fcmTokens, "motorStartAutomatic",
tankName, fcmTokens,
blockName, tankName,
startTime, blockName,
"Automatic", startTime,
typeOfWater, "Automatic",
threshold typeOfWater,
); threshold
);
}
} }
@ -4331,16 +4335,17 @@ const motorActionAuto = async (req, reply) => {
const formattedDate = currentDateTime.toLocaleDateString(); const formattedDate = currentDateTime.toLocaleDateString();
const formattedTime = currentDateTime.toLocaleTimeString(); const formattedTime = currentDateTime.toLocaleTimeString();
const stopMessage = if (allowNotifications) {
`🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` + const stopMessage =
`🛢️ Tank Name: '${tankName}'\n` + `🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` +
`🏢 Block Name: '${blockName}'\n` + `🛢️ Tank Name: '${tankName}'\n` +
`🕒 Pump stopped at: ${stopTime}\n` + `🏢 Block Name: '${blockName}'\n` +
`⏳ Operation Duration: ${threshold} `; `🕒 Pump stopped at: ${stopTime}\n` +
`⏳ Operation Duration: ${threshold} `;
// Send stop notification
await sendNotification(fcmTokens, "Arminta Water Management", stopMessage);
// 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 () => { // const calculateWaterLevelAndNotify = async () => {
// try { // try {
// const now = moment(); // const now = moment();

@ -137,6 +137,7 @@ const userSchema = new mongoose.Schema(
lowWaterAlert: { type: Boolean, default: true }, lowWaterAlert: { type: Boolean, default: true },
criticalLowWaterAlert: { type: Boolean, default: true }, criticalLowWaterAlert: { type: Boolean, default: true },
manualStartAndStopNotify: { type: Boolean, default: true }, manualStartAndStopNotify: { type: Boolean, default: true },
automaticStartAndStopNotify: { type: Boolean, default: true },
createdAt: { createdAt: {

@ -1398,6 +1398,24 @@ module.exports = function (fastify, opts, next) {
}, },
handler: tanksController.sendUserManualStartAndStop, 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({ fastify.route({
method: "POST", method: "POST",

Loading…
Cancel
Save