From 45a0b540a1a5fdb4f5f1bb9a608f07859566b2c0 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 20 Jan 2025 10:35:06 +0530 Subject: [PATCH] notifications changes --- src/controllers/tanksController.js | 50 ++++++++++++++++-------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6293fc61..5b9bdbfb 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1564,30 +1564,32 @@ 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,stopTime) => { - // const message = `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({ fcmIds: { $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 '${stopTime}'`; - const message = - `Tank Name: '${tankName}'\n` + - `Pump started at: '${startTime}'\n` + - `Initiated by user(s): ${userNames}\n` + - `Pump started by: '${motorOnType.toUpperCase()}'\n` + - `Will stop at: '${stopTime}'`; - // Send the notification - await sendNotification(fcmTokens, 'Arminta Water Management', message); - } catch (error) { - console.error('Error in motorStart event:', error); +eventEmitter.on( + 'motorStart', + async (fcmTokens, timestamp, motorId, waterLevel, blockName, tankName, startTime, motorOnType, stopCriteria, manual_threshold_time) => { + try { + // Retrieve the user information + const users = await User.find({ fcmIds: { $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}'\n` + + `Pump started at: '${startTime}'\n` + + `Initiated by user(s): ${userNames}\n` + + `Pump started by: '${motorOnType.toUpperCase()}'\n` + + `Will stop at: '${manual_threshold_time}'`; + + // Send the notification + await sendNotification(fcmTokens, 'Arminta Water Management', message); + } catch (error) { + console.error('Error in motorStart event:', error); + } } -}); +); + eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => { try { @@ -2147,7 +2149,7 @@ console.log(fcmToken) 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 stopTime = req.body.stopTime const motorOnType = req.body.motor_on_type || "app"; if (action === "start") {