From 573dcd299ebacd213eac677c6879020b5cd4ae2c Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 20 May 2025 12:42:22 +0530 Subject: [PATCH] power outage issues and list of all items --- src/controllers/installationController.js | 109 ++++++++++++++++++++++ src/models/store.js | 29 ++++++ src/routes/installationRoute.js | 45 +++++++++ 3 files changed, 183 insertions(+) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 17458e39..5399ddc5 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3912,3 +3912,112 @@ exports.deleteTeamMemberSupport = async (req, reply)=> { } } +exports.moveIssueToCategory = async (req, reply) => { + try { + const { supportId, category } = req.body; + const { hardwareId, tankhardwareId } = req.body; + + if (!supportId || !category || (!hardwareId && !tankhardwareId)) { + return reply.code(400).send({ + message: "supportId, category, and hardwareId or tankhardwareId are required", + }); + } + + const support = await Support.findOne({ supportId }); + if (!support) { + return reply.code(404).send({ message: "Support record not found" }); + } + + 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 + ); + + if (index !== -1) { + const issue = support.issues[index]; + + support.categorizedIssues.push({ + ...issue, + 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 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 (issueMoved) { + 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" }); + } + } catch (err) { + console.error("Error moving issue:", err); + return reply.code(500).send({ error: "Internal Server Error" }); + } +}; + + +exports.particularCategory = async (req, reply) => { + const { supportId, category } = req.params; + + const support = await Support.findOne({ supportId }); + if (!support) { + return reply.code(404).send({ message: 'Support record not found' }); + } + + const issues = (support.categorizedIssues || []).filter( + (issue) => issue.category === category + ); + + if (issues.length === 0) { + return reply.code(404).send({ message: `No issues found for category: ${category}` }); + } + + return reply.send({ category, issues }); +}; diff --git a/src/models/store.js b/src/models/store.js index 13ab1e19..8adad09a 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -239,6 +239,35 @@ const installationschema = new mongoose.Schema({ slaveHardwareId: String, slaveName: String, }], + categorizedIssues: [ + { + type: { + type: String, + required: true + }, + hardwareId: { + type: String, + required: true + }, + masterHardwareId: { + type: String, + required: true + }, + slaveName: { + type: String, + }, + category: { + type: String, + enum: ["Power Outage", "Level1", "Pending", "Onsite Issues"], + required: true + }, + movedAt: { + type: String, // or Date, depending on your preference + required: true + } + } + ], + profile: { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 41603b54..3c16e633 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -640,6 +640,51 @@ module.exports = function (fastify, opts, next) { }); + + fastify.post("/api/moveIssueToCategory", { + schema: { + description: "Move specific issue to a categorized bucket", + tags: ["Support"], + summary: "Move disconnected issue to category and remove from issues list", + body: { + type: "object", + required: ["supportId", "category"], + properties: { + supportId: { type: "string" }, + category: { + type: "string", + enum: ["Power Outage", "Level1", "Pending", "Onsite Issues"] + }, + hardwareId: { type: "string" }, // master + tankhardwareId: { type: "string" } // slave + } + } + }, + handler: installationController.moveIssueToCategory + }); + + fastify.get('/api/support/categorizedIssues/:supportId/:category', { + schema: { + description: 'Get all issues in a particular category for a support record', + tags: ['Support'], + summary: 'Fetch issues by category', + params: { + type: 'object', + required: ['supportId', 'category'], + properties: { + supportId: { type: 'string' }, + category: { + type: 'string', + enum: ['Power Outage', 'Level1', 'Pending', 'Onsite Issues'] // your allowed categories + } + } + }, + + }, + handler: installationController.particularCategory + }); + + next();