added create connection module

master
Sneha 3 years ago
parent c229c947af
commit bf223c5120

@ -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);
}
};

@ -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);
}
};
};
//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);
}
};

@ -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

@ -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);

@ -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 },

@ -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();
}

@ -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();
}

Loading…
Cancel
Save