diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 71865407..68c40002 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -7663,29 +7663,35 @@ exports.resolvedIssuesForSupport = async (req, reply) => { return reply.code(404).send({ message: "Support record not found" }); } - if (!Array.isArray(support.categorizedIssues)) { - support.categorizedIssues = []; - } - - if (!Array.isArray(support.resolvedIssues)) { - support.resolvedIssues = []; - } + if (!Array.isArray(support.categorizedIssues)) support.categorizedIssues = []; + if (!Array.isArray(support.resolvedIssues)) support.resolvedIssues = []; const nowTime = moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss"); - // Find the issue in categorizedIssues matching hardwareId and category - const index = support.categorizedIssues.findIndex((issue) => { - if (issue.hardwareId === hardwareId && issue.category === category) return true; - return false; - }); + // Try to find issue in categorizedIssues + let categorizedIndex = support.categorizedIssues.findIndex( + (issue) => issue.hardwareId === hardwareId + ); - if (index === -1) { + // Try to find issue in resolvedIssues + let resolvedIndex = support.resolvedIssues.findIndex( + (issue) => issue.hardwareId === hardwareId + ); + + // We want to get the issue object (prefer from categorizedIssues, else resolvedIssues) + let issue = + categorizedIndex !== -1 + ? support.categorizedIssues[categorizedIndex] + : resolvedIndex !== -1 + ? support.resolvedIssues[resolvedIndex] + : null; + + if (!issue) { return reply.code(404).send({ - message: `No issue found in categorizedIssues with hardwareId: ${hardwareId} and category: ${category}`, + message: `No issue found with hardwareId: ${hardwareId}`, }); } - const issue = support.categorizedIssues[index]; const masterHardwareId = issue.masterHardwareId || issue.hardwareId; // Fetch master and slaves connection status @@ -7700,34 +7706,65 @@ exports.resolvedIssuesForSupport = async (req, reply) => { master.connected_status === "connected" && slaves.every((s) => s.connected_status === "connected"); - if (!allConnected) { - return reply.send({ - message: "Master or some slaves are not connected yet. Issue cannot be resolved.", - }); - } + if (allConnected) { + // If all connected, move to resolvedIssues if not already there + if (resolvedIndex === -1) { + // Add to resolvedIssues + support.resolvedIssues.push({ + type: issue.type, + hardwareId: issue.hardwareId, + masterHardwareId, + category: "Resolved", + resolvedAt: nowTime, + originalMovedAt: issue.movedAt || null, + movedToCategory: true, + slaveName: issue.slaveName || null, + }); + } - // Move issue to resolvedIssues - const resolvedIssue = { - type: issue.type, - hardwareId: issue.hardwareId, - masterHardwareId, - category: "Resolved", - resolvedAt: nowTime, - originalMovedAt: issue.movedAt || null, - movedToCategory: true, - slaveName: issue.slaveName || null, - }; + // Remove from categorizedIssues if present + if (categorizedIndex !== -1) { + support.categorizedIssues.splice(categorizedIndex, 1); + } - support.resolvedIssues.push(resolvedIssue); + await support.save(); + return reply.send({ message: "Issue moved to resolved category successfully" }); + } else { + // Some device disconnected — move or keep in categorizedIssues with "Escalation" category - // Remove from categorizedIssues - support.categorizedIssues.splice(index, 1); + // Remove from resolvedIssues if present (issue is no longer resolved) + if (resolvedIndex !== -1) { + support.resolvedIssues.splice(resolvedIndex, 1); + } - await support.save(); + if (categorizedIndex === -1) { + // Not in categorizedIssues — add it now + support.categorizedIssues.push({ + type: issue.type, + hardwareId: issue.hardwareId, + masterHardwareId, + category: "Escalation", + movedAt: nowTime, + movedToCategory: true, + slaveName: issue.slaveName || null, + }); + } else { + // Already in categorizedIssues — update category to "Escalation" if different + if (support.categorizedIssues[categorizedIndex].category !== "Escalation") { + support.categorizedIssues[categorizedIndex].category = "Escalation"; + support.categorizedIssues[categorizedIndex].movedAt = nowTime; + support.categorizedIssues[categorizedIndex].movedToCategory = true; + } + } - return reply.send({ message: "Issue moved to resolved category successfully" }); + await support.save(); + return reply.send({ + message: + "Master or some slaves are disconnected. Issue moved/kept in escalation category.", + }); + } } catch (err) { - console.error("Error in updateIssueResolutionBasedOnConnectionStatus:", err); + console.error("Error in resolvedIssuesForSupport:", err); return reply.code(500).send({ error: "Internal Server Error" }); } -}; \ No newline at end of file +};