diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index ed83eee1..287c211c 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1454,10 +1454,18 @@ exports.createMasterSlaveData = async (req, reply) => { exports.masterConnectedSlaveList = async (req, reply) => { try { - const { connectedTo } = req.params; + const { connectedTo, customerId } = req.params; - // Step 1: Get master device details - const master = await Insensors.findOne({ hardwareId: connectedTo, type: 'master' }).lean(); + if (!connectedTo) { + return reply.status(400).send({ success: false, message: "connectedTo is required" }); + } + + if (!customerId) { + return reply.status(400).send({ success: false, message: "customerId is required" }); + } + + // Step 1: Get master device details (only for this customer) + const master = await Insensors.findOne({ hardwareId: connectedTo, type: 'master', customerId }).lean(); if (!master) { return reply.status(404).send({ success: false, @@ -1465,7 +1473,7 @@ exports.masterConnectedSlaveList = async (req, reply) => { }); } - // Step 2: Get tank metadata for master (including height, length, width) + // Step 2: Get tank metadata for master const tankDetails = await Tank.findOne( { hardwareId: connectedTo }, { @@ -1479,14 +1487,14 @@ exports.masterConnectedSlaveList = async (req, reply) => { const masterTypeOfWater = tankDetails?.typeOfWater || null; - // Step 3: Get slave tanks connected to master - const slaveTanks = await Insensors.find({ connected_to: connectedTo, type: 'slave' }).lean(); + // Step 3: Get slave tanks connected to master and belonging to this customer + const slaveTanks = await Insensors.find({ connected_to: connectedTo, type: 'slave', customerId }).lean(); const slaveCount = slaveTanks.length; // Step 4: Get latest IotData for master const latestIotData = await IotData.findOne({ hardwareId: connectedTo }).sort({ date: -1 }).lean(); - // Step 5: Fetch order to get masterName, location, googleLocation, coordinates + summary counts + // Step 5: Fetch order to get masterName, location, etc. const order = await Order.findOne({ "master_connections.hardwareId": connectedTo }).lean(); let masterOrderInfo = {}; @@ -1515,7 +1523,7 @@ exports.masterConnectedSlaveList = async (req, reply) => { electricals = order.electricals || []; } - // Step 6: Prepare master object for response + // Step 6: Build master response object const masterResponse = { ...master, isMaster: true, @@ -1543,7 +1551,7 @@ exports.masterConnectedSlaveList = async (req, reply) => { t.tankhardwareId === finalHardwareId || t.hardwareId === finalHardwareId ); - // Optionally fetch tank meta for slave too (if needed) + // Fetch slave's tank metadata const slaveTankMeta = await Tank.findOne( { hardwareId: slave.hardwareId }, { height: 1, length: 1, width: 1 } @@ -1561,7 +1569,7 @@ exports.masterConnectedSlaveList = async (req, reply) => { }; })); - // Step 8: Combine master and slaves into one array + // Step 8: Combine into one array const combinedData = [masterResponse, ...processedSlaves]; return reply.send({ diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index ed5e0b69..23da29f6 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -405,7 +405,7 @@ module.exports = function (fastify, opts, next) { handler: installationController.createMasterSlaveData }); - fastify.get("/api/getmasterConnectedSlaveData/:connectedTo", { + fastify.get("/api/getmasterConnectedSlaveData/:connectedTo/:customerId", { schema: { description: "Get masrter connected slave data", tags: ["Installation"], @@ -414,7 +414,7 @@ module.exports = function (fastify, opts, next) { type: "object", properties: { connectedTo: { type: "string" }, - + customerId: { type: "string" }, }, required: [ "connectedTo"], },