|
|
|
//const Connections = require("../models/CreateConnections");
|
|
|
|
const Tank = require("../models/tanks");
|
|
|
|
|
|
|
|
const User = require("../models/User");
|
|
|
|
const boom = require("boom");
|
|
|
|
const fastify = require("fastify")({
|
|
|
|
logger: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// add new tanks
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (usertobeInserted.outputConnections) tankInfo.connections.outputConnections = usertobeInserted.outputConnections;
|
|
|
|
|
|
|
|
const connection_data = req.body.outputConnections
|
|
|
|
for (const data of connection_data){
|
|
|
|
if(data['output_type'] === "overhead"){
|
|
|
|
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)) {
|
|
|
|
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' }] } } },
|
|
|
|
{ new: true }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//console.log(tankConnections)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tank_connections = await tankInfo.save();
|
|
|
|
|
|
|
|
|
|
|
|
return tank_connections;
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.updateconnectionInfo = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
//const username = loginObject.user.username;
|
|
|
|
const tankname = req.body.tankname
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname })
|
|
|
|
const updateData = req.body;
|
|
|
|
|
|
|
|
tankInfo.connections.source = tankInfo.tankName;
|
|
|
|
if (updateData.inputConnections) tankInfo.connections.inputConnections = updateData.inputConnections;
|
|
|
|
if (updateData.outputConnections) tankInfo.connections.outputConnections = updateData.outputConnections;
|
|
|
|
const tank_connections = await tankInfo.save();
|
|
|
|
return tank_connections;
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getConnections = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
var connectionsData=await Tank.find({customerId: req.query.customerId})
|
|
|
|
// var connection_data = connectionsData.connections
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: connectionsData});
|
|
|
|
// return tank;
|
|
|
|
|
|
|
|
|
|
|
|
// return tank;
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.deleteTankConnections = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const tankname = req.query.tankName;
|
|
|
|
|
|
|
|
const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname })
|
|
|
|
console.log(tankInfo)
|
|
|
|
|
|
|
|
tankInfo.connections.source = tankInfo.tankName;
|
|
|
|
tankInfo.connections.inputConnections = [];
|
|
|
|
tankInfo.connections.outputConnections = [];
|
|
|
|
const tank_connections = await tankInfo.save();
|
|
|
|
return tank_connections;
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.createConnectionsforOh = async (req, body) => {
|
|
|
|
// try {
|
|
|
|
// const customerId = req.params.customerId;
|
|
|
|
// var tankname = req.body.tankname;
|
|
|
|
|
|
|
|
// const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname ,tankLocation:"sump"})
|
|
|
|
// const connection_data = tankinfo.connections.outputConnections
|
|
|
|
// for (data in connection_data) {
|
|
|
|
// console.log(data)
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
// // const usertobeInserted = req.body;
|
|
|
|
// //tankInfo.connections.source = tankInfo.tankName;
|
|
|
|
// //if (usertobeInserted.inputConnections) tankInfo.connections.inputConnections = usertobeInserted.inputConnections;
|
|
|
|
// //if (usertobeInserted.outputConnections) tankInfo.connections.outputConnections = usertobeInserted.outputConnections;
|
|
|
|
// //const tank_connections = await tankInfo.save();
|
|
|
|
// // return tank_connections;
|
|
|
|
// } catch (err) {.
|
|
|
|
// throw boom.boomify(err);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|