diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 32a2ef59..7598623d 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2414,6 +2414,91 @@ exports.raiseATicket = async (req, reply) => { }; +exports.raiseATicketBuildingDetails = async (req, reply) => { + try { + const { customerId, connected_to, installationId } = req.params; + + if (!customerId || !connected_to || !installationId) { + return reply.code(400).send({ error: "customerId, connected_to, and installationId are required" }); + } + + const customer = await User.findOne({ customerId, installationId }).lean(); + if (!customer) { + return reply.code(404).send({ message: "Customer not found." }); + } + + 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 formattedNow = now.format("YYYY-MM-DD HH:mm:ss"); + + const diffInMinutesMaster = now.diff(indiaTime, "minutes"); + + if (diffInMinutesMaster > 1) { + await Insensors.updateOne( + { hardwareId: connected_to }, + { $set: { lastTicketRaisedAt: formattedNow } } + ); + } + + const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === connected_to.trim()); + + 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) { + await Insensors.updateOne( + { hardwareId: slaveId }, + { $set: { lastTicketRaisedAt: formattedNow } } + ); + } + } + } + + await Support.updateOne( + { supportId: "AWHYSU64" }, + { $set: { lastTicketRaisedAt: formattedNow } } + ); + + // Fetch updated `lastTicketRaisedAt` values + const updatedMasterSensor = await Insensors.findOne({ hardwareId: connected_to }).lean(); + const updatedSupport = await Support.findOne({ supportId: "AWHYSU64" }).lean(); + console.log("updatedMasterSensor",updatedMasterSensor) + return reply.send({ + status_code: 200, + customer, + lastTicketRaisedAt: { + masterSensor: updatedMasterSensor?.lastTicketRaisedAt || null, + support: updatedSupport?.lastTicketRaisedAt || null + } + }); + + } catch (error) { + console.error("Error raising ticket:", error); + return reply.code(500).send({ error: "Internal server error" }); + } +}; + + exports.raiseATicketSlave = async (req, reply) => { try { const { customerId, connected_to, tankHardwareId } = req.params; // Now tankHardwareId from params diff --git a/src/models/store.js b/src/models/store.js index ecb965ca..ae4feec9 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -228,6 +228,7 @@ const installationschema = new mongoose.Schema({ departmentName: { type: String, default: null }, zone: { type: String, default: null }, type: { type: String }, + lastTicketRaisedAt: {type : String}, issues: [{ type: Object }], // existing issues array masterDisconnected: [{ // new field for master disconnected details hardwareId: String, @@ -468,7 +469,8 @@ lora_last_disconnect_time : { type: String, default: null }, connecting_to_slave: { result: String }, data_sending: { result: String }, connected_slave_count: {type : String}, - distance_check: { +lastTicketRaisedAt: { type: String }, +distance_check: { result: String, steps: [ { step: Number, result: String } diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 31f989c7..7a8b7fee 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/getraiseAticketBuildingDetails/:customerId/:connected_to/:installationId", { + schema: { + description: "Raise A Ticket for Support Building Details", + tags: ["Support"], + summary: "Raise A Ticket for Support Building Details", + params: { + type: "object", + properties: { + customerId: { type: "string" }, + connected_to: { type: "string" }, + installationId: { type: "string" }, + + }, + required: [ "customerId"], + }, + }, + handler: installationController.raiseATicketBuildingDetails, + }); + + fastify.get("/api/getraiseAticketslave/:customerId/:connected_to/:tankHardwareId", { schema: { description: "Raise A Ticket particular slave for Support",