From 1378b3bcb163cf74a7757c8d8d7c18fe2e473f49 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 30 Jan 2023 07:40:19 -0500 Subject: [PATCH] generating booking id and storing booking details in database by creating new table --- src/config/swagger.js | 4 +- src/controllers/tankersController.js | 144 +++++++++++++++++++++++++ src/models/tankers.js | 14 ++- src/routes/tankersRoute.js | 153 +++++++++++++++++++++++++++ 4 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 src/controllers/tankersController.js create mode 100644 src/routes/tankersRoute.js diff --git a/src/config/swagger.js b/src/config/swagger.js index 7308e53c..5ca1ddf9 100644 --- a/src/config/swagger.js +++ b/src/config/swagger.js @@ -14,8 +14,8 @@ 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: "35.207.198.4:3000", //Production for swagger + host: "localhost:3000", //Devlopemnt host on lcoal + //host: "35.207.198.4:3000", //Production for swagger schemes: ["http"], consumes: ["application/json"], produces: ["application/json"], diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js new file mode 100644 index 00000000..d303833d --- /dev/null +++ b/src/controllers/tankersController.js @@ -0,0 +1,144 @@ + +const Tanker = require("../models/tankers"); +const Tankerbooking = require("../models/tankers"); + +const User = require("../models/User"); +const boom = require("boom"); +const fastify = require("fastify")({ + logger: true, +}); + + + +exports.addTankers = async (req, reply) => { + try { + + //const username = req.params.username; + +console.log(req.params); + const username = loginObject.user.username; + //console.log(loginObject.user.username) + // const userInfo = await User.findOne({ username: username.toString() }); + // const updateData = req.body; + + + + // console.log("This is the reply in the handler after the validations", reply); + tankersData = { + tankerName: req.body.tankerName, + phoneNumber: req.body.phoneNumber, + typeofwater: req.body.typeofwater, + capacity: req.body.capacity, + }; + var tanker_Name = req.body.tankerName + var i_tank = await Tanker.findOne({ tankerName: tanker_Name}) + if(i_tank){ + throw new Error('tankname already exists'); + } + else { + + var tankers = new Tanker(tankersData); + + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + usertobeInserted = checkFormEncoding.tank; + console.log("thsi true url string"); + tankers.tankerName = usertobeInserted.tankerName; + tankers.phoneNumber = usertobeInserted.phoneNumber; + tankers.capacity = usertobeInserted.capacity; + tankers.typeofwater = usertobeInserted.typeofwater; + } + } + const insertedTank = await tankers.save(); + + return insertedTank; + + + } catch (err) { + throw boom.boomify(err); + } +}; + +//update selected tanker +exports.updateTankersInfo = async (req, reply) => { + try { + //const username = loginObject.user.username; + const tankerName = req.params.tankerName; + + var tank_name = req.body.tankerName + var i_tank = await Tanker.findOne({ tankerName: tank_name}) + const tanker = req.body; + const { ...updateData } = tanker; + const update = await Tanker.findOneAndUpdate({ tankerName: tankerName }, updateData, { new: true }); + + if(i_tank){ + throw new Error('tankname already exists'); + } + else + { + //return update; + reply.send({ status_code: 200, data: update }); + } + } + catch (err) { + throw boom.boomify(err); + } +}; + +//delete selected tank +exports.deleteTankerInfo = async (req, reply) => { + try { + const tankerName = req.params.tankerName; + const tanker = await Tanker.findOneAndDelete({ tankerName: tankerName }); + reply.send({ status_code: 200, data: tanker}); + // return tanker; + } catch (err) { + throw boom.boomify(err); + } +}; + + +exports.tankerBooking = async (req, reply) => { + try { + const date = new Date() + const year = date.getFullYear() + const month = (date.getMonth() + 1).toString().padStart(2, '0') + const day = date.getDate().toString().padStart(2, '0') + const count = bookingCount.toString().padStart(2, '0') + const bookingId = `booking${year}${month}${day}${count}` + //bookingCount = (bookingCount + 1) % 100; + + + bookingsData = { + + typeofwater: req.body.typeofwater, + capacity: req.body.capacity, + address: req.body.address, + dateTime: req.body.dateTime, + + }; + + var tankersBookingData = new Tankerbooking(bookingsData); + + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + usertobeInserted = checkFormEncoding.tankersBookingData; + console.log("thsi true url string"); + tankersBookingData.bookingid = bookingId; + tankersBookingData.capacity = usertobeInserted.capacity; + tankersBookingData.typeofwater = usertobeInserted.typeofwater; + tankersBookingData.address = usertobeInserted.address; + tankersBookingData.dateTime = usertobeInserted.dateTime; + + + } + + const booking_data = await tankersBookingData.save(); + + return booking_data; + + + } catch (err) { + throw boom.boomify(err); + } +}; \ No newline at end of file diff --git a/src/models/tankers.js b/src/models/tankers.js index 1b38e253..b06aa25d 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -8,6 +8,8 @@ const ObjectId = Schema.Types.ObjectId; // Store a random password reset code const code = Math.floor(100000 + Math.random() * 900000); const RoleSchema = new Schema({ name: String }); + + const tankersSchema = new mongoose.Schema({ tankerName: { type: String, default: null }, @@ -16,4 +18,14 @@ const tankersSchema = new mongoose.Schema({ capacity: { type: String, default: null }, }); -module.exports = mongoose.model("Tanker", tankersSchema); \ No newline at end of file +const tankersbookingSchema = new mongoose.Schema({ + + bookingid: { type: String, default: null }, + typeofwater: { type: String, default: null }, + capacity: { type: String, default: null }, + address: { type: String, default: null }, + dateTime: { type: String, default: null }, +}); + +module.exports = mongoose.model("Tanker", tankersSchema); +module.exports = mongoose.model("Tankerbooking", tankersbookingSchema); \ No newline at end of file diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js new file mode 100644 index 00000000..e7789e91 --- /dev/null +++ b/src/routes/tankersRoute.js @@ -0,0 +1,153 @@ +const fastify = require("fastify"); +const tankersController = require("../controllers/tankersController.js"); + + + + +module.exports = function (fastify, opts, next) { + + fastify.route({ + method: "POST", + url: "/api/addTankers", + schema: { + tags: ["Tanker"], + description: "This is for cretae New Tanker", + summary: "This is for Create New Tanker.", + body: { + type: "object", + properties: { + tankerName: { type: "string" }, + phoneNumber: { type: "string"}, + typeofwater: { type: "string" }, + capacity: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.addTankers, + // onResponse: (request, reply) => { + // validationHandler.sendPhoneVerificationCode(request, reply); + // }, + //onResponse: validationHandler.sendPhoneVerificationCode, + }); + + +//update tankers + fastify.route({ + method: "PUT", + url: "/api/updateTankers/:tankerName", + schema: { + tags: ["Tanker"], + summary: "This is for update tanker", + params: { + required: ["tankerName"], + type: "object", + properties: { + tankerName: { + type: "string", + description: "tankerName", + }, + }, + }, + body: { + type: "object", + // required: ['phone'], + properties: { + tankerName: { type: "string", default: null }, + phoneNumber: { type: "string", default: null }, + capacity: { type: "number" }, + typeofwater: { type: "string", default: null }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.updateTankersInfo, + }); + + + fastify.route({ + method: "PUT", + url: "/api/deleteTanker/:tankerName", + schema: { + tags: ["Tanker"], + summary: "This is for delete tanker", + params: { + required: ["tankerName"], + type: "object", + properties: { + tankerName: { + type: "string", + description: "tankerName", + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.deleteTankerInfo, + }); + + + + + fastify.route({ + method: "POST", + url: "/api/bookingData", + schema: { + tags: ["Tanker"], + description: "This is for store booking data of a Tanker", + summary: "This is for store booking data of a Tanker", + body: { + type: "object", + properties: { + + typeofwater: { type: "string" }, + capacity: { type: "string" }, + address: { type: "string" }, + dateTime: { type: "string"}, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.tankerBooking, + // onResponse: (request, reply) => { + // validationHandler.sendPhoneVerificationCode(request, reply); + // }, + //onResponse: validationHandler.sendPhoneVerificationCode, + }); + + + + + + + next(); +} + +