master
varun 1 year ago
parent 2d6644502f
commit 3819941824

@ -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 {
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;
const usertobeInserted = req.body;
tankInfo.connections.source = tankInfo.tankName;
// Handle input connections
if (req.body.inputConnections) {
tankInfo.connections.inputConnections = req.body.inputConnections.map(connection => ({
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,
@ -477,12 +527,13 @@ exports.createConnections = async (req, reply) => {
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 => ({
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,
@ -491,15 +542,46 @@ exports.createConnections = async (req, reply) => {
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);
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 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 reply.send({ status_code: 200, data: updatedTankConnections });
return tank_connections;
} catch (err) {
throw boom.boomify(err);
}

@ -188,39 +188,8 @@ exports.getConnectionsInfoOfParticularTank = async (req, reply) => {
return reply.send({ status_code: 404, error: "Main tank not found" });
}
// Function to find tanks by name and location
const findTankDetails = async (connection, type) => {
return await Tank.findOne({
tankName: connection.inputConnections || connection.outputConnections,
tankLocation: connection.input_type || connection.output_type,
customerId: customerId
}).select('hardwareId tankName capacity height connections waterlevel');
};
// Get all related input and output tanks
const inputTankPromises = mainTank.connections.inputConnections.map(input => findTankDetails(input, 'input'));
const outputTankPromises = mainTank.connections.outputConnections.map(output => findTankDetails(output, 'output'));
// Resolve all promises
const inputTanks = await Promise.all(inputTankPromises);
const outputTanks = await Promise.all(outputTankPromises);
// Combine data from the main tank and its connections in a list with descriptive messages
const resultList = [{
type: 'Main Tank',
message: 'This is the data of the main tank.',
details: mainTank
}, {
type: 'Input Connections',
message: 'These are the data of all input tanks connected to the main tank.',
details: inputTanks.filter(tank => tank !== null) // Filter out null results
}, {
type: 'Output Connections',
message: 'These are the data of all output tanks connected to the main tank.',
details: outputTanks.filter(tank => tank !== null) // Filter out null results
}];
reply.send({ status_code: 200, data: resultList });
// Send the found tank
reply.send({ status_code: 200, data: mainTank });
} catch (err) {
throw boom.boomify(err);
}

Loading…
Cancel
Save