From 4844be2e7ccec7976c1c22e9af8c629d7d1c6661 Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Tue, 14 Mar 2023 16:36:25 +0530 Subject: [PATCH] supplier phone verifyApi --- src/handlers/supplierHandler.js | 62 +++++++++++++++++++++++++++++++-- src/routes/supplierRoute.js | 27 ++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index ad749678..4229b4a1 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -144,7 +144,7 @@ exports.loginSupplier = async(request, reply) =>{ message: "Please Verify your phone number", }, }); - } else if (!oneTimePasswordSetFlag) { + } else if (oneTimePasswordSetFlag) { reply.send({ simplydata: { error: false, @@ -349,7 +349,7 @@ exports.validatePhoneFormat = async (req, reply) => { code: 10002, message: "10002 - Phone # " + - user.phone + + supplier.phone + " is not a valid phone number", }, }; @@ -463,3 +463,61 @@ exports.logoutsupplier = async (request, reply) => { } + exports.verifyPhone = async (req, reply) => { + console.log("-------------------------------------------------"); + try { + phone = req.body.phone; + phoneVerificationCode = req.body.phoneVerificationCode; + + // check if user exists in the system. If user exists , display message that + // username is not available + console.log( + "this is the phone and verification code", + phone, + phoneVerificationCode + ); + supplierExists = await Supplier.findOne({ + phone: phone, + phoneVerified: false, + phoneVerificationCode: phoneVerificationCode, + }); + console.log(supplierExists); + if (supplierExists) { + // update the phoneVerified flag to true. + const filter = { + phone: phone, + phoneVerificationCode: phoneVerificationCode, + }; + const update = { phoneVerified: true }; + const doc = await Supplier.findOneAndUpdate(filter, update); + updatedSupplier = await Supplier.findOne({ phone: phone }); + + if (updatedSupplier.phoneVerified) { + reply.send('{"armintatankdata":{"error":false,"verified": true}}'); + } else { + error = { + armintatankdata: { + error: true, + code: 10005, + message: "10005 - Verification code entered cannot be validated.", + }, + }; + req.body.regError = error; + reply.send(error); + } + } else { + error = { + armintatankdata: { + error: true, + code: 10005, + message: "10005 - Verification code entered cannot be validated.", + }, + }; + req.body.regError = error; + reply.send(error); + } + } catch (err) { + throw boom.boomify(err); + } + }; + diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 44c1de05..ed0f3181 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -96,6 +96,33 @@ module.exports = function (fastify, opts, next) { handler: supplierController.addSupplier, }); + + fastify.route({ + method: "POST", + url: "/api/supplierphone", + schema: { + tags: ["Supplier-Data"], + description: "This is for verify Supplier Phone", + summary: "This is to Verify Supplier Phone.", + body: { + type: "object", + required: ["phone"], + properties: { + phoneVerificationCode: { type: "string" }, + phone: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: validationHandler.verifyPhone, + }); + + next(); }