From bf223c5120b9244d8f48cdcc406c31a42f55cf7e Mon Sep 17 00:00:00 2001 From: Sneha Date: Fri, 20 Jan 2023 02:04:09 -0500 Subject: [PATCH] added create connection module --- src/controllers/createConnectionController.js | 37 +++++++ src/controllers/tanksController.js | 80 ++++++++++----- src/index.js | 7 ++ src/models/CreateConnections.js | 20 ++++ src/models/tanks.js | 2 +- src/routes/createConnectionsRoute.js | 56 +++++++++++ src/routes/tanksRoute.js | 98 ++++++++++++++++++- 7 files changed, 269 insertions(+), 31 deletions(-) create mode 100644 src/controllers/createConnectionController.js create mode 100644 src/models/CreateConnections.js create mode 100644 src/routes/createConnectionsRoute.js diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js new file mode 100644 index 00000000..2404233b --- /dev/null +++ b/src/controllers/createConnectionController.js @@ -0,0 +1,37 @@ +const Connections = require("../models/CreateConnections"); +const User = require("../models/User"); +const boom = require("boom"); +const fastify = require("fastify")({ + logger: true, +}); + + +// add new tanks +exports.createConnections = async (req, reply) => { + try { + + //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; + + } catch (err) { + throw boom.boomify(err); + } +}; diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f01235c2..eaabbe0a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -5,15 +5,14 @@ const fastify = require("fastify")({ logger: true, }); - - exports.addTanks = async (req, reply) => { try { //const username = req.params.username; console.log(req.params); - // const { username } = req.params; + const username = loginObject.user.username; + //console.log(loginObject.user.username) // const userInfo = await User.findOne({ username: username.toString() }); // const updateData = req.body; @@ -21,13 +20,19 @@ console.log(req.params); // console.log("This is the reply in the handler after the validations", reply); tankData = { - //userName: username, + userName: username, tankName: req.body.tankName, blockName: req.body.blockName, capacity: req.body.capacity, typeOfWater: req.body.typeOfWater, tankLocation:req.body.tankLocation, }; + var tank_name = req.body.tankName + var i_tank = await Tank.findOne({ tankName: tank_name}) + if(i_tank){ + throw new Error('tankname already exists'); + } + else { var tank = new Tank(tankData); @@ -41,11 +46,11 @@ console.log(req.params); tank.typeOfWater = usertobeInserted.typeOfWater; tank.tankLocation = usertobeInserted.tankLocation; } - + } const insertedTank = await tank.save(); return insertedTank; - + } catch (err) { throw boom.boomify(err); @@ -53,26 +58,51 @@ console.log(req.params); }; +//update selected tank +exports.updateTanksInfo = async (req, reply) => { + try { + //const username = loginObject.user.username; + const tankName = req.params.tankName; + const tank = req.body; + const { ...updateData } = tank; + const update = await Tank.findOneAndUpdate({ tankName: tankName }, updateData, { new: true }); + //return update; -exports.editTanksInfo = async (req, body) => { + reply.send({ status_code: 200, data: update }); - try { - const { tankId } = req.params; - const tankInfo = await Tank.findById(tankId); - const updateData = req.body; - console.log(updateData.tankName); - if (updateData.tankName) tankInfo.tankName = updateData.tankName; - if (updateData.blockName) tankInfo.blockName = updateData.blockName; - if (updateData.capacity) tankInfo.capacity = updateData.capacity; - if (updateData.typeOfWater) tankInfo.typeOfWater = updateData.typeOfWater; - if (updateData.tankLocation) tankInfo.tankLocation = updateData.tankLocation; - - const tanks = await tankInfo.save(); - return tanks; - } - catch (err) { - throw boom.boomify(err); - } + } + catch (err) { + throw boom.boomify(err); + } +}; - }; \ No newline at end of file +//delete selected tank +exports.deleteTanksInfo = async (req, reply) => { + try { + const tankName = req.params.tankName; + const tank = await Tank.findOneAndDelete({ tankName: tankName }); + + reply.send({ status_code: 200, data: tank}); + // return tank; + } catch (err) { + throw boom.boomify(err); + } +}; + +//get tanks data by passing username +exports.getTank = async (req, reply) => { + try { + await Tank.find({userName: req.query.userName}) + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 61692213..a493db80 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ const userController = require("./controllers/userController"); const tanksController = require("./controllers/tanksController"); +const createConnectionController = require("./controllers/createConnectionController"); const cors = require("cors"); const swagger = require("./config/swagger"); const rawBody = require('raw-body') @@ -173,6 +174,7 @@ fastify.post("/api/login", { simplydata: { error: false, phoneVerified: false, + phone: loginObject.user.phone, oneTimePasswordSetFlag: oneTimePasswordSetFlag, message: "Please Verify your phone number", @@ -216,7 +218,11 @@ fastify.post("/api/login", { error: false, apiversion: fastify.config.APIVERSION, access_token: token, + email: loginObject.user.emails, + phone: loginObject.user.phone, username: loginObject.user.username, + address1: loginObject.user.profile.address1, + address2: loginObject.user.profile.address2, phoneVerified: loginObject.user.phoneVerified, oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag, type: loginObject.user.profile.role, @@ -281,6 +287,7 @@ const { Schema } = require("mongoose"); fastify.register(require("./routes/usersRoute")); fastify.register(require("./routes/tanksRoute")); +fastify.register(require("./routes/createConnectionsRoute")); // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Also allows deletion of a user with a given phone number diff --git a/src/models/CreateConnections.js b/src/models/CreateConnections.js new file mode 100644 index 00000000..06d88482 --- /dev/null +++ b/src/models/CreateConnections.js @@ -0,0 +1,20 @@ +const mongoose = require("mongoose"); + + + +const Schema = mongoose.Schema; +const ObjectId = Schema.Types.ObjectId; + +// Store a random password reset code +const code = Math.floor(100000 + Math.random() * 900000); +const inputSchema = new Schema({ name: String }); +const createConnectionsSchema = new mongoose.Schema({ + + // userName: { type: String, default: null }, + source: { type: String,unique:false,trim: true ,required: true}, + inputConnections: [{ inputConnections: String}], + outputConnections: [{ outputConnections: String}] + +}); + +module.exports = mongoose.model("Connections", createConnectionsSchema); \ No newline at end of file diff --git a/src/models/tanks.js b/src/models/tanks.js index ba09d4dd..0680feb1 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -13,7 +13,7 @@ const tanksSchema = new mongoose.Schema({ userName: { type: String, default: null }, tankName: { type: String, default: null }, blockName: { type: String, default: null }, - capacity: { type: Number, default: null }, + capacity: { type: String, default: null }, typeOfWater: { type: String, default: null }, tankLocation: { type: String, default: null }, diff --git a/src/routes/createConnectionsRoute.js b/src/routes/createConnectionsRoute.js new file mode 100644 index 00000000..8becc119 --- /dev/null +++ b/src/routes/createConnectionsRoute.js @@ -0,0 +1,56 @@ +const fastify = require("fastify"); +const createConnectionController = require("../controllers/createConnectionController"); + +module.exports = function (fastify, opts, next) { + + fastify.route({ + method: "POST", + url: "/api/createConnections", + schema: { + tags: ["Connections"], + description: "This is for cretae New Connection", + summary: "This is for cretae New Connection.", + body: { + type: "object", + 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.authenticate]), + handler: createConnectionController.createConnections, + // onResponse: (request, reply) => { + // validationHandler.sendPhoneVerificationCode(request, reply); + // }, + //onResponse: validationHandler.sendPhoneVerificationCode, + }); + + + next(); +} diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 9d6d2230..92ff6409 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -13,11 +13,11 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { - tankName: { type: "string", default: null }, - blockName: { type: "string", default: null }, - capacity: { type: "number" }, - typeOfWater: { type: "string", default: null }, - tankLocation: { type: "string", default: null }, + tankName: { type: "string" }, + blockName: { type: "string"}, + capacity: { type: "string" }, + typeOfWater: { type: "string" }, + tankLocation: { type: "string" }, }, }, security: [ @@ -35,6 +35,94 @@ module.exports = function (fastify, opts, next) { //onResponse: validationHandler.sendPhoneVerificationCode, }); + fastify.route({ + method: "PUT", + url: "/api/updateTanks/:tankName", + schema: { + tags: ["Tank"], + summary: "This is for update tank", + params: { + required: ["tankName"], + type: "object", + properties: { + tankName: { + type: "string", + description: "tankName", + }, + }, + }, + body: { + type: "object", + // required: ['phone'], + properties: { + tankName: { type: "string", default: null }, + blockName: { type: "string", default: null }, + capacity: { type: "number" }, + typeOfWater: { type: "string", default: null }, + tankLocation: { type: "string", default: null }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.updateTanksInfo, + }); + + + fastify.route({ + method: "PUT", + url: "/api/deleteTank/:tankName", + schema: { + tags: ["Tank"], + summary: "This is for delete tank", + params: { + required: ["tankName"], + type: "object", + properties: { + tankName: { + type: "string", + description: "tankName", + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.deleteTanksInfo, + }); + + + fastify.get("/api/getTanks", { + schema: { + tags: ["Tank"], + description: "This is for Get Tank Data", + summary: "This is for to Get Tank Data", + querystring: { + userName: {type: 'string'} + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getTank, + }); + + next(); }