From f82395bb8057de49f80085c01c0b48734728cfee Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 2 Apr 2025 11:44:14 +0530 Subject: [PATCH] get the master list --- src/controllers/installationController.js | 48 ++++++++++++++++++++++- src/routes/installationRoute.js | 18 +++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 05ee2541..4c1e68ad 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4,7 +4,7 @@ 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 { Counter } = require("../models/User"); +const { Counter, User } = require("../models/User"); const { IotData, Tank } = require("../models/tanks"); const fastify = require("fastify")({ logger: true, @@ -773,4 +773,48 @@ exports.masterConnectedSlaveList = async (req, reply) => { console.error("Error fetching tanks:", error); return reply.status(500).send({ success: false, message: "Internal Server Error" }); } -}; \ No newline at end of file +}; + + + +exports.mastrerList = async (req, reply) => { + try { + const { customerId } = req.params; + + // Step 1: Get User and extract buildingName + const user = await User.findOne({ customerId }); + + if (!user) { + return reply.status(404).send({ success: false, message: "User not found" }); + } + + const { buildingName } = user; + + // Step 2: Get Tanks with matching customerId + const tanks = await Tank.find({ customerId }); + + if (!tanks.length) { + return reply.status(404).send({ success: false, message: "No tanks found for this customer" }); + } + + // Step 3: Extract hardwareId from tanks + const hardwareIds = tanks.map(tank => tank.hardwareId); + + // Step 4: Find master tanks in InSensors with customerId filter + const masterTanks = await Insensors.find({ + customerId, // Ensure only this customer’s data is fetched + connected_to: { $in: hardwareIds }, + type: "master" + }); + + if (!masterTanks.length) { + return reply.status(404).send({ success: false, message: "No master tanks found" }); + } + + return reply.send({ success: true, buildingName, data: masterTanks }); + + } catch (error) { + console.error("Error fetching master tanks:", error); + return reply.status(500).send({ success: false, message: "Internal Server Error" }); + } +}; diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 78055fa8..1bb960ce 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -358,6 +358,24 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.masterConnectedSlaveList, }); + + fastify.get("/api/getmasterList/:customerId", { + schema: { + description: "Get masrter connected slave data", + tags: ["Installation"], + summary: "Get masrter List", + params: { + type: "object", + properties: { + customerId: { type: "string" }, + + }, + required: [ "customerId"], + }, + }, + handler: installationController.mastrerList, + }); + next();