From 2073783c050f36f2141a2efbe39ceb9e353da3fc Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 31 Jan 2025 16:05:36 +0530 Subject: [PATCH] added get pumps and users --- src/controllers/tanksController.js | 43 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 26 +++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d536595b..69f68e2f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2691,6 +2691,47 @@ const checkWaterLevel = async (customerId, motorId, fcmToken, receiverTank) => { // }; + + +exports.getPumpsAndUsers = async (req, reply) => { + try { + const { customerId } = req.params; + + // Fetch motor_id from inputConnections of all tanks for the customer + const tanks = await Tank.find({ customerId }, { "connections.inputConnections.motor_id": 1 }); + + const motorIds = tanks.flatMap((tank) => + tank.connections?.inputConnections?.map((conn) => conn.motor_id).filter(Boolean) || [] + ); + + // Fetch username and staff names from User collection + const user = await User.findOne({ customerId }, { username: 1, "staff.staff.name": 1 }); + + const staffNames = user?.staff?.staff?.map((s) => s.name) || []; + const username = user?.username || ""; + + // Include username at the beginning of staffNames and add "manual" at the end + const updatedStaffNames = [username, ...staffNames, "manual"]; + + // Prepare response + const result = { + motorIds, + staffNames: updatedStaffNames, + }; + + return reply.send({ success: true, data: result }); + } catch (error) { + console.error("Error fetching data:", error); + return reply.status(500).send({ success: false, message: "Internal Server Error" }); + } +}; + + + + + + + const monitorWaterLevels = async () => { try { const tanks = await Tank.find({}); @@ -2736,6 +2777,8 @@ async function calculateTotalPumpedWater(customerId, motorId, start_instance_id) } return 0; // Return 0 if no data found } + + exports.motorAction = async (req, reply) => { try { const customerId = req.params.customerId; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index a2f201f5..96a1f82e 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -881,7 +881,31 @@ module.exports = function (fastify, opts, next) { }); - + fastify.get("/api/getPumpsAndUsers/:customerId", { + schema: { + tags: ["Tank"], + description: "This is to Get pumps and users of particular customerId", + summary: "This is to Get pumps and users of particular customerId", + params: { + type: "object", + properties: { + customerId: { + type: "string", + description: "storeId", + }, + }, + required: ["customerId"], + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getPumpsAndUsers, + }); +