From 0cc50fe03b3ae0272a9aa9b72538526c54bf90e8 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 5 May 2025 12:02:02 +0530 Subject: [PATCH] changes --- src/controllers/installationController.js | 74 +++++++++++++++-------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index b3a9d984..f36f36df 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2688,7 +2688,7 @@ exports.raiseATicketBuildingDetails = async (req, reply) => { exports.raiseATicketSlave = async (req, reply) => { try { - const { customerId, connected_to, tankHardwareId } = req.params; // Now tankHardwareId from params + const { customerId, connected_to, tankHardwareId } = req.params; if (!customerId || !connected_to) { return reply.code(400).send({ error: "customerId and connected_to are required" }); @@ -2700,7 +2700,7 @@ exports.raiseATicketSlave = async (req, reply) => { return reply.code(404).send({ message: "No sensors found for this customer." }); } - const masterSensor = sensors.find(s => (s.hardwareId?.trim() === connected_to.trim())); + const masterSensor = sensors.find(s => s.hardwareId?.trim() === connected_to.trim()); if (!masterSensor) { return reply.code(404).send({ message: "Master hardwareId not found." }); @@ -2712,20 +2712,20 @@ exports.raiseATicketSlave = async (req, reply) => { return reply.code(404).send({ message: "No IoT data found for this hardwareId." }); } - const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata"); const now = moment.tz("Asia/Kolkata"); + const masterTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata"); + const masterDiff = now.diff(masterTime, "minutes"); - const diffInMinutesMaster = now.diff(indiaTime, "minutes"); - - const masterDisconnected = diffInMinutesMaster > 1 ? [{ + const masterDisconnected = masterDiff > 1 ? [{ hardwareId: connected_to, - masterName: masterSensor.tankName, - // disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss"), + masterName: masterSensor.tankName || "Unknown Master", }] : []; - let connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === connected_to.trim()); + let connectedSlaves = sensors.filter(sensor => + sensor.connected_to?.trim() === connected_to.trim() && + sensor.type === "slave" + ); - // If tankHardwareId is provided, filter for that specific slave if (tankHardwareId) { connectedSlaves = connectedSlaves.filter(s => s.hardwareId?.trim() === tankHardwareId.trim()); } @@ -2736,15 +2736,15 @@ exports.raiseATicketSlave = async (req, reply) => { const slaveId = slave.hardwareId?.trim(); const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId); - if (matchedTank && matchedTank.date) { + if (matchedTank?.date) { const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); - const loraDiffInMinutes = now.diff(tankTime, "minutes"); + const loraDiff = now.diff(tankTime, "minutes"); - if (loraDiffInMinutes > 1) { + if (loraDiff > 1) { disconnectedSlaves.push({ hardwareId: connected_to, slaveHardwareId: slaveId, - slaveName: slave.tankName + slaveName: slave.tankName || "Unknown Slave" }); } } @@ -2752,22 +2752,46 @@ exports.raiseATicketSlave = async (req, reply) => { const issuesToAdd = []; + // Check for existing GSM issue if (masterDisconnected.length > 0) { - issuesToAdd.push({ - type: "GSM Disconnected", - hardwareId: connected_to, - message: `Master GSM disconnected - ${connected_to}`, - // disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss") + const existingGsmIssue = await Support.findOne({ + "issues.hardwareId": connected_to, + "issues.type": "GSM Disconnected" }); + + if (!existingGsmIssue) { + issuesToAdd.push({ + type: "GSM Disconnected", + hardwareId: connected_to, + message: `Master GSM disconnected - ${connected_to}` + }); + } + } + + // Check for new LoRa issues + const newSlaveHardwareIds = []; + const newSlaveNames = []; + + for (const slave of disconnectedSlaves) { + const existingSlaveIssue = await Support.findOne({ + "issues.hardwareIds": slave.slaveHardwareId, + "issues.masterHardwareId": connected_to, + "issues.type": "LoRa Disconnected" + }); + + if (!existingSlaveIssue) { + newSlaveHardwareIds.push(slave.slaveHardwareId); + newSlaveNames.push(slave.slaveName); + } } - if (disconnectedSlaves.length > 0) { + if (newSlaveHardwareIds.length > 0) { issuesToAdd.push({ type: "LoRa Disconnected", - hardwareIds: disconnectedSlaves.map(d => d.slaveHardwareId), - slaveNames: disconnectedSlaves.map(d => d.slaveName), - message: "Slaves LoRa disconnected", - //disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss") + masterHardwareId: connected_to, + hardwareIds: newSlaveHardwareIds, + slaveNames: newSlaveNames, + message: `Slaves LoRa disconnected under master ${connected_to}` }); } @@ -2798,7 +2822,7 @@ exports.raiseATicketSlave = async (req, reply) => { }); } catch (error) { - console.error("Error raising ticket:", error); + console.error("Error raising ticket (slave):", error); return reply.code(500).send({ error: "Internal server error" }); } };