From 19ed9af93300f2b4c98e72588bcf680a634b5c50 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 4 Jun 2025 12:56:56 +0530 Subject: [PATCH] modify get all disconnected issues --- src/controllers/installationController.js | 25 ++--------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 898e01f3..ecec9b40 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -5138,7 +5138,6 @@ exports.raiseATicketSlave = async (req, reply) => { // } // }; - exports.getDisconnectedIssuesBySupportId = async (req, reply) => { try { const { supportId, customerId } = req.params; @@ -5155,7 +5154,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const allIssues = supportRecord.issues || []; const hardwareSet = new Set(); - // Collect all hardwareIds and masterHardwareIds from issues for (const issue of allIssues) { if (issue.hardwareId) hardwareSet.add(issue.hardwareId); if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId); @@ -5163,7 +5161,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const hardwareIds = [...hardwareSet]; - // Fetch all relevant sensors (masters and slaves) for customer const sensors = await Insensors.find({ customerId, $or: [ @@ -5172,17 +5169,14 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { ] }).lean(); - // Map sensors by their hardwareId and tankhardwareId for quick lookup const sensorMap = {}; for (const sensor of sensors) { if (sensor.hardwareId) sensorMap[sensor.hardwareId] = sensor; if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor; } - // Fetch all orders for the customer const orders = await Order.find({ customerId }).lean(); - // Map master order connections by hardwareId for master info const orderMap = {}; for (const order of orders) { (order.master_connections || []).forEach(conn => { @@ -5195,7 +5189,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { }); } - // Map slave order connections by hardwareId for slave info const slaveOrderMap = {}; for (const order of orders) { (order.tank_connections || []).forEach(conn => { @@ -5209,20 +5202,14 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { } const masterMap = {}; - for (const issue of allIssues) { const masterId = issue.masterHardwareId || issue.hardwareId; const masterSensor = sensorMap[masterId]; if (!masterSensor || masterSensor.type !== "master") continue; - // Only process masters with active support_issue_status + // Proceed even if support_issue_status is not "active" const masterIssueStatus = masterSensor.support_issue_status || "inactive"; - if (masterIssueStatus !== "active") { - // Skip this master and its slaves if issue inactive - continue; - } - // Get latest IotData for master to check GSM connection const latestMasterData = await IotData.findOne({ hardwareId: masterSensor.hardwareId }).sort({ date: -1 }).lean(); const now = moment.tz("Asia/Kolkata"); let gsmConnected = false; @@ -5258,7 +5245,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const master = masterMap[masterSensor.hardwareId]; - // Find slaves connected to this master with active issues only const connectedSlaves = await Insensors.find({ connected_to: masterSensor.hardwareId, type: "slave", @@ -5268,10 +5254,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const slaveSet = new Set(master.connected_slaves.map(s => s.hardwareId)); for (const slave of connectedSlaves) { - // Only add slave if its issue status is active const slaveIssueStatus = slave.support_issue_status || "inactive"; - if (slaveIssueStatus !== "active") continue; - const slaveHardwareId = slave.tankhardwareId || slave.hardwareId; if (slaveSet.has(slaveHardwareId)) continue; slaveSet.add(slaveHardwareId); @@ -5284,9 +5267,9 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { }).lean(); const slaveOrderInfo = slaveOrderMap[slaveHardwareId] || {}; - const matchedTank = latestMasterData?.tanks?.find(t => t.tankhardwareId === slaveHardwareId); let loraConnected = false; + if (matchedTank?.date && matchedTank.tankHeight !== "0") { const loraTime = moment.tz(matchedTank.date, "Asia/Kolkata"); loraConnected = now.diff(loraTime, "minutes") <= 1; @@ -5316,12 +5299,10 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { } } - // Add comments with text and createdAt to each master device const comments = (supportRecord.comments || []).map(c => ({ text: c.text, commentsTime: moment(c.createdAt).tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm") })); - for (const master of Object.values(masterMap)) { master.comments = comments; @@ -5334,7 +5315,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { totalMasters: Object.keys(masterMap).length, disconnectedIssues: Object.values(masterMap) }); - } catch (error) { console.error("Error fetching disconnected issues:", error); return reply.code(500).send({ error: "Internal server error" }); @@ -5342,7 +5322,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { }; - // exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => { // try { // const { supportId } = req.params;