Merge branch 'master' of http://35.207.205.18:3000/Arminta/watermanagement-backend
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);
|
||||||
|
}
|
||||||
|
};
|
@ -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…
Reference in new issue