diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index d65a2ca0..fc780421 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -7645,3 +7645,71 @@ exports.updateComments = async (req, reply) => { return reply.code(500).send({ error: "Internal server error" }); } }; + + +exports.resolvedIssuesForSupport = async (req, reply) => { + try { + const { supportId } = req.params; + + if (!supportId) { + return reply.code(400).send({ error: "supportId is required" }); + } + + // Fetch support document + const support = await Support.findOne({ supportId }); + if (!support) { + return reply.code(404).send({ error: "Support record not found" }); + } + + const categorizedIssues = support.categorizedIssues || []; + const resolvedIssues = support.resolvedIssues || []; + let updatedCount = 0; + + // Filter escalation issues + const escalationIssues = categorizedIssues.filter(issue => issue.category === "Escalation"); + + for (const issue of escalationIssues) { + const masterHardwareId = issue.masterHardwareId || issue.hardwareId; + + // Get master sensor + const master = await Insensors.findOne({ hardwareId: masterHardwareId }).lean(); + if (!master) continue; + + // Get slaves connected to this master + const slaves = await Insensors.find({ connected_to: masterHardwareId }).lean(); + + // Check if master and all slaves are connected + const allConnected = master.connected_status === "connected" && slaves.every(s => s.connected_status === "connected"); + + if (allConnected) { + // Prepare resolved issue data + const resolvedIssue = { + ...issue.toObject ? issue.toObject() : issue, // convert if mongoose doc + category: "Resolved", + resolvedAt: new Date().toISOString(), + originalMovedAt: issue.movedAt || null, + }; + + // Remove from categorizedIssues + support.categorizedIssues = support.categorizedIssues.filter(i => i._id.toString() !== issue._id.toString()); + + // Add to resolvedIssues + support.resolvedIssues.push(resolvedIssue); + + updatedCount++; + } + } + + // Save updated support document + await support.save(); + + return reply.send({ + status: "success", + message: `${updatedCount} issue(s) moved to resolved`, + supportId, + }); + } catch (error) { + console.error("Error in updateIssueResolutionBasedOnConnectionStatus:", error); + return reply.code(500).send({ error: "Internal server error" }); + } +} \ No newline at end of file diff --git a/src/models/store.js b/src/models/store.js index 0bb3e9c4..660013ba 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -322,6 +322,16 @@ const installationschema = new mongoose.Schema({ issues: [IssueSchema], categorizedIssues: [CategorizedIssueSchema], + resolvedIssues: [ + { + type: { type: String }, + hardwareId: String, + masterHardwareId: String, + category: String, // will be 'Resolved' + resolvedAt: String, // ISO string or Date + originalMovedAt: String, // store original movedAt for reference + } + ], masterDisconnected: [{ // new field for master disconnected details hardwareId: String, masterName: String, diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index c86db920..43756337 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -894,6 +894,24 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.StatusTeamMember }); + + + fastify.put('/api/resolvedIssues/:supportId', { + schema: { + description: "Resolved Issues for Support Team", + tags: ["Support"], + params: { + type: "object", + required: ["supportId"], + properties: { + supportId: { type: "string", minLength: 1 }, + + } + }, + }, + handler: installationController.resolvedIssuesForSupport + }); + next(); } \ No newline at end of file