From a12804ef33e097a56fd4f9554c78f4405550ff7c Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 21 Mar 2023 08:01:24 -0400 Subject: [PATCH] made changes in tanker.js,tankercontrollers,tanker routes --- src/controllers/tankersController.js | 65 ++++++++++++++++++++++++ src/routes/tankersRoute.js | 76 +++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 1 deletion(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index ba347767..757bbc8f 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -75,6 +75,71 @@ console.log(req.params); } }; + + + +exports.addTankers1 = async (req, reply) => { + try { + + //const username = req.params.username; + +console.log(req.params); + const supplierId = req.params.supplierId; + //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 = { + supplierId:supplierId, + + supplier_name:req.body.supplier_name, + supplier_address : req.body.supplier_address, + tankerName: req.body.tankerName, + phoneNumber: req.body.phoneNumber, + alternative_phoneNumber: req.body.alternative_phoneNumber, + typeofwater: req.body.typeofwater, + capacity: req.body.capacity, + price: req.body.price, + status: req.body.status + }; + console.log(req.body.typeofwater,req.body.capacity) + + var tanker_Name = req.body.tankerName + var i_tank = await Tanker.findOne({ tankerName: tanker_Name,supplierId:supplierId}) + if(i_tank){ + throw new Error('tankername already exists'); + } + else { + + var tankers = new Tanker(tankersData); + + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + usertobeInserted = checkFormEncoding.tankers; + console.log("thsi true url string"); + tankers.supplier_name = usertobeInserted.supplier_name; + tankers.supplier_address = usertobeInserted.supplier_address; + tankers.tankerName = usertobeInserted.tankerName; + tankers.phoneNumber = usertobeInserted.phoneNumber; + tankers.alternative_phoneNumber = usertobeInserted.alternative_phoneNumber; + tankers.capacity = usertobeInserted.capacity; + tankers.typeofwater = usertobeInserted.typeofwater; + tankers.price = usertobeInserted.price; + tankers.status = usertobeInserted.status; + } + } + const insertedTanker = await tankers.save(); + + return insertedTanker; + + + } catch (err) { + throw boom.boomify(err); + } +}; + //update selected tanker exports.updateTankersInfo = async (req, reply) => { diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index f886a0a4..6afcfa2d 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -41,7 +41,7 @@ module.exports = function (fastify, opts, next) { }, }, }, - + price: { type: "array", maxItems: 2500, @@ -569,6 +569,80 @@ module.exports = function (fastify, opts, next) { preHandler: fastify.auth([fastify.authenticate]), handler: tankersController.status, }); + + + + +module.exports = function (fastify, opts, next) { + + fastify.route({ + method: "POST", + url: "/api/addTankers5/:supplierId", + schema: { + tags: ["Supplier"], + description: "This is to cretae New Tanker", + summary: "This is to Create New Tanker.", + params: { + required: ["supplierId"], + type: "object", + properties: { + supplierId: { + type: "string", + description: "supplierId", + }, + }, + }, + body: { + type: "object", + properties: { + supplier_name: { type: "string" }, + supplier_address:{ type: "string" }, + tankerName: { type: "string" }, + phoneNumber: { type: "string"}, + alternative_phoneNumber: { type: "string"}, + typeofwater: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + typeofwater: { type: "string", default: null }, + }, + }, + }, + + price: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + price: { type: "string", default: null }, + + }, + }, + }, + capacity: { type: "string"}, + + status: {type: "string"} + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.addTankers1, + // onResponse: (request, reply) => { + // validationHandler.sendPhoneVerificationCode(request, reply); + // }, + //onResponse: validationHandler.sendPhoneVerificationCode, + }); + next(); }