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();
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(
(issue) => issue.category === category
);
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}` });
}
// ✅ Extract master hardwareIds from categorized issues
const hardwareIds = [];
for (const issue of issues) {
if (issue.hardwareId) hardwareIds.push(issue.hardwareId);
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 } }
]
const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
const insensors = await Insensors.find({
hardwareId: { $in: hardwareIds },
connected_status: "disconnected"
}).lean();
if (!sensors.length) {
return reply.code(404).send({ message: "No related sensors found." });
if (!insensors.length) {
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 = {};
orders.forEach(order => {
order.master_connections.forEach(connection => {
orderMap[connection.hardwareId] = {
masterName: connection.master_name || null,
location: connection.location || null
};
const customerId = insensors[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
};
});
});
});
// ✅ 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 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);
const disconnectedIssues = [];
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 tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
const loraDiff = now.diff(tankTime, "minutes");
loraMessage = loraDiff <= 1 ? "LORA is connected" : "LORA is not connected";
}
const slaveDetails = slaves.map(slave => ({
hardwareId: slave.hardwareId,
tankName: slave.tankName || "",
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 {
tankhardwareId: slaveId,
tankName: slave.tankName ?? null,
tankLocation: slave.tankLocation ?? null,
masterName: orderInfo.masterName ?? null,
location: orderInfo.location ?? null,
loraMessage,
latestTankData: matchedTank ?? null
};
disconnectedIssues.push({
hardwareId: master.hardwareId,
masterName: orderMap[master.hardwareId]?.masterName || "",
location: orderMap[master.hardwareId]?.location || "",
type: "master",
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,
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({
status_code: 200,
category,
message: "IoT data fetched for category",
data: enrichedMasters
supportId,
totalMasters: disconnectedIssues.length,
disconnectedIssues
});
} catch (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