const boom = require("boom"); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const fastify = require("fastify")({ logger: true, //disableRequestLogging: true, genReqId(req) { // you get access to the req here if you need it - must be a synchronous function return uuidv4(); }, }); const { Install, ProfilePictureInstall, generateinstallationId,Store} = require("../models/store"); exports.installSignUp = async (request, reply) => { try { const i_id = await generateinstallationId(); const installationId = `AWIN${i_id}`; const { // name, phone, address, address1, address2, emails, password, profile, team, manager, longitude, latitude, fcmId, alternativeNumber, firstName, lastName, city, createdBy, updatedBy, } = request.body; // Check if a user with the same phone number already exists const existingInstall = await Install.findOne({ phone }); if (existingInstall) { return reply.status(400).send({ message: 'Phone is already registered' }); } // Hash the password using bcrypt const hashedPassword = await bcrypt.hash(password, 10); // Create a new install object with the hashed password and other details const install = new Install({ // name, installationId, phone, address, address1, address2, emails, services: { password: { bcrypt: hashedPassword } }, profile, team, manager, longitude, latitude, fcmId, alternativeNumber, firstName, lastName, city, createdBy, updatedBy, }); // Save the new install to the database await install.save(); reply.send({ message: 'Install Account Created Successfully' }); } catch (err) { reply.status(500).send({ message: err.message }); } }; // exports.installLogin = async (request, reply) => { // try { // const { phone, password } = request.body; // // Check if an install with the phone number exists // const install = await Install.findOne({ phone }); // if (!install) { // return reply.status(401).send({ // simplydata: { // error: true, // message: 'Invalid Phone or password' // } // }); // } // // Compare the password entered by the user with the hashed password stored in the database // const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt); // if (!isPasswordValid) { // return reply.status(401).send({ // simplydata: { // error: true, // message: 'Invalid phone or password' // } // }); // } // // Generate a JWT token for the authenticated install // const token = fastify.jwt.sign({ phone: install.phone }, 'your_jwt_secret', { expiresIn: '30d' }); // // Fetch the profile picture if it exists // const profilePicture = await ProfilePictureInstall.findOne({ customerId: install._id }); // const responsePayload = { // simplydata: { // error: false, // apiversion: fastify.config.APIVERSION, // access_token: token, // email: install.emails, // installationId: install.installationId, // phone: install.phone, // address1: install.address1, // address2: install.address2, // phoneVerified: install.phoneVerified, // oneTimePasswordSetFlag: install.oneTimePasswordSetFlag, // type: install.profile.role, // fcmId: install.fcmId, // team: install.team, // city: install.city, // manager: install.manager, // firstName: install.firstName, // lastName: install.lastName, // address: install.address, // alternativeNumber: install.alternativeNumber, // } // }; // if (profilePicture) { // responsePayload.simplydata.picture = profilePicture.picture; // } // // Return the token and user details to the client // return reply.send(responsePayload); // } catch (err) { // reply.status(500).send({ // simplydata: { // error: true, // message: err.message // } // }); // } // }; exports.installationVerifyPhone = 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 Install.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 Install.findOneAndUpdate(filter, update); updatedDeliveryBoy = await Install.findOne({ phone: phone }); if (updatedDeliveryBoy.phoneVerified) { loginObject = await supplierController.loginInstallation(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( { name: loginObject.delivery.name, }, //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 ProfilePictureInstall.findOne({ installationId: 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, installationId: loginObject.delivery.installationId, name: loginObject.delivery.name, address: loginObject.delivery.address, phoneVerified: loginObject.delivery.phoneVerified, oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, }, }); } if (profilePicture) { reply.send({ simplydata: { error: false, apiversion: fastify.config.APIVERSION, access_token: token, picture: profilePicture.picture, phone: loginObject.delivery.phone, installationId: loginObject.delivery.installationId, name: loginObject.delivery.name, address: loginObject.delivery.address, phoneVerified: loginObject.delivery.phoneVerified, oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, }, }); } } } 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); } }; const generateStoreId = async () => { const result = await Counter.findOneAndUpdate( { _id: 'store_id' }, { $inc: { seq: 1 } }, { upsert: true, new: true } ); return result.seq; }; exports.addStore = async (req, reply) => { try { var s_id = await generateStoreId(); var building = ((req.body.storename).slice(0, 3)).toUpperCase(); var store_id = `AWSST${building}${s_id}`; s_data = { storeId: store_id, storename: req.body.storename, emails: req.body.emails, password: req.body.password, phone: req.body.phone, description: req.body.description, profile: { firstName: req.body.firstName, lastName: req.body.lastName, contactNumber: req.body.phone, alternativeContactNumber: req.body.alternativeContactNumber, office_address: req.body.office_address, country: req.body.country, state: req.body.state, city: req.body.city, zip: req.body.zip, }, latitude: req.body.latitude, longitude: req.body.longitude, fcmId: req.body.fcmId, }; var store = new Store(s_data); storepass = req.body.password; // Store hash in your password DB. hash = await bcryptPassword(storepass); if (hash) { store.services.password.bcrypt = hash; if (req.body.role) { store.profile.role = req.body.role; } else { role = ["store"]; store.profile.role = role; } insertedStore = await store.save(); if (insertedStore) { var retStore = { armintatankdata: { storename: insertedStore.storename, phone: insertedStore.phone, storeId: insertedStore.storeId, office_address: insertedStore.profile.office_address, emails: [ { email: insertedStore.emails[0].email, }, ], profile: insertedStore.profile, latitude: insertedStore.latitude, longitude: insertedStore.longitude, fcmId: insertedStore.fcmId, description: insertedStore.description, }, status_code: 200, }; return retStore; } } } catch (err) { throw boom.boomify(err); } }; // exports.loginStore = async (request, reply) => { // try { // let store = await Store.findOne({ phone: request.body.phone }); // if (!store) { // return reply.code(400).send({ // simplydata: { // error: true, // code: 400, // message: "Invalid Phone or Password", // }, // }); // } // const isMatch = await bcrypt.compare(request.body.password, store.services.password.bcrypt); // if (!isMatch) { // return reply.code(400).send({ // simplydata: { // error: true, // code: 400, // message: "Invalid Phone or Password", // }, // }); // } // const token = request.jwt.sign( // { // storename: store.storename, // storeId: store._id, // roles: store.profile.role, // }, // { expiresIn: "30d" } // ); // var profilePicture = await profilePictureStore.findOne({ storeId: store.storeId }); // if (!profilePicture) { // reply.send({ // simplydata: { // error: false, // apiversion: fastify.config.APIVERSION, // access_token: token, // email: store.emails, // phone: store.phone, // storeId: store.storeId, // storename: store.storename, // office_address: store.profile.office_address, // phoneVerified: store.phoneVerified, // oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, // latitude: store.latitude, // longitude: store.longitude, // description: store.description, // type: store.profile.role, // typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), // }, // }); // } else { // reply.send({ // simplydata: { // error: false, // apiversion: fastify.config.APIVERSION, // access_token: token, // picture: profilePicture.picture, // email: store.emails, // phone: store.phone, // storeId: store.storeId, // storename: store.storename, // office_address: store.profile.office_address, // phoneVerified: store.phoneVerified, // oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, // latitude: store.latitude, // longitude: store.longitude, // description: store.description, // type: store.profile.role, // typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), // }, // }); // } // } catch (err) { // reply.send({ // simplydata: { // error: true, // code: 500, // message: err.message, // }, // }); // } // };