fetch by categories

master^2
Bhaskar 4 months ago
parent 1f5b56b943
commit 594099bf30

@ -6410,10 +6410,9 @@ 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;
const { customerId } = req.query; // ✅ from query string
if (!customerId || !supportId || !category) { if (!supportId || !category) {
return reply.code(400).send({ error: "customerId, supportId, and category are required" }); return reply.code(400).send({ error: "supportId and category are required" });
} }
const support = await Support.findOne({ supportId }).lean(); const support = await Support.findOne({ supportId }).lean();
@ -6421,87 +6420,90 @@ exports.particularCategory = async (req, reply) => {
return reply.code(404).send({ message: "Support record not found" }); return reply.code(404).send({ message: "Support record not found" });
} }
const issues = (support.categorizedIssues || []).filter( const issues = (support.categorizedIssues || []).filter(issue => issue.category === category);
issue => issue.category === category
);
if (issues.length === 0) { if (issues.length === 0) {
return reply.code(404).send({ message: `No issues found for category: ${category}` }); return reply.code(404).send({ message: `No issues found for category: ${category}` });
} }
const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean); const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
const insensors = await Insensors.find({ // Get all sensors related to the issues (including masters and potential slaves)
customerId, const allRelatedSensors = await Insensors.find({
hardwareId: { $in: hardwareIds }, hardwareId: { $in: hardwareIds }
connected_status: "disconnected"
}).lean(); }).lean();
if (!insensors.length) { if (!allRelatedSensors.length) {
return reply.code(404).send({ message: "No disconnected devices found for this category." }); return reply.code(404).send({ message: "No matching devices found for this category." });
} }
const orderMap = {}; const orderMap = {};
const orders = await Order.find({ customerId }).lean(); const customerId = allRelatedSensors[0]?.customerId;
orders.forEach(order => { if (customerId) {
order.master_connections.forEach(conn => { const orders = await Order.find({ customerId }).lean();
orderMap[conn.hardwareId] = { orders.forEach(order => {
masterName: conn.master_name || null, order.master_connections.forEach(conn => {
location: conn.location || null orderMap[conn.hardwareId] = {
}; masterName: conn.master_name || null,
location: conn.location || null
};
});
}); });
}); }
const disconnectedIssues = []; const disconnectedIssues = [];
for (const master of insensors.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({
customerId, connected_to: master.hardwareId
connected_to: master.hardwareId,
connected_status: "disconnected"
}).lean(); }).lean();
const slaveDetails = slaves.map(slave => { const disconnectedSlaves = slaves.filter(slave => slave.connected_status === "disconnected");
const slaveIssue = issues.find(i => i.hardwareId === slave.hardwareId);
return { // If master is disconnected OR any of its slaves are disconnected, include it
hardwareId: slave.tankhardwareId, if (master.connected_status === "disconnected" || disconnectedSlaves.length > 0) {
tankName: slave.tankName || "", const slaveDetails = disconnectedSlaves.map(slave => {
location: slave.tankLocation || "", const slaveIssue = issues.find(i => i.hardwareId === slave.hardwareId);
connected_status: slave.connected_status, return {
lora_last_disconnect_time: slave.lora_last_disconnect_time || null, hardwareId: slave.hardwareId,
team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time, tankName: slave.tankName || "",
connected_to: slave.connected_to || "", 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 || "", masterName: orderMap[master.hardwareId]?.masterName || "",
type: "slave", location: orderMap[master.hardwareId]?.location || "",
typeOfWater: slave.typeOfWater || "", type: "master",
support_lora_last_check_time: slave.support_lora_last_check_time, 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, category,
assignedTo: slaveIssue?.assignedTo || null assignedTo: masterIssue?.assignedTo || null
}; });
}); }
}
const masterIssue = issues.find(i => i.hardwareId === master.hardwareId);
disconnectedIssues.push({ if (disconnectedIssues.length === 0) {
hardwareId: master.hardwareId, return reply.code(404).send({ message: "No disconnected devices found for this category." });
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,
team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time,
support_gsm_last_check_time: master.support_gsm_last_check_time,
connected_slave_count: slaveDetails.length,
connected_slaves: slaveDetails,
category,
assignedTo: masterIssue?.assignedTo || null
});
} }
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
supportId, supportId,
customerId,
totalMasters: disconnectedIssues.length, totalMasters: disconnectedIssues.length,
disconnectedIssues disconnectedIssues
}); });
@ -6514,6 +6516,7 @@ 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