changes on fecth issue by category

master^2
Bhaskar 4 months ago
parent 594099bf30
commit c5ab234dd7

@ -6407,6 +6407,114 @@ exports.moveIssueToCategory = async (req, reply) => {
// } // }
// }; // };
// exports.particularCategory = async (req, reply) => {
// try {
// const { supportId, category } = req.params;
// if (!supportId || !category) {
// return reply.code(400).send({ error: "supportId and category are required" });
// }
// const support = await Support.findOne({ supportId }).lean();
// 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}` });
// }
// const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
// // Get all sensors related to the issues (including masters and potential slaves)
// const allRelatedSensors = await Insensors.find({
// hardwareId: { $in: hardwareIds }
// }).lean();
// if (!allRelatedSensors.length) {
// return reply.code(404).send({ message: "No matching devices found for this category." });
// }
// const orderMap = {};
// const customerId = allRelatedSensors[0]?.customerId;
// if (customerId) {
// const orders = await Order.find({ customerId }).lean();
// orders.forEach(order => {
// order.master_connections.forEach(conn => {
// orderMap[conn.hardwareId] = {
// masterName: conn.master_name || null,
// location: conn.location || null
// };
// });
// });
// }
// const disconnectedIssues = [];
// for (const master of allRelatedSensors.filter(i => i.type === "master")) {
// const slaves = await Insensors.find({
// connected_to: master.hardwareId
// }).lean();
// const disconnectedSlaves = slaves.filter(slave => slave.connected_status === "disconnected");
// // If master is disconnected OR any of its slaves are disconnected, include it
// if (master.connected_status === "disconnected" || disconnectedSlaves.length > 0) {
// const slaveDetails = disconnectedSlaves.map(slave => {
// const slaveIssue = issues.find(i => i.hardwareId === slave.hardwareId);
// return {
// hardwareId: slave.hardwareId,
// tankName: slave.tankName || "",
// location: slave.tankLocation || "",
// connected_status: slave.connected_status,
// lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
// connected_to: slave.connected_to || "",
// masterName: orderMap[master.hardwareId]?.masterName || "",
// type: "slave",
// typeOfWater: slave.typeOfWater || "",
// support_lora_last_check_time: null,
// category,
// assignedTo: slaveIssue?.assignedTo || null
// };
// });
// const masterIssue = issues.find(i => i.hardwareId === master.hardwareId);
// disconnectedIssues.push({
// hardwareId: master.hardwareId,
// masterName: orderMap[master.hardwareId]?.masterName || "",
// location: orderMap[master.hardwareId]?.location || "",
// type: "master",
// connected_status: master.connected_status,
// gsm_last_disconnect_time: master.gsm_last_disconnect_time || null,
// support_gsm_last_check_time: null,
// connected_slave_count: slaveDetails.length,
// connected_slaves: slaveDetails,
// category,
// assignedTo: masterIssue?.assignedTo || null
// });
// }
// }
// if (disconnectedIssues.length === 0) {
// return reply.code(404).send({ message: "No disconnected devices found for this category." });
// }
// return reply.send({
// status_code: 200,
// supportId,
// totalMasters: disconnectedIssues.length,
// disconnectedIssues
// });
// } catch (err) {
// console.error("Error in particularCategory:", err);
// return reply.code(500).send({ error: "Internal server error" });
// }
// };
exports.particularCategory = async (req, reply) => { exports.particularCategory = async (req, reply) => {
try { try {
const { supportId, category } = req.params; const { supportId, category } = req.params;
@ -6426,11 +6534,7 @@ exports.particularCategory = async (req, reply) => {
} }
const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean); const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
const allRelatedSensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean();
// Get all sensors related to the issues (including masters and potential slaves)
const allRelatedSensors = await Insensors.find({
hardwareId: { $in: hardwareIds }
}).lean();
if (!allRelatedSensors.length) { if (!allRelatedSensors.length) {
return reply.code(404).send({ message: "No matching devices found for this category." }); return reply.code(404).send({ message: "No matching devices found for this category." });
@ -6453,33 +6557,37 @@ exports.particularCategory = async (req, reply) => {
const disconnectedIssues = []; const disconnectedIssues = [];
for (const master of allRelatedSensors.filter(i => i.type === "master")) { for (const master of allRelatedSensors.filter(i => i.type === "master")) {
const slaves = await Insensors.find({ const slaves = await Insensors.find({ connected_to: master.hardwareId }).lean();
connected_to: master.hardwareId
}).lean();
const disconnectedSlaves = slaves.filter(slave => slave.connected_status === "disconnected");
// If master is disconnected OR any of its slaves are disconnected, include it const slaveDetails = slaves.map(slave => {
if (master.connected_status === "disconnected" || disconnectedSlaves.length > 0) {
const slaveDetails = disconnectedSlaves.map(slave => {
const slaveIssue = issues.find(i => i.hardwareId === slave.hardwareId); const slaveIssue = issues.find(i => i.hardwareId === slave.hardwareId);
const slaveComments = (support.comments || [])
.filter(comment => comment.hardwareId === slave.hardwareId)
.map(c => c.text);
return { return {
hardwareId: slave.hardwareId, hardwareId: slave.tankhardwareId,
tankName: slave.tankName || "", tankName: slave.tankName || "",
location: slave.tankLocation || "", location: slave.tankLocation || "",
connected_status: slave.connected_status, connected_status: slave.connected_status,
connected_lora_time: slave.connected_lora_time || "",
connected_lora_date: slave.connected_lora_date || "",
lora_last_check_time: slave.lora_last_check_time || null,
lora_last_disconnect_time: slave.lora_last_disconnect_time || null, lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
connected_to: slave.connected_to || "", connected_to: slave.connected_to || "",
masterName: orderMap[master.hardwareId]?.masterName || "", masterName: orderMap[master.hardwareId]?.masterName || "",
type: "slave", type: "slave",
typeOfWater: slave.typeOfWater || "", typeOfWater: slave.typeOfWater || "",
support_lora_last_check_time: null, support_lora_last_check_time: slave.support_lora_last_check_time || null,
category, team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time || null,
assignedTo: slaveIssue?.assignedTo || null comments: slaveComments
}; };
}); });
const masterIssue = issues.find(i => i.hardwareId === master.hardwareId); const masterIssue = issues.find(i => i.hardwareId === master.hardwareId);
const masterComments = (support.comments || [])
.filter(comment => comment.hardwareId === master.hardwareId)
.map(c => c.text);
disconnectedIssues.push({ disconnectedIssues.push({
hardwareId: master.hardwareId, hardwareId: master.hardwareId,
@ -6487,23 +6595,24 @@ exports.particularCategory = async (req, reply) => {
location: orderMap[master.hardwareId]?.location || "", location: orderMap[master.hardwareId]?.location || "",
type: "master", type: "master",
connected_status: master.connected_status, connected_status: master.connected_status,
gsm_last_check_time: master.gsm_last_check_time || null,
gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, gsm_last_disconnect_time: master.gsm_last_disconnect_time || null,
support_gsm_last_check_time: null, connected_gsm_date: master.connected_gsm_date || "",
connected_gsm_time: master.connected_gsm_time || "",
connected_lora_date: master.connected_lora_date || "",
connected_lora_time: master.connected_lora_time || "",
support_gsm_last_check_time: master.support_gsm_last_check_time || null,
team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time || null,
connected_slave_count: slaveDetails.length, connected_slave_count: slaveDetails.length,
connected_slaves: slaveDetails, connected_slaves: slaveDetails,
category, comments: masterComments
assignedTo: masterIssue?.assignedTo || null
}); });
} }
}
if (disconnectedIssues.length === 0) {
return reply.code(404).send({ message: "No disconnected devices found for this category." });
}
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
supportId, supportId,
customerId,
totalMasters: disconnectedIssues.length, totalMasters: disconnectedIssues.length,
disconnectedIssues disconnectedIssues
}); });
@ -6516,7 +6625,6 @@ exports.particularCategory = async (req, reply) => {
// exports.assignCategorizeIssue = async (request, reply) => { // exports.assignCategorizeIssue = async (request, reply) => {
// const { supportId } = request.params; // const { supportId } = request.params;
// const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body; // const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;

Loading…
Cancel
Save