diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 30ee2a58..dac55d33 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3,7 +3,7 @@ const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const { Deparments } = require("../models/Department"); -const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures } = require("../models/store"); +const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support } = require("../models/store"); const { Counter, User } = require("../models/User"); const { IotData, Tank } = require("../models/tanks"); const moment = require('moment-timezone'); @@ -2286,3 +2286,128 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => { return reply.code(500).send({ error: "Internal Server Error" }); } }; + +exports.raiseATicket = async (req, reply) => { + try { + const { customerId, connected_to } = req.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." }); + } + + // Debugging log to check the fetched date and current time + console.log("Latest Master Record Date:", latestMasterRecord.date); + + const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata"); + const now = moment.tz("Asia/Kolkata"); + + // Log the current time and difference + console.log("Current Time:", now.format("DD-MM-YYYY HH:mm:ss")); + + const diffInMinutesMaster = now.diff(indiaTime, "minutes"); + + // Log the time difference + console.log("Time difference in minutes (Master):", diffInMinutesMaster); + + // Update masterDisconnected to store details + const masterDisconnected = diffInMinutesMaster > 1 ? [{ + hardwareId: connected_to, + masterName: masterSensor.tankName, + disconnectedAt: moment().tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss"), + }] : []; + + // Log the disconnected status + console.log("Master Disconnected:", masterDisconnected); + + const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === connected_to.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: moment().tz("Asia/Kolkata").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: moment().tz("Asia/Kolkata").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/models/store.js b/src/models/store.js index b1f0999a..676cac82 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -228,6 +228,16 @@ const installationschema = new mongoose.Schema({ departmentName: { type: String, default: null }, zone: { type: String, default: null }, type: { type: String }, + issues: [{ type: Object }], // existing issues array + masterDisconnected: [{ // new field for master disconnected details + hardwareId: String, + masterName: String, + disconnectedAt: String, + }], + disconnectedSlaves: [{ // new field for disconnected slaves details + slaveHardwareId: String, + slaveName: String, + }], profile: { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 1b9f663d..b398763c 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -426,6 +426,24 @@ module.exports = function (fastify, opts, next) { handler: installationController.getIotDataByCustomerAndHardwareId, }); + fastify.get("/api/getraiseAticket/:customerId/:connected_to", { + schema: { + description: "Raise A Ticket for Support", + tags: ["Support"], + summary: "Raise A Ticket for Support", + params: { + type: "object", + properties: { + customerId: { type: "string" }, + connected_to: { type: "string" }, + + }, + required: [ "customerId"], + }, + }, + handler: installationController.raiseATicket, + }); + next(); } \ No newline at end of file