From 9cc4dc0a2ff44af424a205893cec85b8ebacb117 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 12 Mar 2025 15:02:34 +0530 Subject: [PATCH] allocated sensors --- src/controllers/installationController.js | 43 +++++++++++++++++++++-- src/routes/installationRoute.js | 18 ++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index b5774cde..3f45fdb9 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3,9 +3,9 @@ const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const { Deparments } = require("../models/Department"); -const { Install, SensorStock, SensorQuotation, Order } = require("../models/store"); +const { Install, SensorStock, SensorQuotation, Order, Insensors } = require("../models/store"); const { Counter } = require("../models/User"); -const { IotData } = require("../models/tanks"); +const { IotData, Tank } = require("../models/tanks"); const fastify = require("fastify")({ logger: true, //disableRequestLogging: true, @@ -635,4 +635,41 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { }; - \ No newline at end of file + +exports.getAllocatedSensorsByTank = async (req, reply) => { + try { + let { customerId, tankName } = req.params; + + if (!customerId || !tankName) { + return reply.status(400).send({ error: "customerId and tankName are required" }); + } + + tankName = tankName.trim(); // Trim spaces + + console.log("Querying MongoDB with:", { customerId, tankName, status: "blocked" }); + + const allocatedSensors = await Insensors.find({ + customerId, + tankName: { $regex: `^${tankName}$`, $options: "i" }, // Case-insensitive search + status: "blocked", + }).lean(); + + if (!allocatedSensors.length) { + return reply.send({ + status_code: 200, + message: "No allocated sensors found for this tank", + allocatedSensors: [], + }); + } + + return reply.send({ + status_code: 200, + message: "Allocated sensors fetched successfully", + allocatedSensors, + }); + + } catch (err) { + console.error("Error fetching allocated sensors:", err); + return reply.status(500).send({ error: "Internal server error" }); + } +}; diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 79452e29..f9aa6058 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -260,6 +260,24 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.getByHardwareAndTankId, }); + + fastify.get("/api/getAllocatedSensorsByTank/:customerId/:tankName", { + schema: { + description: "Get allocated sensors by installationId, customerId, and tankName", + tags: ["Installation"], + summary: "Fetch allocated sensors for a given tank", + params: { + type: "object", + properties: { + // installationId: { type: "string" }, + customerId: { type: "string" }, + tankName: { type: "string" }, + }, + required: [ "customerId", "tankName"], + }, + }, + handler: installationController.getAllocatedSensorsByTank, + }); next();