diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index 231d5cc6..adbeb54b 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -506,94 +506,96 @@ const fastify = require("fastify")({ // }; + exports.createConnections = async (req, body) => { 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 { - const tank_info1 = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname }); - const tankConnections = await Tank.findOneAndUpdate( - { customerId: customerId.toString(), tankName: tankName }, - { - $addToSet: { - 'connections.inputConnections': { - $each: [{ - inputConnections: tankname, - input_type: 'sump', - inputismotor: true || false, - motor_id: motorId || null, - water_level: tank_info1.waterlevel || null, - capacity: tank_info1.capacity || null, - motor_status: data.motor_status || "1", - motor_stop_status: data.motor_stop_status || "1" - }], - }, - }, - }, - { new: true } - ); - console.log("tankConnections", tankConnections.connections.inputConnections); - console.log("tankConnections", tankConnections.connections.outputConnections); - } - } - } - - - + 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; } catch (err) { - throw boom.boomify(err); + throw boom.boomify(err); } -}; - + }; +