| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -510,77 +510,72 @@ const fastify = require("fastify")({
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					exports.createConnections = async (req, body) => {
 | 
					 | 
					 | 
					 | 
					exports.createConnections = async (req, body) => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  try {
 | 
					 | 
					 | 
					 | 
					  try {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const customerId = req.params.customerId;
 | 
					 | 
					 | 
					 | 
					    const customerId = req.params.customerId;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const tankname = req.body.tankname;
 | 
					 | 
					 | 
					 | 
					    const tankname  = req.body.tankname;
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const tankInfo = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname });
 | 
					 | 
					 | 
					 | 
					    const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname })
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const usertobeInserted = req.body;
 | 
					 | 
					 | 
					 | 
					    const usertobeInserted = req.body;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    tankInfo.connections.source = tankInfo.tankName;
 | 
					 | 
					 | 
					 | 
					    tankInfo.connections.source = tankInfo.tankName;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (usertobeInserted.inputConnections) {
 | 
					 | 
					 | 
					 | 
					    if (usertobeInserted.inputConnections) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      // update inputismotor field for each input connection
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => {
 | 
					 | 
					 | 
					 | 
					      tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return {
 | 
					 | 
					 | 
					 | 
					          return {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          inputConnections: connection.inputConnections,
 | 
					 | 
					 | 
					 | 
					              inputConnections: connection.inputConnections,
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          input_type: connection.input_type,
 | 
					 | 
					 | 
					 | 
					              input_type: connection.input_type,
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          motor_id: connection.motor_id || null,
 | 
					 | 
					 | 
					 | 
					              motor_status: connection.motor_status || "0",
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          motor_status: connection.motor_status || "0",
 | 
					 | 
					 | 
					 | 
					              inputismotor: connection.inputismotor // default to false if not specified
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          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) {
 | 
					 | 
					 | 
					 | 
					  if (usertobeInserted.outputConnections) {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      // update outputismotor field for each output connection
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => {
 | 
					 | 
					 | 
					 | 
					      tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return {
 | 
					 | 
					 | 
					 | 
					          return {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          outputConnections: connection.outputConnections,
 | 
					 | 
					 | 
					 | 
					              outputConnections: connection.outputConnections,
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        output_type: connection.output_type,
 | 
					 | 
					 | 
					 | 
					              output_type: connection.output_type,
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        motor_id: connection.motor_id || null,
 | 
					 | 
					 | 
					 | 
					              outputismotor: connection.outputismotor // default to false if not specified
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        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;
 | 
					 | 
					 | 
					 | 
					    const connection_data = req.body.outputConnections
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    console.log(connection_data, "connection_data");
 | 
					 | 
					 | 
					 | 
					    for (const data of connection_data){
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    for (const data of connection_data) {
 | 
					 | 
					 | 
					 | 
					      if(data['output_type'] === "overhead"){
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      if (data['output_type'] === "overhead") {
 | 
					 | 
					 | 
					 | 
					        const tankName = data['outputConnections']
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const tankName = data['outputConnections'];
 | 
					 | 
					 | 
					 | 
					        const tank_info = await Tank.findOne({ customerId: customerId.toString(),tankName:tankName })
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        const connection_data_check =tank_info.connections.inputConnections
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        const sump_names=[]
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        for(const d of connection_data_check){
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					          if (sump_names.includes(d.inputConnections)) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					            console.log(`${d.inputConnections} exists in ${sump_names}`);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					          } else {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					            sump_names.push(d.inputConnections)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					          }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          
 | 
					 | 
					 | 
					 | 
					          
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        console.log(sump_names)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        //tank_info.connections.inputConnections = tankname.toString()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        if (sump_names.includes(tankname)) {
 | 
					 | 
					 | 
					 | 
					        if (sump_names.includes(tankname)) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          console.log(`${tankname} exists in ${sump_names}`);
 | 
					 | 
					 | 
					 | 
					          console.log(`${tankname} exists in ${sump_names}`);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        } else {
 | 
					 | 
					 | 
					 | 
					        } else {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          const tankConnections = await Tank.findOneAndUpdate(
 | 
					 | 
					 | 
					 | 
					          const tankConnections = await Tank.findOneAndUpdate(
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            { customerId: customerId.toString(), tankName: tankName },
 | 
					 | 
					 | 
					 | 
					            { customerId: customerId.toString(), tankName: tankName },
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            {
 | 
					 | 
					 | 
					 | 
					            { $addToSet: { 'connections.inputConnections': { $each: [{ inputConnections: tankname, input_type: 'sump' }] } } },      
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					              $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);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        }
 | 
					 | 
					 | 
					 | 
					        }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        //console.log(tankConnections)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      }
 | 
					 | 
					 | 
					 | 
					      }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    const tank_connections = await tankInfo.save();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return tank_connections;
 | 
					 | 
					 | 
					 | 
					    return tank_connections;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  } catch (err) {
 | 
					 | 
					 | 
					 | 
					  } catch (err) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    throw boom.boomify(err);
 | 
					 | 
					 | 
					 | 
					    throw boom.boomify(err);
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					 | 
					
 
 |