varun 1 year ago
commit dd4f1feea5

@ -0,0 +1,254 @@
const boom = require("boom");
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const fastify = require("fastify");
const { Install, ProfilePictureInstall } = require("../models/store");
const supplierController = require("../controllers/supplierController")
exports.installSignUp = async (request, reply) => {
try {
const {
name,
phone,
address,
installationId,
emails,
password,
profile,
team,
manager,
longitude,
latitude,
fcmId,
createdBy,
updatedBy,
} = request.body;
// Check if the email address is valid
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (emails.some((emailObj) => !emailRegex.test(emailObj.email))) {
return reply.status(400).send({ message: 'Invalid email address' });
}
// 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,
phone,
address,
installationId,
emails,
services: { password: { bcrypt: hashedPassword } },
profile,
team,
manager,
longitude,
latitude,
fcmId,
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 { phone1, password } = request.body
// Check if an admin with the email address exists
const install = await Install.findOne({ phone1 })
if (!install) {
return reply.status(401).send({ message: 'Invalid Phone1 or password' })
}
// Compare the password entered by the user with the hashed password stored in the database
const isPasswordValid = await bcrypt.compare(password, install.password)
if (!isPasswordValid) {
return reply.status(401).send({ message: 'Invalid phone or password' })
}
// Generate a JWT token for the authenticated admin
const token = jwt.sign({ phone1: install.phone1 }, 'secret')
// Return the token to the client
return { token }
} catch (err) {
reply.status(500).send({ 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);
}
};

@ -300,6 +300,38 @@ exports.loginUser = async (req) => {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.loginUserWithOTP = async (req) => {
try {
const phone = req.body.phone;
const phoneVerificationCode = req.body.phoneVerificationCode;
const userDetails = await User.findOne({ phone: phone });
const supplier = await Supplier.findOne({phone: phone})
const deliveryBoy = await DeliveryBoy.findOne( { phone : phone})
const installation = await Install.findOne( { phone : phone})
let user;
if(userDetails){
user = await User.findOne({ phone: phone, 'phoneVerificationCode': phoneVerificationCode });
}
if(supplier){
user = await Supplier.findOne({ phone: phone, 'phoneVerificationCode': phoneVerificationCode });
}
if(deliveryBoy){
user = await DeliveryBoy.findOne({ phone: phone, 'phoneVerificationCode': phoneVerificationCode });
}
if(installation){
user = await Install.findOne({ phone: phone, 'phoneVerificationCode': phoneVerificationCode });
}
if (user) {
return { same: true, user: user };
} else {
return { same: false };
}
} catch (err) {
throw boom.boomify(err);
}
};
// Update an existing user // Update an existing user
exports.updateUser = async (req, reply) => { exports.updateUser = async (req, reply) => {
@ -451,6 +483,7 @@ exports.logout = async (request, reply) => {
// controller.js // controller.js
const http = require('https'); const http = require('https');
const { Install } = require("../models/store");
exports.sendSms = async (request, reply) => { exports.sendSms = async (request, reply) => {
const code = Math.floor(100000 + Math.random() * 900000); const code = Math.floor(100000 + Math.random() * 900000);
@ -463,6 +496,9 @@ exports.sendSms = async (request, reply) => {
const user = await User.findOne({phone: mobile}) const user = await User.findOne({phone: mobile})
const supplier = await Supplier.findOne({phone: mobile}) const supplier = await Supplier.findOne({phone: mobile})
const deliveryBoy = await DeliveryBoy.findOne( { phone : mobile}) const deliveryBoy = await DeliveryBoy.findOne( { phone : mobile})
const installation = await Install.findOne( { phone : mobile})
if(user){ if(user){
await User.findOneAndUpdate({phone: mobile}, { $set: {'phoneVerificationCode': code } }) await User.findOneAndUpdate({phone: mobile}, { $set: {'phoneVerificationCode': code } })
} }
@ -472,6 +508,10 @@ exports.sendSms = async (request, reply) => {
if(deliveryBoy){ if(deliveryBoy){
await DeliveryBoy.findOneAndUpdate({phone: mobile}, { $set: {'phoneVerificationCode': code } }) await DeliveryBoy.findOneAndUpdate({phone: mobile}, { $set: {'phoneVerificationCode': code } })
} }
if(installation){
await Install.findOneAndUpdate({phone: mobile}, { $set: {'phoneVerificationCode': code } })
}
const apiUrl = `https://smslogin.co/v3/api.php?username=${username}&apikey=${apiKey}&senderid=${senderId}&mobile=${mobile}&message=${encodeURIComponent(message)}`; const apiUrl = `https://smslogin.co/v3/api.php?username=${username}&apikey=${apiKey}&senderid=${senderId}&mobile=${mobile}&message=${encodeURIComponent(message)}`;

@ -4,6 +4,8 @@ const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfileP
//const tanksController = require("./controllers/tanksController"); //const tanksController = require("./controllers/tanksController");
const tankersController = require("./controllers/tankersController.js"); const tankersController = require("./controllers/tankersController.js");
const createConnectionController = require("./controllers/createConnectionController"); const createConnectionController = require("./controllers/createConnectionController");
const storeController = require("./controllers/storeController.js")
const cors = require("cors"); const cors = require("cors");
const swagger = require("./config/swagger"); const swagger = require("./config/swagger");
const rawBody = require('raw-body') const rawBody = require('raw-body')
@ -274,6 +276,107 @@ fastify.post("/api/login", {
}, },
}); });
fastify.post("/api/installotplogin", {
schema: {
description: "This is for Login Otp Boy",
tags: ["Install"],
summary: "This is for Login Otp Boy",
body: {
type: "object",
required: ["phone", "phoneVerificationCode"],
properties: {
phoneVerificationCode: { type: "string" },
phone: { type: "string" },
},
},
},
async handler(req, reply) {
const { phone, phoneVerificationCode } = req.body;
// Assuming loginUserInstall function exists and works properly
try{
const loginObject = await userController.loginUserWithOTP(req);
if (loginObject.same) {
const phoneVerified = loginObject.user.phoneVerified;
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
if (!phoneVerified) {
reply.send({
simplydata: {
error: false,
phoneVerified: false,
phone: loginObject.user.phone,
oneTimePasswordSetFlag: oneTimePasswordSetFlag,
message: "Please Verify your phone number",
},
});
} else if (oneTimePasswordSetFlag) {
reply.send({
simplydata: {
error: false,
phoneVerified: phoneVerified,
phone: loginObject.user.phone,
oneTimePasswordSetFlag: true,
message: "Password must be reset",
},
});
} else {
const token = fastify.jwt.sign(
{
name: loginObject.user.name,
},
'your_jwt_secret', // Replace with your actual JWT secret
{ expiresIn: '30d' }
);
const profilePicture = await ProfilePictureInstall.findOne({ customerId: loginObject.user._id });
const responsePayload = {
simplydata: {
error: false,
apiversion: fastify.config.APIVERSION,
access_token: token,
email: loginObject.user.emails,
phone: loginObject.user.phone,
name: loginObject.user.name,
address1: loginObject.user.profile.address1,
address2: loginObject.user.profile.address2,
phoneVerified: loginObject.user.phoneVerified,
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
type: loginObject.user.profile.role,
fcmId: loginObject.user.fcmId,
team: loginObject.user.team,
city:loginObject.user.city,
manager:loginObject.user.manager,
// typeasobj: JSON.parse(loginObject.user.profile.role),
},
};
if (profilePicture) {
responsePayload.simplydata.picture = profilePicture.picture;
}
reply.send(responsePayload);
}
} else {
reply.send({
simplydata: {
error: true,
code: 400,
message: "Invalid phone or phoneVerificationCode supplied",
},
});
}
}catch(e){
console.log('errrorrrr',e)
}
},
});
fastify.get("/api/reset_token/:customerId", { fastify.get("/api/reset_token/:customerId", {
schema: { schema: {
@ -346,6 +449,7 @@ fastify.register(require("./routes/supplierRoute"));
fastify.register(require("./routes/supplierOrdersRoutes")); fastify.register(require("./routes/supplierOrdersRoutes"));
fastify.register(require("./routes/friendRequestRoute")); fastify.register(require("./routes/friendRequestRoute"));
fastify.register(require("./routes/adminRoute")); fastify.register(require("./routes/adminRoute"));
fastify.register(require("./routes/storeRoute"));
// Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone
// Also allows deletion of a user with a given phone number // Also allows deletion of a user with a given phone number
@ -358,6 +462,7 @@ fastify.register(require("./routes/forTestingRoute"));
const {Storage} = require('@google-cloud/storage'); const {Storage} = require('@google-cloud/storage');
const { Supplier, profilePictureSupplier } = require("./models/supplier"); const { Supplier, profilePictureSupplier } = require("./models/supplier");
const multer = require('fastify-multer'); const multer = require('fastify-multer');
const { ProfilePictureInstall } = require("./models/store.js");
fastify.register(require('fastify-formbody')); fastify.register(require('fastify-formbody'));
// fastify.register(multer.contentParser); // fastify.register(multer.contentParser);
// const multipart = require('fastify-multipart'); // const multipart = require('fastify-multipart');

@ -0,0 +1,75 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;
const installationschema = new mongoose.Schema({
name: { type: String },
phone: { type: String, unique: true, trim: true },
address: String,
installationId: { type: String },
phoneVerified: { type: Boolean, default: false },
phoneVerificationCode: { type: Number, default: 11111 },
passwordResetCode: { type: Number},
oneTimePasswordSetFlag: { type: Boolean, default: false },
emails: [{ email: String, verified: { type: Boolean, default: false } }],
services: { password: { bcrypt: String } },
profile: {
alternativeNumber: { type: String, default: null },
address1: { type: String, default: null },
address2: { type: String, default: null },
city: { type: String, default: null },
state: { type: String, default: null },
country: { type: String, default: null },
},
team : { type: String, default: null},
manager : { type: String, default: null},
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},
fcmId: { type: String, default: null },
createdAt: {
type: Date,
default: function () {
return Date.now();
},
},
createdBy: ObjectId,
updatedAt: {
type: Date,
default: function () {
return Date.now();
},
},
updatedBy: ObjectId,
});
const profilePictureInstallSchema = new Schema({
installationId: {
type: String,
unique: true,
required: true
},
picture: {
type: String, // Change the type to String
required: true,
validate: {
validator: function (value) {
const supportedFormats = ['jpg', 'jpeg', 'png'];
const fileExtension = value.split('.').pop().toLowerCase();
return supportedFormats.includes(fileExtension);
},
message: 'Picture must be a JPEG, PNG, or JPG image'
}
}
});
const ProfilePictureInstall = mongoose.model('ProfilePictureInstall', profilePictureInstallSchema);
const Install = mongoose.model("Install", installationschema);
module.exports = { Install, ProfilePictureInstall};

@ -0,0 +1,88 @@
const fastify = require("fastify");
const storeController = require('../controllers/storeController')
module.exports = function (fastify, opts, next) {
fastify.route({
method: 'POST',
url: '/api/installSignup',
schema: {
tags: ['Install'],
description: 'This is for creating a New Install Account',
summary: 'This is for creating a New Install Account',
body: {
type: 'object',
properties: {
phone: { type: 'string' },
password: { type: 'string' },
emails: {
type: 'array',
maxItems: 2,
items: {
type: 'object',
properties: {
email: { type: 'string', default: null },
},
},
},
name: { type: 'string' },
team: { type: 'string', default: null },
manager: { type: 'string', default: null },
address1: { type: 'string', default: null },
address2: { type: 'string', default: null },
city: { type: 'string', default: null },
state: { type: 'string', default: null },
zip: { type: 'string', default: null },
country: { type: 'string', default: null },
notes: { type: 'string', default: null },
latitude: { type: 'number', default: 0.0 },
longitude: { type: 'number', default: 0.0 },
fcmId: { type: 'string', default: null },
},
},
security: [
{
basicAuth: [],
},
],
},
handler: storeController.installSignUp,
});
fastify.post("/api/insatllLogin", {
schema: {
description: "This is for Login Install",
tags: ["Install"],
summary: "This is for Login Install",
body: {
type: "object",
required: ["phone1", "password"],
properties: {
phone1: { type: "string" },
password: { type: "string" },
phoneVerificationCode: { type: "string" },
},
},
},
handler: storeController.installLogin,
});
// fastify.post("/api/installotplogin", {
// schema: {
// description: "This is for Login Otp Boy",
// tags: ["Install"],
// summary: "This is for Login Otp Boy",
// body: {
// type: "object",
// required: ["phone"],
// properties: {
// phoneVerificationCode: { type: "string" },
// phone: { type: "string" },
// },
// },
// },
// handler: storeController.installationVerifyPhone,
// });
next();
};
Loading…
Cancel
Save