ashok 3 months ago
commit 24eaeba13e

@ -9126,84 +9126,93 @@ exports.moveIssueToCategory = async (req, reply) => {
let issueMoved = false; let issueMoved = false;
const nowTime = moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss"); const nowTime = moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
// Ensure categorizedIssues exists
if (!Array.isArray(support.categorizedIssues)) { if (!Array.isArray(support.categorizedIssues)) {
support.categorizedIssues = []; support.categorizedIssues = [];
} }
// Step 1: Try moving from support.issues (original logic)
const index = support.issues.findIndex((issue) => { const index = support.issues.findIndex((issue) => {
if (issue.hardwareId === hardwareId) return true; if (issue.hardwareId === hardwareId) return true;
if (Array.isArray(issue.hardwareIds) && issue.hardwareIds.includes(hardwareId)) return true; if (Array.isArray(issue.hardwareIds) && issue.hardwareIds.includes(hardwareId)) return true;
return false; return false;
}); });
if (index === -1) { if (index !== -1) {
return reply.code(404).send({ message: "No matching issue found to move" }); const issue = support.issues[index];
}
const issue = support.issues[index];
// Normalize type if needed
const normalizedType =
issue.type === "GSM or LoRa Disconnected" ? "GSM Disconnected" : issue.type;
if (issue.hardwareId === hardwareId) { // Normalize type if needed
// Master issue const normalizedType =
issue.movedToCategory = true; issue.type === "GSM or LoRa Disconnected" ? "GSM Disconnected" : issue.type;
support.categorizedIssues.push({ if (issue.hardwareId === hardwareId) {
type: normalizedType, // Master issue
hardwareId: issue.hardwareId, issue.movedToCategory = true;
masterHardwareId: issue.masterHardwareId || issue.hardwareId,
category,
movedAt: nowTime,
movedToCategory: true,
});
support.issues.splice(index, 1);
issueMoved = true;
} else {
// Slave issue
const slaveIndex = issue.hardwareIds.indexOf(hardwareId);
if (slaveIndex !== -1) {
const slaveName = issue.slaveNames?.[slaveIndex] || "Unknown";
support.categorizedIssues.push({ support.categorizedIssues.push({
type: normalizedType, type: normalizedType,
hardwareId, hardwareId: issue.hardwareId,
masterHardwareId: issue.masterHardwareId || issue.hardwareId, masterHardwareId: issue.masterHardwareId || issue.hardwareId,
slaveName,
category, category,
movedAt: nowTime, movedAt: nowTime,
movedToCategory: true, movedToCategory: true,
}); });
issue.hardwareIds.splice(slaveIndex, 1); support.issues.splice(index, 1);
issue.slaveNames.splice(slaveIndex, 1); issueMoved = true;
} else {
// Slave issue
const slaveIndex = issue.hardwareIds.indexOf(hardwareId);
if (slaveIndex !== -1) {
const slaveName = issue.slaveNames?.[slaveIndex] || "Unknown";
support.categorizedIssues.push({
type: normalizedType,
hardwareId,
masterHardwareId: issue.masterHardwareId || issue.hardwareId,
slaveName,
category,
movedAt: nowTime,
movedToCategory: true,
});
issue.hardwareIds.splice(slaveIndex, 1);
issue.slaveNames.splice(slaveIndex, 1);
if (issue.hardwareIds.length === 0) {
support.issues.splice(index, 1);
}
// Remove full issue if no more slaves are left issueMoved = true;
if (issue.hardwareIds.length === 0) {
support.issues.splice(index, 1);
} }
issueMoved = true;
} }
} }
if (issueMoved) { // Step 2: If not found in support.issues, try to update existing categorized issue
await support.save(); if (!issueMoved) {
const categorized = support.categorizedIssues.find(
(i) => i.hardwareId === hardwareId
);
// 🧹 Cleanup any duplicate issues with same hardwareId (master or slave) if (categorized) {
support.issues = support.issues.filter((i) => { categorized.category = category;
const isMaster = i.hardwareId === hardwareId; categorized.movedAt = nowTime;
const isSlave = Array.isArray(i.hardwareIds) && i.hardwareIds.includes(hardwareId); await support.save();
return !isMaster && !isSlave; return reply.send({ message: "Category updated for already categorized issue" });
}); }
await support.save(); return reply.code(404).send({ message: "No matching issue found to move or update" });
return reply.send({ message: "Issue moved to category successfully" });
} else {
return reply.code(404).send({ message: "No matching issue found to move" });
} }
// Cleanup duplicates
support.issues = support.issues.filter((i) => {
const isMaster = i.hardwareId === hardwareId;
const isSlave = Array.isArray(i.hardwareIds) && i.hardwareIds.includes(hardwareId);
return !isMaster && !isSlave;
});
await support.save();
return reply.send({ message: "Issue moved to category successfully" });
} catch (err) { } catch (err) {
console.error("Error moving issue:", err); console.error("Error moving issue:", err);
return reply.code(500).send({ error: "Internal Server Error" }); return reply.code(500).send({ error: "Internal Server Error" });
@ -9211,6 +9220,7 @@ exports.moveIssueToCategory = async (req, reply) => {
}; };
// exports.particularCategory = async (req, reply) => { // exports.particularCategory = async (req, reply) => {
// try { // try {
// const { supportId, category } = req.params; // const { supportId, category } = req.params;

Loading…
Cancel
Save