diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index dac55d33..3ce5788b 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2411,3 +2411,118 @@ exports.raiseATicket = async (req, reply) => { } }; +exports.raiseATicketSlave = async (req, reply) => { + try { + const { customerId, connected_to, tankHardwareId } = req.params; // Now tankHardwareId from params + + if (!customerId || !connected_to) { + return reply.code(400).send({ error: "customerId and connected_to are required" }); + } + + const sensors = await Insensors.find({ customerId }); + + if (!sensors.length) { + return reply.code(404).send({ message: "No sensors found for this customer." }); + } + + const masterSensor = sensors.find(s => (s.hardwareId?.trim() === connected_to.trim())); + + if (!masterSensor) { + return reply.code(404).send({ message: "Master hardwareId not found." }); + } + + const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean(); + + if (!latestMasterRecord) { + return reply.code(404).send({ message: "No IoT data found for this hardwareId." }); + } + + const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata"); + const now = moment.tz("Asia/Kolkata"); + + const diffInMinutesMaster = now.diff(indiaTime, "minutes"); + + const masterDisconnected = diffInMinutesMaster > 1 ? [{ + hardwareId: connected_to, + masterName: masterSensor.tankName, + disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss"), + }] : []; + + let connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === connected_to.trim()); + + // If tankHardwareId is provided, filter for that specific slave + if (tankHardwareId) { + connectedSlaves = connectedSlaves.filter(s => s.hardwareId?.trim() === tankHardwareId.trim()); + } + + const disconnectedSlaves = []; + + for (const slave of connectedSlaves) { + const slaveId = slave.hardwareId?.trim(); + const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId); + + if (matchedTank && matchedTank.date) { + const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); + const loraDiffInMinutes = now.diff(tankTime, "minutes"); + + if (loraDiffInMinutes > 1) { + disconnectedSlaves.push({ + slaveHardwareId: slaveId, + slaveName: slave.tankName + }); + } + } + } + + const issuesToAdd = []; + + if (masterDisconnected.length > 0) { + issuesToAdd.push({ + type: "GSM Disconnected", + hardwareId: connected_to, + message: `Master GSM disconnected - ${connected_to}`, + disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss") + }); + } + + if (disconnectedSlaves.length > 0) { + issuesToAdd.push({ + type: "LoRa Disconnected", + hardwareIds: disconnectedSlaves.map(d => d.slaveHardwareId), + slaveNames: disconnectedSlaves.map(d => d.slaveName), + message: "Slaves LoRa disconnected", + disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss") + }); + } + + if (issuesToAdd.length > 0) { + const supportRecord = await Support.findOne({ supportId: "AWHYSU64" }); + + if (supportRecord) { + await Support.findOneAndUpdate( + { _id: supportRecord._id }, + { + $push: { + issues: { $each: issuesToAdd } + }, + updatedAt: new Date() + }, + { new: true } + ); + } else { + console.error("Support record not found for supportId AWHYSU64"); + } + } + + return reply.send({ + status_code: 200, + message: "Checked connection and updated support if needed.", + masterDisconnected, + disconnectedSlaves + }); + + } catch (error) { + console.error("Error raising ticket:", error); + return reply.code(500).send({ error: "Internal server error" }); + } +}; diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index b398763c..c0b40db6 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -444,6 +444,26 @@ module.exports = function (fastify, opts, next) { handler: installationController.raiseATicket, }); + fastify.get("/api/getraiseAticket/:customerId/:connected_to/:tankHardwareId", { + schema: { + description: "Raise A Ticket particular slave for Support", + tags: ["Support"], + summary: "Raise A Ticket particular slave for Support", + params: { + type: "object", + properties: { + customerId: { type: "string" }, + connected_to: { type: "string" }, + tankHardwareId: { type: "string" }, + + + }, + required: [ "customerId"], + }, + }, + handler: installationController.raiseATicketSlave, + }); + next(); } \ No newline at end of file