diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index c02ff27b..6929b73f 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -535,45 +535,48 @@ exports.createConnections = async (req, body) => { 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 + 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']; - // Check if the current tank is already included in the inputConnections of any other tanks - const tanksWithCurrentTankAsInput = await Tank.find({ 'connections.inputConnections.inputConnections': tankname }); - - // If the current tank is not in the inputConnections of any other tank, add it - if (tanksWithCurrentTankAsInput.length === 0) { - // Loop through each tank to add the current tank as an inputConnection - for (const tank of tanksWithCurrentTankAsInput) { - await Tank.findOneAndUpdate( - { customerId: customerId.toString(), tankName: tank.tankName }, - { - $addToSet: { - 'connections.inputConnections': { - inputConnections: tankname, - input_type: 'sump', - inputismotor: data.outputismotor || false - } - } + if (sump_names.includes(tankname)) { + console.log(`${tankname} exists in ${sump_names}`); + } else { + const tankConnections = await Tank.findOneAndUpdate( + { customerId: customerId.toString(), tankName: tankName }, + { + $addToSet: { + 'connections.inputConnections': { + $each: [{ inputConnections: tankname, input_type: 'sump', inputismotor: data.outputismotor || false }], + }, + // 'connections.outputConnections': { + // $each: [{ outputConnections: tankname, output_type: 'overhead'}], + // }, }, - { new: true } - ); - } + }, + { new: true } + ); + console.log("tankConnections", tankConnections.connections.inputConnections); + console.log("tankConnections", tankConnections.connections.outputConnections); } } } @@ -587,7 +590,6 @@ exports.createConnections = async (req, body) => { - // exports.createConnections = async (req, body) => { // try { // const customerId = req.params.customerId;