From 1cdabfec07b079ed691c6ceac19e944a11b3893a Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 5 May 2025 13:08:36 +0530 Subject: [PATCH] changes in get tank --- src/controllers/tanksController.js | 125 +++++++++++++++++++---------- 1 file changed, 82 insertions(+), 43 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ac584ff2..9daca387 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -240,60 +240,95 @@ exports.getConnectionsInfoOfParticularTank = async (req, reply) => { //get tanks data by passing username exports.getTank = async (req, reply) => { try { - await Tank.find({ customerId: req.query.customerId }) - .exec() - .then((docs) => { - let totalSwitchCount = 0; - let totalSensorCount = 0; + const customerId = req.query.customerId; + if (!customerId) { + return reply.send({ status_code: 400, error: "Missing customerId" }); + } - const transformedDocs = docs.map((tank) => { - const inputConnections = tank.connections?.inputConnections || []; - - // Count switches - const switchCount = inputConnections.reduce((count, connection) => { - return count + (connection.inputismotor === true ? 1 : 0); - }, 0); - totalSwitchCount += switchCount; - - // Count sensors - if (tank.need_sensor?.toLowerCase() === "yes") { - totalSensorCount++; - } + // Use .lean() to get plain JavaScript objects, which are easier to modify + const tanks = await Tank.find({ customerId }).lean().exec(); - // Determine all_motor_status - const allMotorStatus = inputConnections.some(connection => connection.motor_status === "2"); + let totalSwitchCount = 0; + let totalSensorCount = 0; + let allSwitchConnectionsDetailed = []; // Array to hold the final consolidated list - // Add switch_count and all_motor_status to the response - return { - ...tank.toObject(), // Convert Mongoose document to plain object - connections: { - ...tank.connections, - switch_count: switchCount, - }, - all_motor_status: allMotorStatus, // Add all_motor_status field - }; - }); + const transformedDocs = tanks.map((tank) => { + const inputConnections = tank.connections?.inputConnections || []; + const currentToTank = tank.tankName || null; // Destination Tank Name + const currentToLocation = tank.tankLocation || null; // Destination Tank Location + + // Filter only motor switch connections (raw data) + const switchConnectionsRaw = inputConnections.filter(conn => conn.inputismotor === true); + + // Count switches for this tank and add to total + const switchCount = switchConnectionsRaw.length; + totalSwitchCount += switchCount; + + // Count sensor requirement + if (String(tank.need_sensor).toLowerCase() === "yes") { + totalSensorCount++; + } + + // --- Create the detailed switch connection objects for the consolidated list --- + const switchConnectionsForThisTankDetailed = switchConnectionsRaw.map(conn => ({ + // Spread all original properties from the switch connection + from_tank: conn.inputConnections || null, // Get source name from the connection itself + from_location: conn.input_type || null, // Get source type/location from the connection itself + to_tank: currentToTank, // Add destination tank name (parent object) + to_location: currentToLocation // Add destination location (parent object) + })); + // Add the detailed connections from *this* tank to the overall list + allSwitchConnectionsDetailed.push(...switchConnectionsForThisTankDetailed); + // --- End detailed switch connection creation --- + + + // Check if any motor is running (motor_status === "2") - keep as is + const isAnyMotorRunning = inputConnections.some(conn => conn.motor_status === "2"); + + // Map original from_connections for display within the tank object (optional, but your original code did this) + const fromConnections = inputConnections.map(conn => ({ + from_tank: conn.inputConnections || null, + from_type: conn.input_type || null, + })); + + // Return the transformed tank document for the 'data' array + return { + ...tank, // Include original tank fields + connections: { // Keep the connections object structure + ...tank.connections, // Include original connection details (source, input/output arrays) + switch_count: switchCount, // Keep the count specific to this tank + // Decide if you want to keep the raw switch_connections here or remove it + // Keeping it might be useful for context within the specific tank object + switch_connections: switchConnectionsRaw, // Keep the raw list here + from_connections: fromConnections, // Keep this derived list here + }, + all_motor_status: isAnyMotorRunning, // Keep this status flag + // You might not need these 'to_tank'/'to_location' fields at the root of the tank object anymore + // as they are now part of the consolidated switch_connections list, but keeping them doesn't hurt. + to_tank: currentToTank, + to_location: currentToLocation, + }; + }); + + // Send the final response with the consolidated list + reply.send({ + status_code: 200, + data: transformedDocs, // Array of processed tank objects + count: transformedDocs.length, + total_switch_count: totalSwitchCount, + total_sensor_count: totalSensorCount, + switch_connections: allSwitchConnectionsDetailed // The new consolidated list + }); - reply.send({ - status_code: 200, - data: transformedDocs, - count: transformedDocs.length, - total_switch_count: totalSwitchCount, - total_sensor_count: totalSensorCount, - }); - }) - .catch((err) => { - console.error(err); - reply.send({ status_code: 500, error: err.message }); - }); } catch (err) { - console.error(err); + console.error("getTank error:", err); reply.send({ status_code: 500, error: "Internal Server Error" }); } }; + exports.getTanksensorcount = async (req, reply) => { try { const { customerId } = req.params; @@ -6131,6 +6166,10 @@ async function processIotData(hw_Id, data) { // } // Process each tank + + + + for (const tank of tanks) { const { Id: tankhardwareId, level: tankHeight } = tank; const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });