diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index db7cafef..17458e39 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3463,13 +3463,12 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const allIssues = supportRecord.issues || []; const hardwareSet = new Set(); - // Collect all hardware IDs and tankHardwareIds from issues for (const issue of allIssues) { - if (issue.hardwareId) hardwareSet.add(issue.hardwareId); // GSM + if (issue.hardwareId) hardwareSet.add(issue.hardwareId); if (Array.isArray(issue.hardwareIds)) { - issue.hardwareIds.forEach(id => hardwareSet.add(id)); // LoRa slaves + issue.hardwareIds.forEach(id => hardwareSet.add(id)); } - if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId); // LoRa master + if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId); } const hardwareIds = [...hardwareSet]; @@ -3477,20 +3476,21 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const sensors = await Insensors.find({ $or: [ { hardwareId: { $in: hardwareIds } }, - { tankHardwareId: { $in: hardwareIds } } + { tankhardwareId: { $in: hardwareIds } } ] }).lean(); - - // Map sensors by both hardwareId and tankHardwareId const sensorMap = {}; for (const sensor of sensors) { if (sensor.hardwareId) sensorMap[sensor.hardwareId] = sensor; - if (sensor.tankHardwareId) sensorMap[sensor.tankHardwareId] = sensor; + if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor; } - // Fetch Orders using customerId and build a map - const customerId = supportRecord.customerId; - const orders = await Order.find({ customerId }).lean(); + const customerId = sensors.length > 0 ? sensors[0].customerId : null; + console.log("customerId", customerId); + + // ✅ Fetch orders only if customerId is present + const orders = customerId ? await Order.find({ customerId }).lean() : []; + console.log("orders", orders); const orderMap = {}; for (const order of orders) { @@ -3509,15 +3509,14 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const masterMap = {}; for (const issue of allIssues) { - // GSM Disconnected if (issue.type === "GSM or LoRa Disconnected" && issue.hardwareId) { const sensor = sensorMap[issue.hardwareId]; if (sensor && sensor.type === "master") { const enriched = orderMap[sensor.hardwareId] || {}; masterMap[sensor.hardwareId] = { hardwareId: sensor.hardwareId, - masterName: enriched.masterName || sensor.masterName || null, - location: enriched.location || sensor.location || "", + masterName: sensor.masterName || enriched.masterName || "", + location: sensor.location || enriched.location || "", type: "master", connected_status: sensor.connected_status, gsm_last_check_time: sensor.gsm_last_check_time, @@ -3533,17 +3532,17 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { } } - // LoRa Disconnected if (issue.type === "GSM or LoRa Disconnected" && issue.masterHardwareId) { const masterSensor = sensorMap[issue.masterHardwareId]; if (!masterSensor || masterSensor.type !== "master") continue; + const enriched = orderMap[masterSensor.hardwareId] || {}; + if (!masterMap[masterSensor.hardwareId]) { - const enriched = orderMap[masterSensor.hardwareId] || {}; masterMap[masterSensor.hardwareId] = { hardwareId: masterSensor.hardwareId, - masterName: enriched.masterName || masterSensor.masterName || null, - location: enriched.location || masterSensor.location || "", + masterName: masterSensor.masterName || enriched.masterName || "", + location: masterSensor.location || enriched.location || "", type: "master", connected_status: masterSensor.connected_status, gsm_last_check_time: masterSensor.gsm_last_check_time, @@ -3578,6 +3577,19 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { } for (const slaveSensor of fallbackSlaves) { + let typeOfWater = ""; + + if (customerId && slaveSensor.tankhardwareId) { + const tankDetails = await Tank.findOne({ + customerId, + tankhardwareId: slaveSensor.tankhardwareId + }).lean(); + + if (tankDetails?.typeOfWater) { + typeOfWater = tankDetails.typeOfWater; + } + } + master.connected_slaves.push({ hardwareId: slaveSensor.hardwareId, tankName: slaveSensor.tankName || "", @@ -3590,9 +3602,9 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { connected_to: slaveSensor.connected_to, masterName: master.masterName, type: "slave", - typeOfWater: slaveSensor.typeOfWater, + typeOfWater, tankHeight: slaveSensor.tankHeight, - support_lora_last_check_time: slaveSensor.support_lora_last_check_time, + support_lora_last_check_time: slaveSensor.support_lora_last_check_time }); master.connected_slave_count++; } @@ -3615,6 +3627,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { }; + exports.getDisconnectedCustomerDetails = async (req, reply) => { try { const { supportId } = req.params;