diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 4b65e91b..1d2bbd48 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4114,14 +4114,15 @@ exports.raiseATicketSlave = async (req, reply) => { exports.getDisconnectedIssuesBySupportId = async (req, reply) => { try { - const { supportId } = req.params; - if (!supportId) { - return reply.code(400).send({ error: "supportId is required" }); + const { supportId, customerId } = req.params; + + if (!supportId || !customerId) { + return reply.code(400).send({ error: "supportId and customerId are required" }); } const supportRecord = await Support.findOne({ supportId }).lean(); if (!supportRecord) { - return reply.code(404).send({ message: "No support record found for this supportId" }); + return reply.code(404).send({ message: "No support record found for this supportId and customerId" }); } const allIssues = supportRecord.issues || []; @@ -4135,6 +4136,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const hardwareIds = [...hardwareSet]; const sensors = await Insensors.find({ + customerId, $or: [ { hardwareId: { $in: hardwareIds } }, { tankhardwareId: { $in: hardwareIds } } @@ -4147,16 +4149,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor; } - let customerId = supportRecord.customerId; - if (!customerId) { - const firstSensor = sensors.find(sensor => sensor.customerId); - if (firstSensor) { - customerId = firstSensor.customerId; - } else { - return reply.code(404).send({ message: "Unable to determine customerId from support or sensors." }); - } - } - const orders = await Order.find({ customerId }).lean(); const orderMap = {}; @@ -4216,7 +4208,8 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { const master = masterMap[masterSensor.hardwareId]; const connectedSlaves = await Insensors.find({ connected_to: masterSensor.hardwareId, - type: "slave" + type: "slave", + customerId }).lean(); const slaveSet = new Set(master.connected_slaves.map(s => s.hardwareId)); @@ -4260,8 +4253,9 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { return reply.send({ status_code: 200, supportId, + customerId, totalMasters: Object.keys(masterMap).length, - disconnectedIssues: Object.values(masterMap) // includes connected too + disconnectedIssues: Object.values(masterMap) }); } catch (error) { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 7b4361c2..4501d724 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -546,7 +546,7 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.raiseATicketSlave, }); - fastify.get("/api/getAllDisconnectedIsuues/:supportId", { + fastify.get("/api/getAllDisconnectedIsuues/:supportId/:customerId", { schema: { description: "Get All disconnected list for Support", tags: ["Support"], @@ -555,7 +555,7 @@ module.exports = function (fastify, opts, next) { type: "object", properties: { supportId: { type: "string" }, - + customerId: { type: "string" }, }, required: [ "supportId"], },