diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 681d052d..c97ec54a 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -9126,84 +9126,93 @@ exports.moveIssueToCategory = async (req, reply) => { let issueMoved = false; const nowTime = moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss"); + // Ensure categorizedIssues exists if (!Array.isArray(support.categorizedIssues)) { support.categorizedIssues = []; } + // Step 1: Try moving from support.issues (original logic) const index = support.issues.findIndex((issue) => { if (issue.hardwareId === hardwareId) return true; if (Array.isArray(issue.hardwareIds) && issue.hardwareIds.includes(hardwareId)) return true; return false; }); - if (index === -1) { - return reply.code(404).send({ message: "No matching issue found to move" }); - } - - const issue = support.issues[index]; - - // Normalize type if needed - const normalizedType = - issue.type === "GSM or LoRa Disconnected" ? "GSM Disconnected" : issue.type; + if (index !== -1) { + const issue = support.issues[index]; - if (issue.hardwareId === hardwareId) { - // Master issue - issue.movedToCategory = true; + // Normalize type if needed + const normalizedType = + issue.type === "GSM or LoRa Disconnected" ? "GSM Disconnected" : issue.type; - support.categorizedIssues.push({ - type: normalizedType, - hardwareId: issue.hardwareId, - masterHardwareId: issue.masterHardwareId || issue.hardwareId, - category, - movedAt: nowTime, - movedToCategory: true, - }); - - support.issues.splice(index, 1); - issueMoved = true; - } else { - // Slave issue - const slaveIndex = issue.hardwareIds.indexOf(hardwareId); - if (slaveIndex !== -1) { - const slaveName = issue.slaveNames?.[slaveIndex] || "Unknown"; + if (issue.hardwareId === hardwareId) { + // Master issue + issue.movedToCategory = true; support.categorizedIssues.push({ type: normalizedType, - hardwareId, + hardwareId: issue.hardwareId, masterHardwareId: issue.masterHardwareId || issue.hardwareId, - slaveName, category, movedAt: nowTime, movedToCategory: true, }); - issue.hardwareIds.splice(slaveIndex, 1); - issue.slaveNames.splice(slaveIndex, 1); + support.issues.splice(index, 1); + issueMoved = true; + } else { + // Slave issue + const slaveIndex = issue.hardwareIds.indexOf(hardwareId); + if (slaveIndex !== -1) { + const slaveName = issue.slaveNames?.[slaveIndex] || "Unknown"; + + support.categorizedIssues.push({ + type: normalizedType, + hardwareId, + masterHardwareId: issue.masterHardwareId || issue.hardwareId, + slaveName, + category, + movedAt: nowTime, + movedToCategory: true, + }); + + issue.hardwareIds.splice(slaveIndex, 1); + issue.slaveNames.splice(slaveIndex, 1); + + if (issue.hardwareIds.length === 0) { + support.issues.splice(index, 1); + } - // Remove full issue if no more slaves are left - if (issue.hardwareIds.length === 0) { - support.issues.splice(index, 1); + issueMoved = true; } - - issueMoved = true; } } - if (issueMoved) { - await support.save(); + // Step 2: If not found in support.issues, try to update existing categorized issue + if (!issueMoved) { + const categorized = support.categorizedIssues.find( + (i) => i.hardwareId === hardwareId + ); - // 🧹 Cleanup any duplicate issues with same hardwareId (master or slave) - support.issues = support.issues.filter((i) => { - const isMaster = i.hardwareId === hardwareId; - const isSlave = Array.isArray(i.hardwareIds) && i.hardwareIds.includes(hardwareId); - return !isMaster && !isSlave; - }); + if (categorized) { + categorized.category = category; + categorized.movedAt = nowTime; + await support.save(); + return reply.send({ message: "Category updated for already categorized issue" }); + } - await support.save(); - return reply.send({ message: "Issue moved to category successfully" }); - } else { - return reply.code(404).send({ message: "No matching issue found to move" }); + return reply.code(404).send({ message: "No matching issue found to move or update" }); } + + // Cleanup duplicates + support.issues = support.issues.filter((i) => { + const isMaster = i.hardwareId === hardwareId; + const isSlave = Array.isArray(i.hardwareIds) && i.hardwareIds.includes(hardwareId); + return !isMaster && !isSlave; + }); + + await support.save(); + return reply.send({ message: "Issue moved to category successfully" }); } catch (err) { console.error("Error moving issue:", err); return reply.code(500).send({ error: "Internal Server Error" }); @@ -9211,6 +9220,7 @@ exports.moveIssueToCategory = async (req, reply) => { }; + // exports.particularCategory = async (req, reply) => { // try { // const { supportId, category } = req.params;