diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index d659a361..47396428 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1,7 +1,7 @@ //Get the data models -const { Supplier, DeliveryBoy } = require('../models/supplier'); -const { FriendRequest } = require('../models/supplier') -const { ProfilePicture, User } = require('../models/User') +const { Supplier, DeliveryBoy } = require("../models/supplier"); +const { FriendRequest } = require("../models/supplier"); +const { ProfilePicture, User } = require("../models/User"); const supplierController = require("../controllers/supplierController"); const customJwtAuth = require("../customAuthJwt"); const fastify = require("fastify")({ @@ -19,7 +19,6 @@ const boom = require("boom"); const emailValidator = require("email-validator"); const libphonenumberjs = require("libphonenumber-js"); - fastify.register(customJwtAuth); const schema = { @@ -43,18 +42,16 @@ const options = { // data: data // optional, default: process.env }; fastify.register(fastifyEnv, options).ready((err) => { - if (err) console.error(err); - - console.log(fastify.config.PORT); // or fastify[options.confKey] - // output: { PORT: 3000 } - fastify.decorate("conf", { - port: fastify.config.PORT, - APIVERSION: fastify.config.APIVERSION, - }); + if (err) console.error(err); + + console.log(fastify.config.PORT); // or fastify[options.confKey] + // output: { PORT: 3000 } + fastify.decorate("conf", { + port: fastify.config.PORT, + APIVERSION: fastify.config.APIVERSION, }); - - - +}); + const apiversion = "1.0.0"; // fastify.register(require('fastify-cookie')) @@ -93,7 +90,10 @@ isSupplierFormUrlEncoded = (req) => { }, }; - return { isSupplierFormUrlEncoded: isSupplierFormUrlEncoded, supplier: s_data }; + return { + isSupplierFormUrlEncoded: isSupplierFormUrlEncoded, + supplier: s_data, + }; } else { return { isSupplierFormUrlEncoded: false, s_data: "" }; } @@ -107,138 +107,132 @@ fastify.register((fastify, opts, done) => { { parseAs: "buffer" }, function (_req, body, done) { try { - done(null, body) + done(null, body); } catch (error) { - error.statusCode = 400 - done(error, undefined) + error.statusCode = 400; + done(error, undefined); } } - ) - - done(null) -}) - - + ); + done(null); +}); //Login Supplier Handler -exports.loginSupplier = async(request, reply) =>{ - loginObject = await supplierController.loginSupplier(request); - console.log("loginObject...",loginObject) - if (loginObject.same) { - const phoneVerified = loginObject.supplier.phoneVerified; - const oneTimePasswordSetFlag = loginObject.supplier.oneTimePasswordSetFlag; - console.log( - "oneTimePasswordSetFlag is ......", - oneTimePasswordSetFlag, - typeof oneTimePasswordSetFlag, - typeof phoneVerified +exports.loginSupplier = async (request, reply) => { + loginObject = await supplierController.loginSupplier(request); + console.log("loginObject...", loginObject); + if (loginObject.same) { + const phoneVerified = loginObject.supplier.phoneVerified; + const oneTimePasswordSetFlag = loginObject.supplier.oneTimePasswordSetFlag; + console.log( + "oneTimePasswordSetFlag is ......", + oneTimePasswordSetFlag, + typeof oneTimePasswordSetFlag, + typeof phoneVerified + ); + if (!phoneVerified) { + reply.send({ + simplydata: { + error: false, + phoneVerified: false, + + phone: loginObject.supplier.phone, + oneTimePasswordSetFlag: oneTimePasswordSetFlag, + message: "Please Verify your phone number", + }, + }); + } else if (oneTimePasswordSetFlag) { + reply.send({ + simplydata: { + error: false, + phoneVerified: phoneVerified, + phone: loginObject.supplier.phone, + oneTimePasswordSetFlag: true, + message: "Password must be reset", + }, + }); + } else { + const token = fastify.jwt.sign( + { + suppliername: loginObject.supplier.suppliername, + supplierId: loginObject.supplier._id, + roles: loginObject.supplier.profile.role, + }, + //expiresIn: expressed in seconds or a string describing a time span zeit/ms. Eg: 60, "2 days", "10h", "7d". + //A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), + //otherwise milliseconds unit is used by default ("120" is equal to "120ms"). + { expiresIn: "30d" } ); - if (!phoneVerified) { - reply.send({ - simplydata: { - error: false, - phoneVerified: false, - - phone: loginObject.supplier.phone, - oneTimePasswordSetFlag: oneTimePasswordSetFlag, - message: "Please Verify your phone number", - }, - }); - } else if (oneTimePasswordSetFlag) { - reply.send({ - simplydata: { - error: false, - phoneVerified: phoneVerified, - phone: loginObject.supplier.phone, - oneTimePasswordSetFlag: true, - message: "Password must be reset", - }, - }); - } else { - const token = fastify.jwt.sign( - { - suppliername: loginObject.supplier.suppliername, - supplierId: loginObject.supplier._id, - roles: loginObject.supplier.profile.role, - }, - //expiresIn: expressed in seconds or a string describing a time span zeit/ms. Eg: 60, "2 days", "10h", "7d". - //A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), - //otherwise milliseconds unit is used by default ("120" is equal to "120ms"). - { expiresIn: "30d" } - ); - console.log(token, "..token") + console.log(token, "..token"); - var arr = loginObject.supplier.profile.role; - var arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string - var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object - var s_id = loginObject.supplier.supplierId + var arr = loginObject.supplier.profile.role; + var arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string + var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object + var s_id = loginObject.supplier.supplierId; - console.log(s_id,"supplierId") - var profilePicture = await ProfilePicture.findOne({ supplierId:s_id}); + console.log(s_id, "supplierId"); + var profilePicture = await ProfilePicture.findOne({ supplierId: s_id }); - // request.session.set('supplierId', loginObject.supplier._id) + // request.session.set('supplierId', loginObject.supplier._id) - if (!profilePicture) { - reply.send({ - simplydata: { + if (!profilePicture) { + reply.send({ + simplydata: { error: false, apiversion: fastify.config.APIVERSION, access_token: token, - + email: loginObject.supplier.emails, phone: loginObject.supplier.phone, supplierId: loginObject.supplier.supplierId, suppliername: loginObject.supplier.suppliername, - office_address: loginObject.supplier.profile.office_address, + office_address: loginObject.supplier.profile.office_address, phoneVerified: loginObject.supplier.phoneVerified, oneTimePasswordSetFlag: loginObject.supplier.oneTimePasswordSetFlag, latitude: loginObject.supplier.latitude, longitude: loginObject.supplier.longitude, type: loginObject.supplier.profile.role, typeasobj: stringToJsonObject, - }, - }); - }if (profilePicture) { - reply.send({ - simplydata: { - error: false, + }, + }); + } + if (profilePicture) { + reply.send({ + simplydata: { + error: false, apiversion: fastify.config.APIVERSION, access_token: token, - picture:profilePicture.picture, + picture: profilePicture.picture, email: loginObject.supplier.emails, phone: loginObject.supplier.phone, supplierId: loginObject.supplier.supplierId, suppliername: loginObject.supplier.suppliername, - office_address: loginObject.supplier.profile.office_address, + office_address: loginObject.supplier.profile.office_address, phoneVerified: loginObject.supplier.phoneVerified, oneTimePasswordSetFlag: loginObject.supplier.oneTimePasswordSetFlag, latitude: loginObject.supplier.latitude, longitude: loginObject.supplier.longitude, type: loginObject.supplier.profile.role, typeasobj: stringToJsonObject, - }, - }); - } - + }, + }); } - } else { - error = { - simplydata: { - error: true, - code: 400, - message: "Invalid SupplierId , Password supplied", - }, - }; - reply.send(error); } -} - - + } else { + error = { + simplydata: { + error: true, + code: 400, + message: "Invalid SupplierId , Password supplied", + }, + }; + reply.send(error); + } +}; //Login Delivery Handler -exports.loginDeliveryBoy = async(request, reply) =>{ - +exports.loginDeliveryBoy = async (request, reply) => { phone = request.body.phone; phoneVerificationCode = request.body.phoneVerificationCode; @@ -266,7 +260,7 @@ exports.loginDeliveryBoy = async(request, reply) =>{ updatedDeliveryBoy = await DeliveryBoy.findOne({ phone: phone }); } loginObject = await supplierController.loginDeliveryBoy(request); - console.log("loginObject...",loginObject) + console.log("loginObject...", loginObject); if (loginObject.same) { const phoneVerified = loginObject.delivery.phoneVerified; const oneTimePasswordSetFlag = loginObject.delivery.oneTimePasswordSetFlag; @@ -281,7 +275,7 @@ exports.loginDeliveryBoy = async(request, reply) =>{ simplydata: { error: false, phoneVerified: false, - + phone: loginObject.delivery.phone, oneTimePasswordSetFlag: oneTimePasswordSetFlag, message: "Please Verify your phone number", @@ -308,61 +302,57 @@ exports.loginDeliveryBoy = async(request, reply) =>{ //otherwise milliseconds unit is used by default ("120" is equal to "120ms"). { expiresIn: "30d" } ); - console.log(token, "..token") + console.log(token, "..token"); + var d_id = loginObject.delivery._id; - var d_id = loginObject.delivery._id - - - console.log(d_id,"deliveryId") - var profilePicture = await ProfilePicture.findOne({ deliveryBoyId:d_id}); - + console.log(d_id, "deliveryId"); + var profilePicture = await ProfilePicture.findOne({ + deliveryBoyId: d_id, + }); // request.session.set('supplierId', loginObject.supplier._id) - if (!profilePicture) { reply.send({ simplydata: { - error: false, - apiversion: fastify.config.APIVERSION, - access_token: token, - - - phone: loginObject.delivery.phone, - deliveryBoyId: loginObject.delivery.deliveryBoyId, - deliveryBoyname: loginObject.delivery.name, - address: loginObject.delivery.address, - phoneVerified: loginObject.delivery.phoneVerified, - oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, - supplierId: loginObject.delivery.supplierId, - suppliername: loginObject.delivery.suppliername, - longitude:loginObject.delivery. longitude, - latitude:loginObject.delivery.latitude, + error: false, + apiversion: fastify.config.APIVERSION, + access_token: token, + + phone: loginObject.delivery.phone, + deliveryBoyId: loginObject.delivery.deliveryBoyId, + deliveryBoyname: loginObject.delivery.name, + address: loginObject.delivery.address, + phoneVerified: loginObject.delivery.phoneVerified, + oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, + supplierId: loginObject.delivery.supplierId, + suppliername: loginObject.delivery.suppliername, + longitude: loginObject.delivery.longitude, + latitude: loginObject.delivery.latitude, }, }); - }if (profilePicture) { + } + if (profilePicture) { reply.send({ simplydata: { error: false, - apiversion: fastify.config.APIVERSION, - access_token: token, - picture:profilePicture.picture, - phone: loginObject.delivery.phone, - deliveryBoyId: loginObject.delivery.deliveryBoyId, - deliveryBoyname: loginObject.delivery.name, - address: loginObject.delivery.address, - phoneVerified: loginObject.delivery.phoneVerified, - oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, - supplierId: loginObject.delivery.supplierId, - suppliername: loginObject.delivery.suppliername, - longitude:loginObject.delivery. longitude, - latitude:loginObject.delivery.latitude, + apiversion: fastify.config.APIVERSION, + access_token: token, + picture: profilePicture.picture, + phone: loginObject.delivery.phone, + deliveryBoyId: loginObject.delivery.deliveryBoyId, + deliveryBoyname: loginObject.delivery.name, + address: loginObject.delivery.address, + phoneVerified: loginObject.delivery.phoneVerified, + oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, + supplierId: loginObject.delivery.supplierId, + suppliername: loginObject.delivery.suppliername, + longitude: loginObject.delivery.longitude, + latitude: loginObject.delivery.latitude, }, }); } - - } } else { error = { @@ -374,8 +364,162 @@ exports.loginDeliveryBoy = async(request, reply) =>{ }; reply.send(error); } -} +}; +exports.deliveryBoyVerifyPhone = 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 + ); + deliveryBoyExists = await DeliveryBoy.findOne({ + phone: phone, + //phoneVerified: false, + phoneVerificationCode: phoneVerificationCode, + }); + console.log(deliveryBoyExists); + if (deliveryBoyExists) { + // update the phoneVerified flag to true. + const filter = { + phone: phone, + phoneVerificationCode: phoneVerificationCode, + }; + const update = { phoneVerified: true }; + const doc = await DeliveryBoy.findOneAndUpdate(filter, update); + updatedDeliveryBoy = await DeliveryBoy.findOne({ phone: phone }); + + if (updatedDeliveryBoy.phoneVerified) { + loginObject = await supplierController.loginDeliveryBoy(req); + console.log("loginObject...", loginObject); + if (loginObject.same) { + const phoneVerified = loginObject.delivery.phoneVerified; + const oneTimePasswordSetFlag = + loginObject.delivery.oneTimePasswordSetFlag; + console.log( + "oneTimePasswordSetFlag is ......", + oneTimePasswordSetFlag, + typeof oneTimePasswordSetFlag, + typeof phoneVerified + ); + if (!phoneVerified) { + reply.send({ + simplydata: { + error: false, + phoneVerified: false, + + phone: loginObject.delivery.phone, + oneTimePasswordSetFlag: oneTimePasswordSetFlag, + message: "Please Verify your phone number", + }, + }); + } else if (oneTimePasswordSetFlag) { + reply.send({ + simplydata: { + error: false, + phoneVerified: phoneVerified, + phone: loginObject.delivery.phone, + oneTimePasswordSetFlag: true, + message: "Password must be reset", + }, + }); + } else { + const token = fastify.jwt.sign( + { + deliveryBoyname: loginObject.delivery.name, + deliveryBoyId: loginObject.delivery._id, + }, + //expiresIn: expressed in seconds or a string describing a time span zeit/ms. Eg: 60, "2 days", "10h", "7d". + //A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), + //otherwise milliseconds unit is used by default ("120" is equal to "120ms"). + { expiresIn: "30d" } + ); + console.log(token, "..token"); + + var d_id = loginObject.delivery._id; + + console.log(d_id, "deliveryId"); + var profilePicture = await ProfilePicture.findOne({ + deliveryBoyId: d_id, + }); + + // request.session.set('supplierId', loginObject.supplier._id) + + if (!profilePicture) { + reply.send({ + simplydata: { + error: false, + apiversion: fastify.config.APIVERSION, + access_token: token, + + phone: loginObject.delivery.phone, + deliveryBoyId: loginObject.delivery.deliveryBoyId, + deliveryBoyname: loginObject.delivery.name, + address: loginObject.delivery.address, + phoneVerified: loginObject.delivery.phoneVerified, + oneTimePasswordSetFlag: + loginObject.delivery.oneTimePasswordSetFlag, + supplierId: loginObject.delivery.supplierId, + suppliername: loginObject.delivery.suppliername, + longitude: loginObject.delivery.longitude, + latitude: loginObject.delivery.latitude, + }, + }); + } + if (profilePicture) { + reply.send({ + simplydata: { + error: false, + apiversion: fastify.config.APIVERSION, + access_token: token, + picture: profilePicture.picture, + phone: loginObject.delivery.phone, + deliveryBoyId: loginObject.delivery.deliveryBoyId, + deliveryBoyname: loginObject.delivery.name, + address: loginObject.delivery.address, + phoneVerified: loginObject.delivery.phoneVerified, + oneTimePasswordSetFlag: + loginObject.delivery.oneTimePasswordSetFlag, + supplierId: loginObject.delivery.supplierId, + suppliername: loginObject.delivery.suppliername, + longitude: loginObject.delivery.longitude, + latitude: loginObject.delivery.latitude, + }, + }); + } + } + } else { + error = { + simplydata: { + error: true, + code: 400, + message: "Invalid Details", + }, + }; + 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); + } +}; // Check if all the required fields are supplied by the user @@ -385,7 +529,7 @@ exports.fieldCheck = async (req, reply) => { suppliername: req.body.suppliername, emails: req.body.emails, password: req.body.password, - services: { password: {bcrypt: req.body.password} }, + services: { password: { bcrypt: req.body.password } }, phone: req.body.phone, profile: { firstName: req.body.firstName, @@ -402,7 +546,7 @@ exports.fieldCheck = async (req, reply) => { var supplier = new Supplier(s_Data); - console.log(supplier,"..supplier") + console.log(supplier, "..supplier"); //password is not at the top level in the collection. password = req.body.password; @@ -416,7 +560,13 @@ exports.fieldCheck = async (req, reply) => { supplier.emails = suppliertobeInserted.emails; password = suppliertobeInserted.password; } - console.log("User to be inserted is ", supplier.suppliername,password,supplier.phone,supplier.profile); + console.log( + "User to be inserted is ", + supplier.suppliername, + password, + supplier.phone, + supplier.profile + ); // check if all rerquired fields are passed. if ( !( @@ -439,7 +589,7 @@ exports.fieldCheck = async (req, reply) => { ); // Required Fields are missing suppliedvalues = - supplier.suppliername + + supplier.suppliername + " ," + password + " ," + @@ -501,7 +651,7 @@ exports.validatePhoneFormat = async (req, reply) => { }, }; req.body.regError = error; - reply.status(406).send(error); + reply.status(406).send(error); } } } @@ -578,7 +728,9 @@ exports.validateEmailFormat = async (req, reply) => { error: true, code: 10003, message: - "10003 - Email " + supplier.emails[0].email + " is not a valid email", + "10003 - Email " + + supplier.emails[0].email + + " is not a valid email", }, }; req.body.regError = error; @@ -590,68 +742,53 @@ exports.validateEmailFormat = async (req, reply) => { } }; - exports.logoutsupplier = async (request, reply) => { - - - // request.session.delete(); - - // // send response to clear token - // reply.send({ message: 'Successfully logged out' }) + // request.session.delete(); - const invalidatedTokens = {}; - const accessToken = request.headers.authorization && request.body.access_token; - invalidatedTokens[accessToken] = true; + // // send response to clear token + // reply.send({ message: 'Successfully logged out' }) -// // localStorage.removeItem(invalidatedTokens[accessToken]) + const invalidatedTokens = {}; + const accessToken = + request.headers.authorization && request.body.access_token; + invalidatedTokens[accessToken] = true; - reply.send({ message: 'Logout successful' }) - - } - - - 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({ + // // localStorage.removeItem(invalidatedTokens[accessToken]) + + reply.send({ message: "Logout successful" }); +}; + +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, - 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); - } + }; + 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: { @@ -663,94 +800,104 @@ exports.logoutsupplier = async (request, reply) => { req.body.regError = error; reply.send(error); } - } catch (err) { - throw boom.boomify(err); - } - }; - - - exports.deliveryBoyVerifyPhone = 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 - ); - deliveryBoyExists = await DeliveryBoy.findOne({ - phone: phone, - phoneVerified: false, - phoneVerificationCode: phoneVerificationCode, - }); - console.log(deliveryBoyExists); - if (deliveryBoyExists) { - // update the phoneVerified flag to true. - const filter = { - phone: phone, - phoneVerificationCode: phoneVerificationCode, - }; - const update = { phoneVerified: true }; - const doc = await DeliveryBoy.findOneAndUpdate(filter, update); - updatedDeliveryBoy = await DeliveryBoy.findOne({ phone: phone }); - - if (updatedDeliveryBoy.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); - } - } - } catch (err) { - throw boom.boomify(err); + } 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); + } +}; +// exports.deliveryBoyVerifyPhone = 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 +// ); +// deliveryBoyExists = await DeliveryBoy.findOne({ +// phone: phone, +// phoneVerified: false, +// phoneVerificationCode: phoneVerificationCode, +// }); +// console.log(deliveryBoyExists); +// if (deliveryBoyExists) { +// // update the phoneVerified flag to true. +// const filter = { +// phone: phone, +// phoneVerificationCode: phoneVerificationCode, +// }; +// const update = { phoneVerified: true }; +// const doc = await DeliveryBoy.findOneAndUpdate(filter, update); +// updatedDeliveryBoy = await DeliveryBoy.findOne({ phone: phone }); + +// if (updatedDeliveryBoy.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); +// } +// } +// } catch (err) { +// throw boom.boomify(err); +// } +// }; - //delete selected deliveryboy +//delete selected deliveryboy exports.deleteDeliveryBoy = async (req, reply) => { try { var supplierId = req.params.supplierId; var phone = req.query.phone; - const delivery = await DeliveryBoy.findOneAndDelete({ phone: phone,supplierId:supplierId }); + const delivery = await DeliveryBoy.findOneAndDelete({ + phone: phone, + supplierId: supplierId, + }); - reply.send({ status_code: 200, data: delivery}); - // return tank; + reply.send({ status_code: 200, data: delivery }); + // return tank; } catch (err) { throw boom.boomify(err); } }; - //update selected Delivery Boy Details exports.updateDeliveryBoy = async (req, reply) => { - try { var supplierId = req.params.supplierId; var phone = req.query.phone; const delivery = req.body; const { ...updateData } = delivery; - const update = await DeliveryBoy.findOneAndUpdate({ phone: phone,supplierId:supplierId, }, updateData, { new: true }); - console.log(update) + const update = await DeliveryBoy.findOneAndUpdate( + { phone: phone, supplierId: supplierId }, + updateData, + { new: true } + ); + console.log(update); //return update; reply.send({ status_code: 200, data: update }); - - - - } - catch (err) { + } catch (err) { throw boom.boomify(err); } }; @@ -794,7 +941,9 @@ exports.getSuppliers = async (req, reply) => { const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object try { const friendRequests = await FriendRequest.find({ customerId }); - const supplierIdsToExclude = friendRequests.map((request) => request.supplierId); + const supplierIdsToExclude = friendRequests.map( + (request) => request.supplierId + ); await Supplier.find({ supplierId: { $nin: supplierIdsToExclude } }) .limit(limit) .skip(startindex) @@ -811,7 +960,6 @@ exports.getSuppliers = async (req, reply) => { } }; - // Get single user by ID exports.getSingleSupplier = async (req, reply) => { try { @@ -829,9 +977,14 @@ exports.getConnectedSuppliers = async (req, reply) => { const startindex = (page - 1) * limit; const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object try { - const friendRequests = await FriendRequest.find({ customerId, status: "accepted" }); - // console.log(friendRequests,customerId) - const supplierIdsToInclude = friendRequests.map((request) => request.supplierId); + const friendRequests = await FriendRequest.find({ + customerId, + status: "accepted", + }); + // console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map( + (request) => request.supplierId + ); await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) @@ -848,19 +1001,22 @@ exports.getConnectedSuppliers = async (req, reply) => { } }; - - exports.getPendingSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object - + try { - const friendRequests = await FriendRequest.find({ customerId, status: ["pending"] }); - // console.log(friendRequests,customerId) - const supplierIdsToInclude = friendRequests.map((request) => request.supplierId); - console.log(supplierIdsToInclude, "SUPLIERINCLUDE") + const friendRequests = await FriendRequest.find({ + customerId, + status: ["pending"], + }); + // console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map( + (request) => request.supplierId + ); + console.log(supplierIdsToInclude, "SUPLIERINCLUDE"); const timestamps = friendRequests.map((request) => request.timestamp); console.log(timestamps, "timestamps"); await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) @@ -868,7 +1024,16 @@ exports.getPendingSuppliers = async (req, reply) => { .skip(startindex) .exec() .then((docs) => { - reply.send({ status_code: 200, data: docs, count: docs.length ,timestamps}); + // const supplierDataWithTimestamp = docs.map((doc, index) => ({ + // data: doc, + // //timestamp: timestamps[index], + // })); + // reply.send({ + // status_code: 200, + // data: supplierDataWithTimestamp, + // count: docs.length, + // }); + reply.send({ status_code: 200, data: docs, count: docs.length }); }) .catch((err) => { console.log(err); @@ -879,18 +1044,22 @@ exports.getPendingSuppliers = async (req, reply) => { } }; - exports.getRejectSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object - + try { - const friendRequests = await FriendRequest.find({ customerId, status: ["rejected"] }); - // console.log(friendRequests,customerId) - const supplierIdsToInclude = friendRequests.map((request) => request.supplierId); - // console.log(supplierIdsToInclude) + const friendRequests = await FriendRequest.find({ + customerId, + status: ["rejected"], + }); + // console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map( + (request) => request.supplierId + ); + // console.log(supplierIdsToInclude) await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) @@ -907,28 +1076,39 @@ exports.getRejectSuppliers = async (req, reply) => { } }; - - - exports.getPendingCustomers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object - + try { - const friendRequests = await FriendRequest.find({ supplierId, status: ["pending"] }); - console.log(friendRequests,supplierId, "su....") - const supplierIdsToInclude = friendRequests.map((request) => request.customerId); - console.log(supplierIdsToInclude, "supplierIdsToInclude..") - const timestamps = friendRequests.map((request) => request.timestamp); + const friendRequests = await FriendRequest.find({ + supplierId, + status: ["pending"], + }); + console.log(friendRequests, supplierId, "su...."); + const supplierIdsToInclude = friendRequests.map( + (request) => request.customerId + ); + console.log(supplierIdsToInclude, "supplierIdsToInclude.."); + const timestamps = friendRequests.map((request) => request.timestamp); console.log(timestamps, "timestamps"); await User.find({ customerId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) .exec() .then((docs) => { - reply.send({ status_code: 200, data: docs, count: docs.length ,timestamps}); + // const customerDataWithTimestamp = docs.map((doc, index) => ({ + // data: doc, + // //timestamp: timestamps[index], + // })); + reply.send({ status_code: 200, data: docs, count: docs.length }); + // reply.send({ + // status_code: 200, + // data: customerDataWithTimestamp, + // count: docs.length, + // }); }) .catch((err) => { console.log(err); @@ -939,18 +1119,22 @@ exports.getPendingCustomers = async (req, reply) => { } }; - exports.getRejectCustomers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object - + try { - const friendRequests = await FriendRequest.find({ supplierId, status: ["rejected"] }); - console.log(friendRequests,supplierId, "su....") - const supplierIdsToInclude = friendRequests.map((request) => request.customerId); - console.log(supplierIdsToInclude, "supplierIdsToInclude..") + const friendRequests = await FriendRequest.find({ + supplierId, + status: ["rejected"], + }); + console.log(friendRequests, supplierId, "su...."); + const supplierIdsToInclude = friendRequests.map( + (request) => request.customerId + ); + console.log(supplierIdsToInclude, "supplierIdsToInclude.."); await User.find({ customerId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) @@ -967,18 +1151,22 @@ exports.getRejectCustomers = async (req, reply) => { } }; - exports.getconnectedCustomers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object - + try { - const friendRequests = await FriendRequest.find({ supplierId, status: ["accepted"] }); - console.log(friendRequests,supplierId, "su....") - const supplierIdsToInclude = friendRequests.map((request) => request.customerId); - console.log(supplierIdsToInclude, "supplierIdsToInclude..") + const friendRequests = await FriendRequest.find({ + supplierId, + status: ["accepted"], + }); + console.log(friendRequests, supplierId, "su...."); + const supplierIdsToInclude = friendRequests.map( + (request) => request.customerId + ); + console.log(supplierIdsToInclude, "supplierIdsToInclude.."); await User.find({ customerId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) @@ -993,4 +1181,4 @@ exports.getconnectedCustomers = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 6f818d93..0aaae6bf 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -215,7 +215,7 @@ module.exports = function (fastify, opts, next) { }, }, }, - handler: validationHandler.loginDeliveryBoy, + handler: validationHandler.deliveryBoyVerifyPhone, }); fastify.route({