diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5d50580d..3d5201ae 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -211,13 +211,17 @@ exports.getTank = async (req, reply) => { await Tank.find({ customerId: req.query.customerId }) .exec() .then((docs) => { - // Transform the response to include switch_count + // Transform the response to include switch_count and calculate totalSwitchCount + let totalSwitchCount = 0; + const transformedDocs = docs.map((tank) => { const inputConnections = tank.connections?.inputConnections || []; const switchCount = inputConnections.reduce((count, connection) => { return count + (connection.inputismotor === true ? 1 : 0); }, 0); + totalSwitchCount += switchCount; // Accumulate the switch_count + // Add the switch_count field inside connections return { ...tank.toObject(), // Convert Mongoose document to plain object @@ -228,7 +232,12 @@ exports.getTank = async (req, reply) => { }; }); - reply.send({ status_code: 200, data: transformedDocs, count: transformedDocs.length }); + reply.send({ + status_code: 200, + data: transformedDocs, + count: transformedDocs.length, + total_switch_count: totalSwitchCount, // Add the total switch count + }); }) .catch((err) => { console.error(err);