File: /var/www/javago-api-updates/src/controllers/auth/auth.controller.js
import * as authService from "../../services/auth/auth.services.js";
import bcrypt from "bcrypt";
import crypto from "crypto";
import moment from "moment";
import jwt from "jsonwebtoken";
import { config } from "../../config/config.js";
import { messages } from "../../config/response.messages.js";
import * as Email from "../../helper/email.js";
import * as Helper from "../../helper/index.js";
import User from "../../models/users.model.js";
export async function login(req, res) {
try {
let userDetails = await authService.findByEmail(req.body);
if (userDetails != null) {
//Password verification
if (!(await bcrypt.compare(req.body.password, userDetails.password))) {
return res.status(401).send({
status: 401,
message: messages.login_password_incorrect,
});
} else if (userDetails.is_verified != 1) {
let sendMail = await Email.sendVerifyOTPMail(req.body.email);
if (sendMail != false) {
let updateUser = {
otp: sendMail,
};
await authService.updateUserByEmail(updateUser, req.body.email);
return res.status(203).send({
status: 203,
message: messages.email_address_not_verified,
});
} else {
return res.status(422).send({
status: 422,
message: messages.invalid_email,
});
}
} else if (userDetails.is_active == 0 || userDetails.is_deleted == 1) {
return res.status(401).send({
status: 401,
message: messages.account_inactive,
});
} else {
//Token generation
let updateUser = {
device_id: req.body.device_id,
device_type: req.body.device_type,
fcm_token: req.body.fcm_token,
};
await authService.updateUserByEmail(updateUser, req.body.email);
let tokenObject = {
user_id: userDetails.id,
email: req.body.email,
};
userDetails.dataValues.token = jwt.sign(
tokenObject,
config.jwt_encryption
);
return res.status(200).send({
status: 200,
message: messages.login_success,
data: userDetails,
});
}
} else {
return res.status(400).send({
status: 400,
message: messages.email_not_found,
});
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function verifyOtp(req, res) {
try {
let userDetails = await authService.findByEmail(req.body);
if (userDetails != null) {
if (req.body.otp == userDetails.dataValues.otp) {
let updateUser = {
is_verified: 1,
otp: 0,
type: 1,
notification_status: 1,
};
let tokenObject = {
user_id: userDetails.dataValues.id,
email: userDetails.dataValues.email,
};
const token = jwt.sign(tokenObject, config.jwt_encryption);
userDetails.dataValues.token = token;
userDetails.dataValues.type = updateUser.type;
await authService.updateUserByEmail(updateUser, req.body.email);
return res.status(200).send({
status: 200,
message: messages.otp_verified,
data: userDetails,
});
} else {
return res.status(400).send({
status: 400,
message: messages.otp_not_verified,
});
}
} else {
return res.status(400).send({
status: 400,
message: messages.email_not_found,
});
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function sendOtp(req, res) {
try {
let userDetails = await authService.findByEmail(req.body);
if (userDetails != null) {
let sendMail = await Email.sendVerifyOTPMail(req.body.email);
if (sendMail != false) {
let updateUser = {
otp: sendMail,
};
await authService.updateUserByEmail(updateUser, req.body.email);
return res.status(200).send({
status: 200,
message: messages.otp_send,
});
} else {
return res.status(422).send({
status: 422,
message: messages.invalid_email,
});
}
} else {
return res.status(400).send({
status: 400,
message: messages.email_not_found,
});
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function userList(req, res) {
try {
let userDetails = await authService.userList();
return res.status(200).send({
status: 200,
message: messages.user_found,
data: userDetails,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export const createUser = async (req, res) => {
try {
const salt = await bcrypt.genSalt(10);
req.body.password = await bcrypt.hash(req.body.password, salt);
var isCreate;
if (req.body.guest_id) {
await authService.updateUser(req.body, req.body.guest_id);
isCreate = true;
} else {
isCreate = await authService.createUser(req.body);
}
if (isCreate != false) {
let sendMail = await Email.sendVerifyOTPMail(req.body.email);
let updateUser = {
is_active: 1,
otp: sendMail,
};
await authService.updateUserByEmail(updateUser, req.body.email);
if (sendMail != false) {
return res.status(201).send({
status: 201,
message: messages.user_created,
});
} else {
return res.status(422).send({
status: 422,
message: messages.invalid_email,
});
}
} else {
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
});
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
};
export async function isEmailExist(req, res, next) {
try {
let userData = await authService.findByEmail({ email: req.body.email });
if (userData != null) {
if (userData.is_verified != 1 || userData.type == 0) {
let sendMail = await Email.sendVerifyOTPMail(req.body.email);
if (sendMail != false) {
let updateUser = {
otp: sendMail,
};
await authService.updateUserByEmail(updateUser, req.body.email);
//redirect to the verify OTP
return res.status(203).send({
status: 203,
message: messages.email_address_not_verified,
});
} else {
return res.status(422).send({
status: 422,
message: messages.invalid_email,
});
}
} else if (userData.is_deleted != 0 && userData.is_active != 0) {
return res.status(401).send({
status: 401,
message: messages.account_inactive,
});
} else {
//need to manage multi device
return res.status(403).send({
status: 403,
message: messages.email_already_exist,
});
}
} else {
next();
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function updateUser(req, res) {
try {
let userDetails = {
name: req.body.name,
profile_picture: req.body.profile_picture,
};
await authService.updateUser(userDetails, req.user_id);
return res.status(202).send({
status: 202,
message: messages.user_updated,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function updateUserFcm(req, res) {
try {
let userDetails = {
fcm_token: req.body.fcm_token,
};
await authService.updateUser(userDetails, req.user_id);
return res.status(202).send({
status: 202,
message: messages.fcm_updated,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function deleteUser(req, res) {
try {
req.body.is_deleted = 1;
await authService.updateUser(req.body, req.user_id);
return res.status(202).send({
status: 202,
message: messages.user_deleted,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function userProfile(req, res) {
try {
let userDetails = await authService.findById({ user_id: req.user_id });
return res.status(202).send({
status: 202,
message: messages.user_found,
data: userDetails,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function appVersions(req, res) {
try {
let versionDetails = await authService.appVersionDetails(req.query.os_type);
return res.status(200).send({
status: 200,
message: messages.version_found,
data: versionDetails,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function logout(req, res) {
try {
await authService.updateUser({ fcm_token: "" }, req.user_id);
return res.status(202).send({
status: 202,
message: messages.logout_success,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function ChangePassword(req, res) {
try {
let result = await authService.findById({ user_id: req.user_id });
let comparePassword = await bcrypt.compare(
req.body.old_password,
result.password
);
if (!comparePassword) {
return res.status(401).send({
status: 401,
message: messages.login_password_incorrect,
});
} else {
if (req.body.new_password == req.body.old_password) {
return res.status(400).send({
status: 400,
message: messages.old_new_same,
});
} else {
const salt = await bcrypt.genSalt(10);
req.body.password = await bcrypt.hash(req.body.new_password, 10);
await authService.updateUser(
{ password: req.body.password },
req.user_id
);
return res.status(202).send({
status: 202,
message: messages.password_change,
});
}
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function ForgotPassword(req, res) {
try {
let createobject = {
token: crypto.randomBytes(48).toString("base64url"),
email: req.body.email,
expired_at: moment().add(5, "minutes").unix(),
created_at: moment().unix(),
user_type: "user",
};
var isEmail = await authService.getUserEmail(req.body, createobject);
if (isEmail) {
if (isEmail.is_verified == 0) {
return res.status(202).send({
status: 203,
message: messages.email_address_not_verified,
});
}
var isData = await authService.ForgotPasswordByEmail(req.body.email);
if (isData) {
await authService.updateForgotPasswordByEmail(
createobject,
req.body.email
);
} else {
await authService.createForgotPassword(createobject);
}
await Email.sendResetPasswordMail(
isEmail.name,
req.body.email,
createobject.token,
createobject.user_type
);
return res.status(202).send({
status: 202,
message: messages.Forgot_password,
});
} else {
return res.status(201).send({
status: 203,
message: messages.email_not_found,
});
}
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(501).send({
status: 501,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function manageNotificationStatus(req, res) {
try {
await authService.updateUser(
{ notification_status: req.body.notification_status },
req.user_id
);
return res.status(202).send({
status: 202,
message: messages.status_change,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
});
}
}
export async function notificationList(req, res) {
try {
let data = await authService.getNotificationList(
{ user_id: req.user_id },
req.query
);
for (let i = 0; i < data.docs.length; i++) {
const notification = data.docs[i].dataValues;
if (notification.notification_type == 2) {
// (notification.title = messages.coffe_run_invite),
// (notification.description =
// notification.sender?.name || "Someone" + messages.initiate_java_go);
const senderName = notification.sender?.name || "Someone";
// Check contributor flag directly from joined model
if (
notification.group_coffee_run_data &&
notification.group_coffee_run_data.is_contributor_person === 1
) {
notification.title = messages.coffe_run_invite;
notification.description = `${senderName} will be covering your order! Choose what you want.`;
} else {
notification.title = messages.coffe_run_invite;
notification.description = `${senderName}${messages.initiate_java_go}`;
}
} else if (notification.notification_type == 4) {
(notification.title = messages.universal_loyalty_card),
(notification.description =
notification.sender?.name ||
"Someone" + messages.tap_here_to_view_your_universal);
} else if (notification.notification_type == 5) {
(notification.title = messages.pay_order_request),
(notification.description =
notification.sender?.name ||
"Someone" + messages.pay_order_request_amount);
} else if (notification.notification_type == 6) {
(notification.title = messages.schedule_notification_title),
(notification.description = messages.schedule_notification_body);
} else if (notification.notification_type == 7) {
(notification.title = messages.coffe_run_request),
(notification.description =
notification.sender?.name ||
"Someone" + messages.coffe_run_request_message);
} else if (notification.notification_type == 8) {
(notification.title = "Order Reminder Notification."),
(notification.description = "Your order will arrive in 5 minutes!");
} else if (notification.notification_type == 9) {
(notification.title = "Order Refund Notification."),
(notification.description =
"Your order refund has been processed and will reflect shortly in your Account!");
const getOrderDetails = await authService.getNotificationDetails(
notification.id
);
notification.order_details = getOrderDetails;
} else if (notification.notification_type == 10) {
(notification.title = "Order Ready Notification."),
(notification.description =
"Your order is Ready! Please collect it.");
const getOrderDetails = await authService.getNotificationDetails(
notification.id
);
notification.order_details = getOrderDetails;
}else if (notification.notification_type == 101) {
(notification.title = "Order Complete Notification."),
(notification.description =
"Your order has been completed!");
const getOrderDetails = await authService.getNotificationDetails(
notification.id
);
notification.order_details = getOrderDetails;
} else {
(notification.title = ""), (notification.description = "");
}
}
return res.status(200).send({
status: 200,
message: messages.notification_list,
data: data,
});
} catch (error) {
console.log(error);
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
});
}
}
export async function updateCoffeeRunRequest(req, res) {
try {
const data = await authService.updateCoffeeRunRequest(
req.params.id,
req.body.coffee_run_id
);
return res.status(200).send({
status: 200,
message: messages.coffe_run_request,
data: data,
});
} catch (error) {
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function createCoffeeRunRequest(req, res) {
try {
req.body.user = req.userData;
await authService.createCoffeeRunRequest(req.body);
return res.status(200).send({
status: 200,
message: messages.coffe_run_request,
});
} catch (error) {
if (error.message.includes(messages.group_cafe_run_already_added)) {
return res.status(403).send({
status: 403,
message: messages.group_cafe_run_already_added,
error: error,
});
}
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function countNotifications(req, res) {
try {
let count = await authService.getNotificationCount(req.user_id);
return res.status(200).send({
status: 200,
messages: messages.data_found,
data: {
flag: count > 0 ? 1 : 0,
},
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function removeNotification(req, res) {
try {
if (req.params.id == 0) {
await authService.removeAllNotification(req.user_id);
} else {
await authService.removeNotification(
{ user_id: req.user_id },
req.params
);
}
return res.status(202).send({
status: 202,
messages: messages.notification_removed,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function s3BucketDetails(req, res) {
try {
let s3Details = await authService.s3BucketDetails(req.query.file_type);
return res.status(200).send({
status: 200,
messages: messages.notification_removed,
data: s3Details,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function createGuestUser(req, res) {
try {
const salt = await bcrypt.genSalt(10);
const name = `guest_${crypto.randomBytes(6).toString("base64url")}`;
const email = `${name}@yopmail.com`;
const password = await bcrypt.hash(
crypto.randomBytes(8).toString("base64url"),
salt
);
req.body.name = name;
req.body.email = email;
req.body.password = password;
req.body.is_verified = 1;
req.body.is_active = 1;
req.body.notification_status = 1;
const user = await User.create(req.body);
let tokenObject = {
user_id: user.dataValues.id,
email: user.dataValues.email,
};
const token = jwt.sign(tokenObject, config.jwt_encryption);
res.send({
status: 200,
message: messages.login_success,
data: {
id: user.dataValues.id,
name: user.dataValues.name,
email: user.dataValues.email,
otp: user.dataValues.otp,
profile_picture: user.dataValues.profile_picture,
notification_status: user.dataValues.notification_status,
is_verified: user.dataValues.is_verified,
is_active: user.dataValues.is_active,
type: user.dataValues.type,
token: token,
},
});
} catch (error) {
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function updateRefferalCode(req, res) {
try {
const referalCode = await authService.updateReferralCode(req.user_id);
return res.status(202).send({
status: 202,
code: referalCode,
message: messages.refferal_updated,
});
} catch (error) {
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error,
});
}
}
export async function joinedByCode(req, res) {
try {
const joinedData = req.body;
joinedData.userId = req.user_id;
const result = await authService.joinedByCode(joinedData);
if (result.status === 404) {
return res.status(404).send({
status: 404,
message: result.message,
});
}
return res.status(200).send({
status: 200,
message: result.message,
});
} catch (error) {
console.log(error);
Helper.writeErrorLog(req, error);
return res.status(500).send({
status: 500,
message: messages.something_went_wrong,
error: error.message || error,
});
}
}