diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 5399ddc5..596be2f8 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3914,12 +3914,12 @@ exports.deleteTeamMemberSupport = async (req, reply)=> { exports.moveIssueToCategory = async (req, reply) => { try { - const { supportId, category } = req.body; - const { hardwareId, tankhardwareId } = req.body; + const { supportId } = req.params; + const { category, hardwareId } = req.body; - if (!supportId || !category || (!hardwareId && !tankhardwareId)) { + if (!supportId || !category || !hardwareId) { return reply.code(400).send({ - message: "supportId, category, and hardwareId or tankhardwareId are required", + message: "supportId (path), category and hardwareId (body) are required", }); } @@ -3931,62 +3931,59 @@ exports.moveIssueToCategory = async (req, reply) => { let issueMoved = false; const nowTime = moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss"); - // Ensure categorizedIssues array exists if (!Array.isArray(support.categorizedIssues)) { support.categorizedIssues = []; } - // Master Issue Move - if (hardwareId && !tankhardwareId) { - const index = support.issues.findIndex( - (issue) => issue.hardwareId === hardwareId && issue.masterHardwareId === hardwareId - ); + // Find the issue where hardwareId matches either hardwareId or inside hardwareIds array + const index = support.issues.findIndex((issue) => { + // Master hardwareId match + if (issue.hardwareId === hardwareId) return true; + // Slave hardwareIds array match + 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" }); + } - if (index !== -1) { - const issue = support.issues[index]; + const issue = support.issues[index]; + + // If the hardwareId matches master hardwareId, move entire issue as is + if (issue.hardwareId === hardwareId) { + support.categorizedIssues.push({ + ...issue, + category, + movedAt: nowTime, + }); + support.issues.splice(index, 1); + issueMoved = true; + } else { + // hardwareId matches inside hardwareIds array — move that slave issue individually + const slaveIndex = issue.hardwareIds.indexOf(hardwareId); + if (slaveIndex !== -1) { + const slaveName = issue.slaveNames?.[slaveIndex] || "Unknown"; support.categorizedIssues.push({ - ...issue, + type: issue.type, + hardwareId, + masterHardwareId: issue.masterHardwareId || issue.hardwareId, + slaveName, category, movedAt: nowTime, }); - support.issues.splice(index, 1); - issueMoved = true; - } - } - - // Slave Issue Move - if (tankhardwareId) { - for (let i = 0; i < support.issues.length; i++) { - const issue = support.issues[i]; - - const slaveIndex = issue.hardwareIds?.indexOf(tankhardwareId); - if (slaveIndex !== -1) { - const slaveName = issue.slaveNames?.[slaveIndex] || "Unknown"; - - // Add to categorized issues - support.categorizedIssues.push({ - type: issue.type, - hardwareId: tankhardwareId, - masterHardwareId: issue.masterHardwareId, - slaveName, - category, - movedAt: nowTime, - }); + // Remove slave from issue + issue.hardwareIds.splice(slaveIndex, 1); + issue.slaveNames.splice(slaveIndex, 1); - // Remove from current issue - issue.hardwareIds.splice(slaveIndex, 1); - issue.slaveNames.splice(slaveIndex, 1); - - // Remove issue completely if no more slaves - if (issue.hardwareIds.length === 0) { - support.issues.splice(i, 1); - } - - issueMoved = true; - break; + // If no more slaves left, remove the issue completely + if (issue.hardwareIds.length === 0) { + support.issues.splice(index, 1); } + + issueMoved = true; } } diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 3c16e633..d8f13693 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -641,24 +641,26 @@ module.exports = function (fastify, opts, next) { }); - fastify.post("/api/moveIssueToCategory", { + fastify.post("/api/moveIssueToCategory/:supportId", { schema: { description: "Move specific issue to a categorized bucket", tags: ["Support"], summary: "Move disconnected issue to category and remove from issues list", - body: { + params: { type: "object", - required: ["supportId", "category"], + required: ["supportId"], properties: { supportId: { type: "string" }, - category: { - type: "string", - enum: ["Power Outage", "Level1", "Pending", "Onsite Issues"] - }, - hardwareId: { type: "string" }, // master - tankhardwareId: { type: "string" } // slave - } - } + }, + }, + body: { + type: "object", + required: ["category", "hardwareId"], + properties: { + category: { type: "string" }, + hardwareId: { type: "string" }, + }, + }, }, handler: installationController.moveIssueToCategory });