From ae90a7254795bb159d32f1360a6ca2259bb6a8e8 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 5 Jun 2025 11:08:56 +0530 Subject: [PATCH] changes --- src/controllers/installationController.js | 80 +++++------------------ 1 file changed, 18 insertions(+), 62 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 60662dce..b9dd1f84 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -6427,14 +6427,17 @@ exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => { // } // }; + + exports.getDisconnectedCustomerDetails = async (req, reply) => { try { const { supportId } = req.params; - if (!supportId) { return reply.code(400).send({ error: "supportId is required" }); } + const normalize = id => (typeof id === "string" ? id.trim().toLowerCase() : ""); + const supportRecord = await Support.findOne({ supportId }).lean(); if (!supportRecord) { return reply.code(404).send({ message: "No support record found for this supportId" }); @@ -6448,19 +6451,17 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { // Step 2: Build set of hardwareIds already in categorizedIssues (moved) const existingCategorizedHardwareIds = new Set(); (supportRecord.categorizedIssues || []).forEach(issue => { - if (issue.hardwareId) existingCategorizedHardwareIds.add(issue.hardwareId.trim().toLowerCase()); + if (issue.hardwareId) existingCategorizedHardwareIds.add(normalize(issue.hardwareId)); if (Array.isArray(issue.hardwareIds)) { - issue.hardwareIds.forEach(id => { - if (typeof id === "string") existingCategorizedHardwareIds.add(id.trim().toLowerCase()); - }); + issue.hardwareIds.forEach(id => existingCategorizedHardwareIds.add(normalize(id))); } }); // Step 3: Build list of hardwareIds in unresolved issues (excluding moved) const allHardwareIds = new Set(); for (const issue of unresolvedIssues) { - const issueHardwareId = issue.hardwareId?.trim().toLowerCase(); - const issueSlaveIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []; + const issueHardwareId = normalize(issue.hardwareId); + const issueSlaveIds = (issue.hardwareIds || []).map(normalize); if (issueHardwareId && !existingCategorizedHardwareIds.has(issueHardwareId)) { allHardwareIds.add(issueHardwareId); @@ -6473,7 +6474,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { } } - // Debug: Log issue hardware IDs being considered console.log("✅ All issue hardwareIds:", Array.from(allHardwareIds)); // Step 4: If no valid hardware IDs left, stop @@ -6493,7 +6493,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { ] }).lean(); - // Debug: Log disconnected sensors found console.log("⚙️ Disconnected sensors matched:", disconnectedSensors.map(s => s.hardwareId)); if (!disconnectedSensors.length) { @@ -6512,53 +6511,16 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { customerHardwareMap[custId] = new Set(); } - const sensorHw = sensor.tankhardwareId?.trim().toLowerCase(); - const sensorConnected = sensor.connected_to?.trim().toLowerCase(); - - // for (const issue of unresolvedIssues) { - // const allIssueHardwareIds = [ - // ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []), - // issue.hardwareId?.trim().toLowerCase() - // ]; - - // if ( - // (sensorHw && allIssueHardwareIds.includes(sensorHw)) || - // (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) - // ) { - // if (issue.hardwareId && !existingCategorizedHardwareIds.has(issue.hardwareId.trim().toLowerCase())) { - // customerHardwareMap[custId].add(issue.hardwareId.trim().toLowerCase()); - // } - // } - // } - - const sensorHardwareId = sensor.hardwareId?.trim().toLowerCase(); - // for (const issue of unresolvedIssues) { - // const allIssueHardwareIds = [ - // ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []), - // issue.hardwareId?.trim().toLowerCase() - // ]; - - // if ( - // (sensorHw && allIssueHardwareIds.includes(sensorHw)) || - // (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) || - // (sensorHardwareId && allIssueHardwareIds.includes(sensorHardwareId)) - // ) { - // for (const hw of allIssueHardwareIds) { - // if (hw && !existingCategorizedHardwareIds.has(hw)) { - // customerHardwareMap[custId].add(hw); - // } - // } - // } - // } + const sensorHw = normalize(sensor.tankhardwareId); + const sensorConnected = normalize(sensor.connected_to); + const sensorHardwareId = normalize(sensor.hardwareId); for (const issue of unresolvedIssues) { - if (issue.movedToCategory === true) continue; // ✅ extra safeguard - const allIssueHardwareIds = [ - ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []), - issue.hardwareId?.trim().toLowerCase() + normalize(issue.hardwareId), + ...(issue.hardwareIds || []).map(normalize) ]; - + if ( (sensorHw && allIssueHardwareIds.includes(sensorHw)) || (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) || @@ -6571,12 +6533,8 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { } } } - - - } - // Debug: Log map of matched customer hardware console.log("📌 Customer hardware map:", Object.fromEntries( Object.entries(customerHardwareMap).map(([k, v]) => [k, Array.from(v)]) )); @@ -6590,9 +6548,10 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { if (hardwareIdSet.size === 0) continue; const relatedIssues = unresolvedIssues.filter(issue => { - const issueHw = issue.hardwareId?.trim().toLowerCase(); - const hardwareIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []; - const allIds = [issueHw, ...hardwareIds]; + const allIds = [ + normalize(issue.hardwareId), + ...(issue.hardwareIds || []).map(normalize) + ]; return Array.from(hardwareIdSet).some(hw => allIds.includes(hw)); }); @@ -6658,9 +6617,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { }; - - - exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { try { const { support_teamMemberId } = req.params;