From eca056dc714dcd2395a35f526ad4c5b2bda06a4a Mon Sep 17 00:00:00 2001 From: varun Date: Wed, 8 May 2024 07:21:37 -0400 Subject: [PATCH] This is to get Connections Info Of ParticularTank --- src/controllers/tanksController.js | 49 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 39 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index a77b5ac4..61dc4b8d 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -169,6 +169,55 @@ exports.deleteTanksInfo = async (req, reply) => { } }; +exports.getConnectionsInfoOfParticularTank = async (req, reply) => { + try { + const customerId = req.params.customerId; + const tankName = req.body.tankName; + const tankLocation = req.body.tankLocation.toLowerCase(); + console.log(customerId,tankName,tankLocation) + + // Find the specific tank + const mainTank = await Tank.findOne({ + tankName: tankName, + customerId: customerId, + tankLocation: tankLocation + }); + + if (!mainTank) { + return reply.send({ status_code: 404, error: "Main tank not found" }); + } + + // Function to find tanks by name and location + const findTankDetails = async (connection, type) => { + return await Tank.findOne({ + tankName: connection.inputConnections || connection.outputConnections, + tankLocation: connection.input_type || connection.output_type, + customerId: customerId + }).select('hardwareId tankName capacity height connections waterlevel'); + }; + + // Get all related input and output tanks + const inputTankPromises = mainTank.connections.inputConnections.map(input => findTankDetails(input, 'input')); + const outputTankPromises = mainTank.connections.outputConnections.map(output => findTankDetails(output, 'output')); + + // Resolve all promises + const inputTanks = await Promise.all(inputTankPromises); + const outputTanks = await Promise.all(outputTankPromises); + + // Combine data from the main tank and its connections + const result = { + mainTank: mainTank, + inputConnections: inputTanks.filter(tank => tank !== null), // Filter out null results + outputConnections: outputTanks.filter(tank => tank !== null) // Filter out null results + }; + + reply.send({ status_code: 200, data: result }); + } catch (err) { + throw boom.boomify(err); + } +}; + + //get tanks data by passing username exports.getTank = async (req, reply) => { try { diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 4d1aa186..a7c841c3 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -136,6 +136,45 @@ module.exports = function (fastify, opts, next) { handler: tanksController.deleteTanksInfo, }); + + fastify.route({ + method: "PUT", + url: "/api/getConnectionsInfoOfParticularTank/:customerId", + schema: { + tags: ["Tank"], + summary: "This is to get Connections Info Of ParticularTank", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + body: { + type: "object", + // required: ['phone'], + properties: { + tankName: {type: 'string'}, + tankLocation: { type: "string", default: null }, + + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getConnectionsInfoOfParticularTank, + }); + + fastify.get("/api/getTanks", { schema: {