master^2
Bhaskar 5 months ago
parent 2b1bac2a59
commit 40be95f6b6

@ -4136,127 +4136,89 @@ exports.particularCategory = async (req, reply) => {
const support = await Support.findOne({ supportId }).lean(); const support = await Support.findOne({ supportId }).lean();
if (!support) { if (!support) {
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}` });
} }
// ✅ Extract master hardwareIds from categorized issues const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
const hardwareIds = []; const insensors = await Insensors.find({
for (const issue of issues) { hardwareId: { $in: hardwareIds },
if (issue.hardwareId) hardwareIds.push(issue.hardwareId); connected_status: "disconnected"
if (Array.isArray(issue.hardwareIds)) hardwareIds.push(...issue.hardwareIds);
}
const uniqueHardwareIds = [...new Set(hardwareIds.map(id => id.trim()))];
if (!uniqueHardwareIds.length) {
return reply.code(404).send({ message: "No hardware IDs found in issues." });
}
// ✅ Find sensors involved
const sensors = await Insensors.find({
$or: [
{ hardwareId: { $in: uniqueHardwareIds } },
{ connected_to: { $in: uniqueHardwareIds } }
]
}).lean(); }).lean();
if (!sensors.length) { if (!insensors.length) {
return reply.code(404).send({ message: "No related sensors found." }); return reply.code(404).send({ message: "No disconnected devices found for this category." });
} }
// ✅ Get customerId from first issue (assuming same customer)
const customerId = support.customerId || sensors[0]?.customerId;
// ✅ Get order map
const orders = await Order.find({ customerId }).lean();
const orderMap = {}; const orderMap = {};
const customerId = insensors[0]?.customerId;
if (customerId) {
const orders = await Order.find({ customerId }).lean();
orders.forEach(order => { orders.forEach(order => {
order.master_connections.forEach(connection => { order.master_connections.forEach(conn => {
orderMap[connection.hardwareId] = { orderMap[conn.hardwareId] = {
masterName: connection.master_name || null, masterName: conn.master_name || null,
location: connection.location || null location: conn.location || null
}; };
}); });
}); });
// ✅ Get master hardwareIds
const masterHardwareIds = uniqueHardwareIds;
// ✅ Build enriched master list
const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => {
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
const orderInfo = orderMap[hardwareId] || {};
if (!latestRecord) {
return {
hardwareId,
message: "No IoT data found",
masterName: orderInfo.masterName ?? null,
location: orderInfo.location ?? null,
tanks: []
};
} }
// ✅ Check GSM status const disconnectedIssues = [];
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
const now = moment.tz("Asia/Kolkata");
const diffInMinutes = now.diff(indiaTime, "minutes");
const gsmConnected = diffInMinutes <= 1;
const message = gsmConnected ? "GSM is connected" : "GSM is not connected";
// ✅ Get slaves connected to this master
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
// ✅ Prepare tank info
const tanks = connectedSlaves.map(slave => {
const slaveId = slave.tankhardwareId?.trim();
const matchedTank = latestRecord.tanks?.find(t => t.tankhardwareId === slaveId);
let loraMessage = "LORA is not connected"; for (const master of insensors.filter(i => i.type === "master")) {
const slaves = await Insensors.find({
connected_to: master.hardwareId,
connected_status: "disconnected"
}).lean();
if (matchedTank?.date && matchedTank.tankHeight !== "0") { const slaveDetails = slaves.map(slave => ({
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); hardwareId: slave.hardwareId,
const loraDiff = now.diff(tankTime, "minutes"); tankName: slave.tankName || "",
loraMessage = loraDiff <= 1 ? "LORA is connected" : "LORA is not connected"; location: slave.tankLocation || "",
} connected_status: slave.connected_status,
connected_lora_time: slave.connected_lora_time || null,
connected_lora_date: slave.connected_lora_date || null,
lora_last_check_time: slave.lora_last_check_time || null,
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
}));
return { disconnectedIssues.push({
tankhardwareId: slaveId, hardwareId: master.hardwareId,
tankName: slave.tankName ?? null, masterName: orderMap[master.hardwareId]?.masterName || "",
tankLocation: slave.tankLocation ?? null, location: orderMap[master.hardwareId]?.location || "",
masterName: orderInfo.masterName ?? null, type: "master",
location: orderInfo.location ?? null, connected_status: master.connected_status,
loraMessage, gsm_last_check_time: master.gsm_last_check_time || null,
latestTankData: matchedTank ?? null gsm_last_disconnect_time: master.gsm_last_disconnect_time || null,
}; connected_gsm_date: master.connected_gsm_date || null,
connected_gsm_time: master.connected_gsm_time || null,
connected_lora_date: master.connected_lora_date || null,
connected_lora_time: master.connected_lora_time || null,
support_gsm_last_check_time: null,
connected_slave_count: slaveDetails.length,
connected_slaves: slaveDetails
}); });
}
return {
hardwareId,
message,
masterName: orderInfo.masterName ?? null,
location: orderInfo.location ?? null,
tanks
};
}));
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
category, supportId,
message: "IoT data fetched for category", totalMasters: disconnectedIssues.length,
data: enrichedMasters disconnectedIssues
}); });
} catch (err) { } catch (err) {
console.error("Error in particularCategory:", err); console.error("Error in particularCategory:", err);
return reply.code(500).send({ error: "Internal Server Error" }); return reply.code(500).send({ error: "Internal server error" });
} }
}; };

Loading…
Cancel
Save