diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index eeb7c36f..b5774cde 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4,7 +4,8 @@ const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const { Deparments } = require("../models/Department"); const { Install, SensorStock, SensorQuotation, Order } = require("../models/store"); -const { Counter } = require("../models/User") +const { Counter } = require("../models/User"); +const { IotData } = require("../models/tanks"); const fastify = require("fastify")({ logger: true, //disableRequestLogging: true, @@ -530,7 +531,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { }); } }; - + exports.getDepartmentByFirstName = async (req, reply) => { try { let { firstName } = req.params; @@ -574,5 +575,64 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { }); } }; + + + exports.getByHardwareId = async (req, reply) => { + try { + const { hardwareId } = req.params; + + if (!hardwareId) { + return reply.status(400).send({ error: "hardwareId is required" }); + } + + console.log("Fetching details for hardwareId:", hardwareId); + + // Fetch only required fields to reduce response size + // const data = await Collection.findOne({ hardwareId }) + // .select("hardwareId mode tanks date time") // Select only needed fields + // .lean(); + + const data = await IotData.find({ hardwareId }); + + + if (!data) { + return reply.send({ status_code: 404, message: "Data not found", data: null }); + } + + return reply.send({ status_code: 200, message: "Success", data }); + } catch (err) { + console.error("Error:", err); + return reply.status(500).send({ error: "Internal Server Error" }); + } + }; + + + exports.getByHardwareAndTankId = async (req, reply) => { + try { + const { hardwareId, tankhardwareId } = req.params; + + if (!hardwareId || !tankhardwareId) { + return reply.status(400).send({ error: "Both hardwareId and tankhardwareId are required" }); + } + + console.log("Fetching details for:", { hardwareId, tankhardwareId }); + + // Correct query using $elemMatch + const data = await IotData.findOne( + { hardwareId, tanks: { $elemMatch: { tankhardwareId } } }, + { "tanks.$": 1, hardwareId: 1, mode: 1, date: 1, time: 1 } // Projection to return only matched tank + ); + + if (!data) { + return reply.send({ status_code: 404, message: "Data not found", data: null }); + } + + return reply.send({ status_code: 200, message: "Success", data }); + } catch (err) { + console.error("Error:", err); + return reply.status(500).send({ error: "Internal Server Error" }); + } + }; + \ No newline at end of file diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 41404a45..79452e29 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -228,6 +228,38 @@ module.exports = function (fastify, opts, next) { handler: installationController.getDepartmentByFirstName, }); + fastify.get("/api/getGsmCheck/:hardwareId", { + schema: { + description: "Get GSM check details", + tags: ["Installation"], + summary: "Get GSM check details", + params: { + type: "object", + properties: { + hardwareId: { type: "string" }, + }, + required: ["hardwareId"], + }, + }, + handler: installationController.getByHardwareId, + }); + + fastify.get("/api/getLoraCheck/:hardwareId/:tankhardwareId", { + schema: { + description: "Get LORA check details", + tags: ["Installation"], + summary: "Get LORA check details", + params: { + type: "object", + properties: { + hardwareId: { type: "string" }, + tankhardwareId: { type : "string"}, + }, + required: ["hardwareId","tankhardwareId"], + }, + }, + handler: installationController.getByHardwareAndTankId, + }); next();