diff --git a/src/config/swagger.js b/src/config/swagger.js index 072e90fa..7308e53c 100644 --- a/src/config/swagger.js +++ b/src/config/swagger.js @@ -15,7 +15,7 @@ exports.options = { // We have to change this beacause this is running on local // we have to run on this on swagger // host: "localhost:3000", //Devlopemnt host on lcoal - host: "localhost:3000", //Production for swagger + host: "35.207.198.4:3000", //Production for swagger schemes: ["http"], consumes: ["application/json"], produces: ["application/json"], diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index 2404233b..59b55167 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -1,4 +1,6 @@ const Connections = require("../models/CreateConnections"); +const Tank = require("../models/tanks"); + const User = require("../models/User"); const boom = require("boom"); const fastify = require("fastify")({ @@ -7,31 +9,41 @@ const fastify = require("fastify")({ // add new tanks -exports.createConnections = async (req, reply) => { + +exports.createConnections = async (req, body) => { try { + var tankname = req.body.source; + + const tankInfo = await Tank.findOne({ tankName: tankname.toString() }) + const updateData = req.body; + console.log(updateData.inputConnections); + if (updateData.source) tankInfo.connections.source = updateData.source; + 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); + } +}; - //const username = loginObject.user.username; - createConnections = { - //userName: username, - source: req.body.source, - inputConnections: req.body.inputConnections, - outputConnections:req.body.outputConnections, - }; - - var connections = new Connections(createConnections); - - checkFormEncoding = isUserFormUrlEncoded(req); - if (checkFormEncoding.isUserFormUrlEncoded) { - usertobeInserted = checkFormEncoding.connections; - connections.source = usertobeInserted.source; - connections.inputConnections = req.body.inputConnections; - connections.outputConnections = req.body.outputConnections; - } - const insertedConnection = await connections.save(); - return insertedConnection; +exports.updateconnectionInfo = async (req, reply) => { + try { + //const username = loginObject.user.username; + const tankName = req.params.source; + const tankInfo = await Tank.findOne({ tankName: tankName.toString() }) + const updateData = req.body; + + if (updateData.source) tankInfo.connections.source = updateData.source; + 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); } }; + + diff --git a/src/models/tanks.js b/src/models/tanks.js index b8acac55..b16f598c 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -17,7 +17,11 @@ const tanksSchema = new mongoose.Schema({ typeOfWater: { type: String, default: null }, type: { type: String, default: null }, tankLocation: { type: String, default: null }, - + connections: { + source: { type: String}, + inputConnections: [{ inputConnections: String}], + outputConnections: [{ outputConnections: String}] + } }); diff --git a/src/routes/createConnectionsRoute.js b/src/routes/createConnectionsRoute.js index 8becc119..09308a51 100644 --- a/src/routes/createConnectionsRoute.js +++ b/src/routes/createConnectionsRoute.js @@ -52,5 +52,67 @@ module.exports = function (fastify, opts, next) { }); + + fastify.route({ + method: "PUT", + url: "/api/updateConnections/:source", + schema: { + tags: ["Connections"], + summary: "This is for update connections", + params: { + required: ["source"], + type: "object", + properties: { + source: { + type: "string", + description: "source", + }, + }, + }, + body: { + type: "object", + // required: ['phone'], + properties: { + source: { type: "string" }, + inputConnections: { + type: "array", + maxItems: 2, + items: { + type: "object", + properties: { + inputConnections: { type: "string", default: null }, + }, + }, + }, + outputConnections: { + type: "array", + maxItems: 2, + items: { + type: "object", + properties: { + outputConnections: { type: "string", default: null }, + }, + }, + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: createConnectionController.updateconnectionInfo, + }); + + + + + next(); }