From 2dd606136a7207d670c4351682f53a88166fa782 Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 20 Dec 2024 17:27:40 +0530 Subject: [PATCH] api for sensor count and switch count --- src/controllers/tanksController.js | 60 +++++++++++++++++++++++++++++- src/routes/tanksRoute.js | 31 +++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d95a35c5..012b8422 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -105,7 +105,7 @@ exports.addTanks = async (req, reply) => { if (existingTank) { throw new Error('The combination of hardwareId and tankhardwareId already exists.'); } - + const tankData = { // InstallerId:InstallerId, customerId: customerId, @@ -222,6 +222,64 @@ exports.getTank = async (req, reply) => { } }; + + + +exports.getTanksensorcount = async (req, reply) => { + try { + const { customerId } = req.params; + + // Aggregate to count need_sensor values + const needSensorCounts = await Tank.aggregate([ + { $match: { customerId } }, + { + $group: { + _id: "$need_sensor", + count: { $sum: 1 }, + }, + }, + ]); + + const inputIsMotorCounts = await Tank.aggregate([ + { $match: { customerId } }, + { $unwind: "$connections.inputConnections" }, + { + $group: { + _id: "$connections.inputConnections.inputismotor", + count: { $sum: 1 }, + }, + }, + ]); + + // Format the results + const needSensorResult = needSensorCounts.reduce((acc, item) => { + acc[item._id] = item.count; + return acc; + }, {}); + + const inputIsMotorResult = inputIsMotorCounts.reduce((acc, item) => { + acc[item._id ? "true" : "false"] = item.count; + return acc; + }, {}); + + // Return the response + reply.send({ + customerId, + needSensor: { + yes: needSensorResult["yes"] || 0, + no: needSensorResult["no"] || 0, + }, + inputIsMotor: { + true: inputIsMotorResult["true"] || 0, + false: inputIsMotorResult["false"] || 0, + }, + }); + } catch (error) { + reply.status(500).send({ error: "An error occurred while fetching the data." }); + } +}; + + exports.getTanksofParticularInstaller = async (req, reply) => { try { await Tank.find({InstallerId: req.query.InstallerId}) diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index e1c5a324..83af36b2 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -207,6 +207,37 @@ module.exports = function (fastify, opts, next) { handler: tanksController.getTank, }); + + fastify.get("/api/getTanksensorcount/:customerId", { + schema: { + tags: ["Tank"], + description: "This is to Get Tank Sensor Count and Switch Count", + summary: "This is to Get Tank Tank Sensor Count and Switch Count", + + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getTanksensorcount, + }); + + + + fastify.get("/api/getTanksofParticularInstaller", { schema: { tags: ["Install"],