|
|
@ -9650,3 +9650,81 @@ exports.resolvedIssuesForSupport = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.resolveIssueIfAllConnected = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { supportId } = req.params;
|
|
|
|
|
|
|
|
const { hardwareId, reason, category } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!supportId || !hardwareId || !reason) {
|
|
|
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
|
|
|
message: "supportId (path), hardwareId and reason (body) are required",
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const support = await Support.findOne({ supportId });
|
|
|
|
|
|
|
|
if (!support) {
|
|
|
|
|
|
|
|
return reply.code(404).send({ message: "Support record not found" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(support.categorizedIssues)) support.categorizedIssues = [];
|
|
|
|
|
|
|
|
if (!Array.isArray(support.resolvedIssues)) support.resolvedIssues = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const categorizedIndex = support.categorizedIssues.findIndex(
|
|
|
|
|
|
|
|
(issue) => issue.hardwareId === hardwareId
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (categorizedIndex === -1) {
|
|
|
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
|
|
|
message: `No categorized issue found for hardwareId: ${hardwareId}`,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const issue = support.categorizedIssues[categorizedIndex];
|
|
|
|
|
|
|
|
const masterHardwareId = issue.masterHardwareId || issue.hardwareId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const master = await Insensors.findOne({ hardwareId: masterHardwareId }).lean();
|
|
|
|
|
|
|
|
if (!master) {
|
|
|
|
|
|
|
|
return reply
|
|
|
|
|
|
|
|
.code(404)
|
|
|
|
|
|
|
|
.send({ message: `Master device not found with hardwareId ${masterHardwareId}` });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const slaves = await Insensors.find({ connected_to: masterHardwareId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const allConnected =
|
|
|
|
|
|
|
|
master.connected_status === "connected" &&
|
|
|
|
|
|
|
|
slaves.every((s) => s.connected_status === "connected");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!allConnected) {
|
|
|
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
|
|
|
message: "Not all devices are connected. Cannot resolve issue.",
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const nowTime = moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
support.resolvedIssues.push({
|
|
|
|
|
|
|
|
type: issue.type,
|
|
|
|
|
|
|
|
hardwareId: issue.hardwareId,
|
|
|
|
|
|
|
|
masterHardwareId,
|
|
|
|
|
|
|
|
category: category || "Resolved", // ✅ use passed category or fallback to "Resolved"
|
|
|
|
|
|
|
|
resolvedAt: nowTime,
|
|
|
|
|
|
|
|
originalMovedAt: issue.movedAt || null,
|
|
|
|
|
|
|
|
movedToCategory: true,
|
|
|
|
|
|
|
|
slaveName: issue.slaveName || null,
|
|
|
|
|
|
|
|
currentlyResolved: true,
|
|
|
|
|
|
|
|
invalidatedAt: null,
|
|
|
|
|
|
|
|
reason: reason,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
support.categorizedIssues.splice(categorizedIndex, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await support.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({ message: "Issue resolved and moved to resolvedIssues", resolvedAt: nowTime });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error resolving issue:", err);
|
|
|
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|