|
|
@ -452,23 +452,73 @@ const fastify = require("fastify")({
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.createConnections = async (req, reply) => {
|
|
|
|
// exports.createConnections = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const customerId = req.params.customerId;
|
|
|
|
|
|
|
|
// const tankname = req.body.tankname;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Retrieve the tank or return an error if it's not found
|
|
|
|
|
|
|
|
// const tankInfo = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname });
|
|
|
|
|
|
|
|
// if (!tankInfo) {
|
|
|
|
|
|
|
|
// return reply.send({ status_code: 404, error: "Tank not found" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Update the source tank info
|
|
|
|
|
|
|
|
// tankInfo.connections.source = tankname;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Handle input connections
|
|
|
|
|
|
|
|
// if (req.body.inputConnections) {
|
|
|
|
|
|
|
|
// tankInfo.connections.inputConnections = req.body.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
|
|
|
|
|
|
|
|
// }));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Handle output connections
|
|
|
|
|
|
|
|
// if (req.body.outputConnections) {
|
|
|
|
|
|
|
|
// tankInfo.connections.outputConnections = req.body.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 connections
|
|
|
|
|
|
|
|
// const updatedTankConnections = await tankInfo.save();
|
|
|
|
|
|
|
|
// console.log("Updated Tank Connections:", updatedTankConnections.connections);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Send the updated tank connections as the response
|
|
|
|
|
|
|
|
// return reply.send({ status_code: 200, data: updatedTankConnections });
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// throw boom.boomify(err);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
// Retrieve the tank or return an error if it's not found
|
|
|
|
|
|
|
|
const tankInfo = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname });
|
|
|
|
const tankInfo = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname });
|
|
|
|
if (!tankInfo) {
|
|
|
|
const usertobeInserted = req.body;
|
|
|
|
return reply.send({ status_code: 404, error: "Tank not found" });
|
|
|
|
tankInfo.connections.source = tankInfo.tankName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update the source tank info
|
|
|
|
|
|
|
|
tankInfo.connections.source = tankname;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle input connections
|
|
|
|
if (usertobeInserted.inputConnections) {
|
|
|
|
if (req.body.inputConnections) {
|
|
|
|
tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => {
|
|
|
|
tankInfo.connections.inputConnections = req.body.inputConnections.map(connection => ({
|
|
|
|
return {
|
|
|
|
inputConnections: connection.inputConnections,
|
|
|
|
inputConnections: connection.inputConnections,
|
|
|
|
input_type: connection.input_type,
|
|
|
|
input_type: connection.input_type,
|
|
|
|
motor_id: connection.motor_id || null,
|
|
|
|
motor_id: connection.motor_id || null,
|
|
|
@ -477,12 +527,13 @@ exports.createConnections = async (req, reply) => {
|
|
|
|
inputismotor: connection.hasOwnProperty("inputismotor") ? connection.inputismotor : false,
|
|
|
|
inputismotor: connection.hasOwnProperty("inputismotor") ? connection.inputismotor : false,
|
|
|
|
capacity: connection.capacity || null,
|
|
|
|
capacity: connection.capacity || null,
|
|
|
|
water_level: connection.water_level || null
|
|
|
|
water_level: connection.water_level || null
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle output connections
|
|
|
|
if (usertobeInserted.outputConnections) {
|
|
|
|
if (req.body.outputConnections) {
|
|
|
|
tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => {
|
|
|
|
tankInfo.connections.outputConnections = req.body.outputConnections.map(connection => ({
|
|
|
|
return {
|
|
|
|
outputConnections: connection.outputConnections,
|
|
|
|
outputConnections: connection.outputConnections,
|
|
|
|
output_type: connection.output_type,
|
|
|
|
output_type: connection.output_type,
|
|
|
|
motor_id: connection.motor_id || null,
|
|
|
|
motor_id: connection.motor_id || null,
|
|
|
@ -491,15 +542,46 @@ exports.createConnections = async (req, reply) => {
|
|
|
|
outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false,
|
|
|
|
outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false,
|
|
|
|
capacity: connection.capacity || null,
|
|
|
|
capacity: connection.capacity || null,
|
|
|
|
water_level: connection.water_level || null
|
|
|
|
water_level: connection.water_level || null
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Save the updated tank connections
|
|
|
|
const tank_connections = await tankInfo.save();
|
|
|
|
const updatedTankConnections = await tankInfo.save();
|
|
|
|
|
|
|
|
console.log("Updated Tank Connections:", updatedTankConnections.connections);
|
|
|
|
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 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 }
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log("tankConnections", tankConnections.connections.inputConnections);
|
|
|
|
|
|
|
|
console.log("tankConnections", tankConnections.connections.outputConnections);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Send the updated tank connections as the response
|
|
|
|
return tank_connections;
|
|
|
|
return reply.send({ status_code: 200, data: updatedTankConnections });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|