diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 2ba7fee4..2e96010e 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -6791,9 +6791,29 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { // }); const customerResults = customerDetails.map((customer) => { - const affectedHardwareSet = customerHardwareMap[customer.customerId] || new Set(); + const customerId = customer.customerId; + const affectedHardwareSet = customerHardwareMap[customerId] || new Set(); const affectedLowerSet = new Set([...affectedHardwareSet].map(id => id.toLowerCase().trim())); + // Filter only sensors that belong to this customer and are disconnected + const disconnectedSensorsForCustomer = disconnectedSensors.filter(sensor => + sensor.customerId === customerId + ); + + // Collect actual disconnected hardwareIds (only masters and slaves) + const uniqueDisconnectedHardwareIds = new Set(); + for (const sensor of disconnectedSensorsForCustomer) { + if ( + (sensor.type === 'master' || sensor.type === 'slave') && + sensor.connected_status === 'disconnected' + ) { + const hw = sensor.tankhardwareId || sensor.hardwareId || sensor.connected_to; + if (hw) { + uniqueDisconnectedHardwareIds.add(hw.trim()); + } + } + } + // Get all unresolved issues related to this customer's affected hardwareIds const customerIssues = unresolvedIssues.filter(issue => { const allIssueHardwareIds = [ @@ -6803,7 +6823,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { return allIssueHardwareIds.some(hw => affectedLowerSet.has(hw)); }); - // Get the latest lastTicketRaisedAt from the issues + // Get latest ticket raise timestamp const lastTicketRaisedAt = customerIssues.reduce((latest, issue) => { const issueTime = new Date(issue.lastTicketRaisedAt); if (!isNaN(issueTime)) { @@ -6827,8 +6847,8 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { city: customer.profile?.city || "", latitude: customer.latitude, longitude: customer.longitude, - totalHardwareIdsCount: affectedHardwareSet.size, - hardwareIds: [...affectedHardwareSet], + totalHardwareIdsCount: uniqueDisconnectedHardwareIds.size, + hardwareIds: [...uniqueDisconnectedHardwareIds], lastTicketRaisedAt: lastTicketRaisedAt || null }; });