|
|
|
@ -154,7 +154,6 @@ fastify.register(require('point-of-view'), {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// * This is for login user as a simply user *
|
|
|
|
|
|
|
|
|
|
fastify.post("/api/login", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "This is for Login User",
|
|
|
|
@ -166,118 +165,146 @@ fastify.post("/api/login", {
|
|
|
|
|
properties: {
|
|
|
|
|
phone: { type: "string" },
|
|
|
|
|
password: { type: "string" },
|
|
|
|
|
fcmId: { type: "string" }, // Add this line
|
|
|
|
|
deviceId: { type: "string" } // Add this line
|
|
|
|
|
fcmId: { type: "string" },
|
|
|
|
|
deviceId: { type: "string" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
async handler(req, reply) {
|
|
|
|
|
// Pass fcmId and deviceId to the loginUser function
|
|
|
|
|
const { phone, password, fcmId, deviceId } = req.body;
|
|
|
|
|
const loginObject = await userController.loginUser(req, fcmId, deviceId);
|
|
|
|
|
|
|
|
|
|
if (loginObject.same) {
|
|
|
|
|
const phoneVerified = loginObject.user.phoneVerified;
|
|
|
|
|
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
|
|
|
|
|
console.log(
|
|
|
|
|
"oneTimePasswordSetFlag is ......",
|
|
|
|
|
oneTimePasswordSetFlag,
|
|
|
|
|
typeof oneTimePasswordSetFlag,
|
|
|
|
|
typeof phoneVerified
|
|
|
|
|
);
|
|
|
|
|
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(
|
|
|
|
|
{
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
userId: loginObject.user._id,
|
|
|
|
|
roles: loginObject.user.profile.role,
|
|
|
|
|
},
|
|
|
|
|
{ expiresIn: "30d" }
|
|
|
|
|
);
|
|
|
|
|
const arr = loginObject.user.profile.role;
|
|
|
|
|
const arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string
|
|
|
|
|
const stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
|
|
|
|
|
const c_id = loginObject.user.customerId;
|
|
|
|
|
const profilePicture = await ProfilePicture.findOne({ customerId: c_id });
|
|
|
|
|
|
|
|
|
|
if (!profilePicture) {
|
|
|
|
|
reply.send({
|
|
|
|
|
// First check for user login
|
|
|
|
|
const user = await User.findOne({ phone });
|
|
|
|
|
if (user) {
|
|
|
|
|
const loginObject = await userController.loginUser(req, fcmId, deviceId);
|
|
|
|
|
if (loginObject.same) {
|
|
|
|
|
const phoneVerified = loginObject.user.phoneVerified;
|
|
|
|
|
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
|
|
|
|
|
|
|
|
|
|
if (!phoneVerified) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
|
access_token: token,
|
|
|
|
|
buildingName: loginObject.user.buildingName,
|
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
|
phoneVerified: false,
|
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
|
customerId: loginObject.user.customerId,
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
|
latitude: loginObject.user.latitude,
|
|
|
|
|
longitude: loginObject.user.longitude,
|
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
|
oneTimePasswordSetFlag: oneTimePasswordSetFlag,
|
|
|
|
|
message: "Please Verify your phone number",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
reply.send({
|
|
|
|
|
} else if (oneTimePasswordSetFlag) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
|
access_token: token,
|
|
|
|
|
picture: profilePicture.picture,
|
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
|
phoneVerified: phoneVerified,
|
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
|
buildingName: loginObject.user.buildingName,
|
|
|
|
|
customerId: loginObject.user.customerId,
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
|
latitude: loginObject.user.latitude,
|
|
|
|
|
longitude: loginObject.user.longitude,
|
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
|
oneTimePasswordSetFlag: true,
|
|
|
|
|
message: "Password must be reset",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
|
{
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
userId: loginObject.user._id,
|
|
|
|
|
roles: loginObject.user.profile.role,
|
|
|
|
|
},
|
|
|
|
|
{ expiresIn: "30d" }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const arr = loginObject.user.profile.role;
|
|
|
|
|
const arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string
|
|
|
|
|
const stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
|
|
|
|
|
const c_id = loginObject.user.customerId;
|
|
|
|
|
const profilePicture = await ProfilePicture.findOne({ customerId: c_id });
|
|
|
|
|
|
|
|
|
|
if (!profilePicture) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
|
access_token: token,
|
|
|
|
|
buildingName: loginObject.user.buildingName,
|
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
|
customerId: loginObject.user.customerId,
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
|
latitude: loginObject.user.latitude,
|
|
|
|
|
longitude: loginObject.user.longitude,
|
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
|
access_token: token,
|
|
|
|
|
picture: profilePicture.picture,
|
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
|
buildingName: loginObject.user.buildingName,
|
|
|
|
|
customerId: loginObject.user.customerId,
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
|
latitude: loginObject.user.latitude,
|
|
|
|
|
longitude: loginObject.user.longitude,
|
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: true,
|
|
|
|
|
code: 400,
|
|
|
|
|
message: "Invalid UserId or Password supplied",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for staff login
|
|
|
|
|
const allUsers = await User.find({ "staff.staff": { $elemMatch: { phone } } });
|
|
|
|
|
for (const currentUser of allUsers) {
|
|
|
|
|
const staffMember = currentUser.staff.staff.find(staff => staff.phone === phone && staff.status === "active");
|
|
|
|
|
if (staffMember && staffMember.password === password) {
|
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
|
{
|
|
|
|
|
name: staffMember.name,
|
|
|
|
|
phone: staffMember.phone,
|
|
|
|
|
customerId: currentUser.customerId,
|
|
|
|
|
role: 'staff',
|
|
|
|
|
},
|
|
|
|
|
{ expiresIn: "30d" }
|
|
|
|
|
);
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
access_token: token,
|
|
|
|
|
name: staffMember.name,
|
|
|
|
|
phone: staffMember.phone,
|
|
|
|
|
customerId: currentUser.customerId,
|
|
|
|
|
message: "Staff login successful",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no user or staff found
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: true,
|
|
|
|
|
code: 400,
|
|
|
|
|
message: "Invalid UserId or Password supplied",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fastify.post("/api/installotplogin", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "This is for Login Otp Installation",
|
|
|
|
|