diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index a1a58c27..e291096d 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -507,94 +507,138 @@ const fastify = require("fastify")({ -exports.createConnections = async (req, body) => { +exports.createConnections = async (req, res) => { try { - const customerId = req.params.customerId; - const tankname = req.body.tankname; - - const tankInfo = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname }); - const usertobeInserted = req.body; - tankInfo.connections.source = tankInfo.tankName; - - if (usertobeInserted.inputConnections) { - tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => { - return { - inputConnections: connection.inputConnections, - input_type: connection.input_type, - motor_id: connection.motor_id || null, - motor_status: connection.motor_status || "0", - motor_stop_status: connection.motor_stop_status || "1", - inputismotor: connection.hasOwnProperty("inputismotor") ? connection.inputismotor : false, - capacity: connection.capacity || null, - water_level: connection.water_level || null - }; - }); - } - - if (usertobeInserted.outputConnections) { - tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => { - return { - outputConnections: connection.outputConnections, - output_type: connection.output_type, - motor_id: connection.motor_id || null, - motor_status: connection.motor_status || "0", - motor_stop_status: connection.motor_stop_status || "1", - outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false, - capacity: connection.capacity || null, - water_level: connection.water_level || null - }; - }); - } - - const tank_connections = await tankInfo.save(); - - const connection_data_check = tank_connections.connections.inputConnections; - const sump_names = connection_data_check.map(d => d.inputConnections); - console.log(sump_names); - - const connection_data = usertobeInserted.outputConnections; - console.log(connection_data, "connection_data"); - for (const data of connection_data) { - if (data['output_type'] === "overhead") { - const tankName = data['outputConnections']; - - if (sump_names.includes(tankname)) { - console.log(`${tankname} exists in ${sump_names}`); - } else { - console.log(tankInfo,"areyyy") - const tankConnections = await Tank.findOneAndUpdate( - { customerId: customerId.toString(), tankName: tankName }, - { - $addToSet: { - 'connections.inputConnections': { - $each: [{ - inputConnections: tankname, - input_type: 'sump', - inputismotor: data.outputismotor || false, - motor_id: data.motor_id || null, - water_level: tankInfo.waterlevel || null, - capacity: tankInfo.capacity || null, - motor_status: data.motor_status || "0", - motor_stop_status: data.motor_stop_status || "1" - }], }, - // 'connections.outputConnections': { - // $each: [{ outputConnections: tankname, output_type: 'overhead'}], - // }, - }, - }, - { new: true } - ); - console.log("tankConnections", tankConnections.connections.inputConnections); - console.log("tankConnections", tankConnections.connections.outputConnections); - } - } - } - - return tank_connections; + const customerId = req.params.customerId; + const tankname = req.body.tankname; + const usertobeInserted = req.body; + + // Find the current tank + const tankInfo = await Tank.findOne({ + customerId: customerId.toString(), + tankName: tankname, + }); + + if (!tankInfo) throw new Error(`Tank ${tankname} not found`); + + // Update source of connections + tankInfo.connections.source = tankInfo.tankName; + + // Map inputConnections if provided + if (usertobeInserted.inputConnections) { + tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map( + (connection) => ({ + inputConnections: connection.inputConnections, + input_type: connection.input_type, + motor_id: connection.motor_id || null, + motor_status: connection.motor_status || "0", + motor_stop_status: connection.motor_stop_status || "1", + inputismotor: connection.hasOwnProperty("inputismotor") + ? connection.inputismotor + : false, + capacity: connection.capacity || null, + water_level: connection.water_level || null, + }) + ); + } + + // Map outputConnections if provided + if (usertobeInserted.outputConnections) { + tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map( + (connection) => ({ + outputConnections: connection.outputConnections, + output_type: connection.output_type, + motor_id: connection.motor_id || null, + motor_status: connection.motor_status || "0", + motor_stop_status: connection.motor_stop_status || "1", + outputismotor: connection.hasOwnProperty("outputismotor") + ? connection.outputismotor + : false, + capacity: connection.capacity || null, + water_level: connection.water_level || null, + }) + ); + } + + // Save the updated tank + const tankConnections = await tankInfo.save(); + + // Handle input-to-output and output-to-input logic + const { inputConnections = [], outputConnections = [] } = usertobeInserted; + + // Process inputConnections + for (const input of inputConnections) { + const relatedTankName = input.inputConnections; + + // Ensure the related tank exists + const relatedTank = await Tank.findOne({ + customerId: customerId.toString(), + tankName: relatedTankName, + }); + + if (relatedTank) { + // Check if the current tank already exists in the related tank's outputConnections + const existsInOutput = relatedTank.connections.outputConnections.some( + (conn) => conn.outputConnections === tankname + ); + + if (!existsInOutput) { + relatedTank.connections.outputConnections.push({ + outputConnections: tankname, + output_type: "sump", + motor_id: input.motor_id || null, + motor_status: input.motor_status || "0", + motor_stop_status: input.motor_stop_status || "1", + outputismotor: input.inputismotor || false, + capacity: tankInfo.capacity || null, + water_level: tankInfo.waterlevel || null, + }); + + await relatedTank.save(); + } + } + } + + // Process outputConnections + for (const output of outputConnections) { + const relatedTankName = output.outputConnections; + + // Ensure the related tank exists + const relatedTank = await Tank.findOne({ + customerId: customerId.toString(), + tankName: relatedTankName, + }); + + if (relatedTank) { + // Check if the current tank already exists in the related tank's inputConnections + const existsInInput = relatedTank.connections.inputConnections.some( + (conn) => conn.inputConnections === tankname + ); + + if (!existsInInput) { + relatedTank.connections.inputConnections.push({ + inputConnections: tankname, + input_type: "overhead", + motor_id: output.motor_id || null, + motor_status: output.motor_status || "0", + motor_stop_status: output.motor_stop_status || "1", + inputismotor: output.outputismotor || false, + capacity: tankInfo.capacity || null, + water_level: tankInfo.waterlevel || null, + }); + + await relatedTank.save(); + } + } + } + + return res.send({ success: true, data: tankConnections }); } catch (err) { - throw boom.boomify(err); + console.error(err); + throw boom.boomify(err); } - }; +}; +