From 2b1bac2a59906f811dae9aea5b14a34d6b07103e Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 20 May 2025 14:59:28 +0530 Subject: [PATCH] changes --- src/controllers/installationController.js | 163 ++++++++++++++++++++-- 1 file changed, 150 insertions(+), 13 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index a4e531e0..d8f66635 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4105,21 +4105,158 @@ exports.moveIssueToCategory = async (req, reply) => { }; -exports.particularCategory = async (req, reply) => { - const { supportId, category } = req.params; +// exports.particularCategory = async (req, reply) => { +// const { supportId, category } = req.params; - const support = await Support.findOne({ supportId }); - if (!support) { - return reply.code(404).send({ message: 'Support record not found' }); - } +// const support = await Support.findOne({ supportId }); +// if (!support) { +// 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}` }); - } +// if (issues.length === 0) { +// return reply.code(404).send({ message: `No issues found for category: ${category}` }); +// } + +// return reply.send({ category, issues }); +// }; + + + +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' }); + } - return reply.send({ category, issues }); + 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 } } + ] + }).lean(); + + if (!sensors.length) { + return reply.code(404).send({ message: "No related sensors found." }); + } + + // ✅ 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 + }; + }); + }); + + // ✅ 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); + + let loraMessage = "LORA is not connected"; + + 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"; + } + + return { + tankhardwareId: slaveId, + tankName: slave.tankName ?? null, + tankLocation: slave.tankLocation ?? null, + masterName: orderInfo.masterName ?? null, + location: orderInfo.location ?? null, + loraMessage, + latestTankData: matchedTank ?? null + }; + }); + + 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 + }); + + } catch (err) { + console.error("Error in particularCategory:", err); + return reply.code(500).send({ error: "Internal Server Error" }); + } };