|
|
@ -7650,66 +7650,84 @@ exports.updateComments = async (req, reply) => {
|
|
|
|
exports.resolvedIssuesForSupport = async (req, reply) => {
|
|
|
|
exports.resolvedIssuesForSupport = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { supportId } = req.params;
|
|
|
|
const { supportId } = req.params;
|
|
|
|
|
|
|
|
const { category, hardwareId } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
if (!supportId) {
|
|
|
|
if (!supportId || !category || !hardwareId) {
|
|
|
|
return reply.code(400).send({ error: "supportId is required" });
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
|
|
|
message: "supportId (path), category and hardwareId (body) are required",
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch support document
|
|
|
|
|
|
|
|
const support = await Support.findOne({ supportId });
|
|
|
|
const support = await Support.findOne({ supportId });
|
|
|
|
if (!support) {
|
|
|
|
if (!support) {
|
|
|
|
return reply.code(404).send({ error: "Support record not found" });
|
|
|
|
return reply.code(404).send({ message: "Support record not found" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const categorizedIssues = support.categorizedIssues || [];
|
|
|
|
if (!Array.isArray(support.categorizedIssues)) {
|
|
|
|
const resolvedIssues = support.resolvedIssues || [];
|
|
|
|
support.categorizedIssues = [];
|
|
|
|
let updatedCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Filter escalation issues
|
|
|
|
if (!Array.isArray(support.resolvedIssues)) {
|
|
|
|
const escalationIssues = categorizedIssues.filter(issue => issue.category === "Escalation");
|
|
|
|
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;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
|
|
|
message: `No issue found in categorizedIssues with hardwareId: ${hardwareId} and category: ${category}`,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const issue of escalationIssues) {
|
|
|
|
const issue = support.categorizedIssues[index];
|
|
|
|
const masterHardwareId = issue.masterHardwareId || issue.hardwareId;
|
|
|
|
const masterHardwareId = issue.masterHardwareId || issue.hardwareId;
|
|
|
|
|
|
|
|
|
|
|
|
// Get master sensor
|
|
|
|
// Fetch master and slaves connection status
|
|
|
|
const master = await Insensors.findOne({ hardwareId: masterHardwareId }).lean();
|
|
|
|
const master = await Insensors.findOne({ hardwareId: masterHardwareId }).lean();
|
|
|
|
if (!master) continue;
|
|
|
|
if (!master) {
|
|
|
|
|
|
|
|
return reply.code(404).send({ message: `Master device not found with hardwareId ${masterHardwareId}` });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get slaves connected to this master
|
|
|
|
|
|
|
|
const slaves = await Insensors.find({ connected_to: masterHardwareId }).lean();
|
|
|
|
const slaves = await Insensors.find({ connected_to: masterHardwareId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Check if master and all slaves are connected
|
|
|
|
const allConnected =
|
|
|
|
const allConnected = master.connected_status === "connected" && slaves.every(s => s.connected_status === "connected");
|
|
|
|
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) {
|
|
|
|
// Move issue to resolvedIssues
|
|
|
|
// Prepare resolved issue data
|
|
|
|
|
|
|
|
const resolvedIssue = {
|
|
|
|
const resolvedIssue = {
|
|
|
|
...issue.toObject ? issue.toObject() : issue, // convert if mongoose doc
|
|
|
|
type: issue.type,
|
|
|
|
|
|
|
|
hardwareId: issue.hardwareId,
|
|
|
|
|
|
|
|
masterHardwareId,
|
|
|
|
category: "Resolved",
|
|
|
|
category: "Resolved",
|
|
|
|
resolvedAt: new Date().toISOString(),
|
|
|
|
resolvedAt: nowTime,
|
|
|
|
originalMovedAt: issue.movedAt || null,
|
|
|
|
originalMovedAt: issue.movedAt || null,
|
|
|
|
|
|
|
|
movedToCategory: true,
|
|
|
|
|
|
|
|
slaveName: issue.slaveName || null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Remove from categorizedIssues
|
|
|
|
|
|
|
|
support.categorizedIssues = support.categorizedIssues.filter(i => i._id.toString() !== issue._id.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add to resolvedIssues
|
|
|
|
|
|
|
|
support.resolvedIssues.push(resolvedIssue);
|
|
|
|
support.resolvedIssues.push(resolvedIssue);
|
|
|
|
|
|
|
|
|
|
|
|
updatedCount++;
|
|
|
|
// Remove from categorizedIssues
|
|
|
|
}
|
|
|
|
support.categorizedIssues.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save updated support document
|
|
|
|
|
|
|
|
await support.save();
|
|
|
|
await support.save();
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({ message: "Issue moved to resolved category successfully" });
|
|
|
|
status: "success",
|
|
|
|
} catch (err) {
|
|
|
|
message: `${updatedCount} issue(s) moved to resolved`,
|
|
|
|
console.error("Error in updateIssueResolutionBasedOnConnectionStatus:", err);
|
|
|
|
supportId,
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error("Error in updateIssueResolutionBasedOnConnectionStatus:", error);
|
|
|
|
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|