diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c8c6dbb4..4d7bd447 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -229,55 +229,54 @@ 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 }, - }, - }, - ]); + // Find all tanks for the given customerId + const tanks = await Tank.find({ customerId }); - const inputIsMotorCounts = await Tank.aggregate([ - { $match: { customerId } }, - { $unwind: "$connections.inputConnections" }, - { - $group: { - _id: "$connections.inputConnections.inputismotor", - count: { $sum: 1 }, + if (!tanks || tanks.length === 0) { + return reply.send({ + message: "No tanks found for the given customerId", + data: [], + }); + } + + // Process each tank + const tankResults = tanks.map(tank => { + const needSensorCount = { + YES: tank.need_sensor === "yes" ? 1 : 0, + + }; + + const inputConnections = tank.connections.inputConnections || []; + const inputIsMotorCounts = inputConnections.reduce( + (acc, connection) => { + if (connection.inputismotor === true) acc.true += 1; + + return acc; }, - }, - ]); + { true: 0 } + ); - // Format the results - const needSensorResult = needSensorCounts.reduce((acc, item) => { - acc[item._id] = item.count; - return acc; - }, {}); + const totalInputConnections = inputConnections.length; - const inputIsMotorResult = inputIsMotorCounts.reduce((acc, item) => { - acc[item._id ? "true" : "false"] = item.count; - return acc; - }, {}); + return { + tankName: tank.tankName, + needSensorCount, + inputIsMotorCounts, + totalInputConnections, + inputConnections, + }; + }); - // Return the response reply.send({ customerId, - needSensor: { - YES: needSensorResult["YES"] || 0, - NO: needSensorResult["NO"] || 0, - }, - inputIsMotor: { - true: inputIsMotorResult["true"] || 0, - false: inputIsMotorResult["false"] || 0, - }, + tanks: tankResults, }); } catch (error) { reply.status(500).send({ error: "An error occurred while fetching the data." }); } -}; +}, + + exports.getTanksofParticularInstaller = async (req, reply) => {