add create user and login user parts
This commit is contained in:
+183
-260
@@ -9,19 +9,29 @@
|
||||
|
||||
// app/controllers/user.controller.js
|
||||
|
||||
// const { Op } = require("sequelize");
|
||||
const db = require("../models");
|
||||
const { hashPassword } = require("../utils/hashPassword.util");
|
||||
const { validatePassword } = require("../utils/validation/validatePassword.util");
|
||||
const {
|
||||
validatePassword,
|
||||
} = require("../utils/validation/validatePassword.util");
|
||||
const { validateEmail } = require("../utils/validation/validateEmail.util");
|
||||
const { generateUserId, generateId } = require("../utils/idGen.util");
|
||||
const {
|
||||
createCustomerDetails,
|
||||
} = require("../utils/users/createCustomerDetails.util");
|
||||
const {
|
||||
createBusinessCustomerDetails,
|
||||
} = require("../utils/users/createBusinessCustomer.util");
|
||||
const { logActivity } = require("../services/activity.service");
|
||||
const { sendMail } = require("../utils/mail.util");
|
||||
const {
|
||||
createEmailVerification, verifyEmailVerificationToken, deleteEmailVerification} = require("../utils/emailVerification.util");;
|
||||
createEmailVerification,
|
||||
verifyEmailVerificationToken,
|
||||
sendVerificationEmail,
|
||||
deleteEmailVerification,
|
||||
} = require("../utils/emailVerification.util");
|
||||
const User = db.User;
|
||||
const Profile = db.Profile;
|
||||
// const EmailVerification = db.EmailVerification;
|
||||
|
||||
// Create a new user
|
||||
exports.createNewUser = async (req, res) => {
|
||||
@@ -33,28 +43,48 @@ exports.createNewUser = async (req, res) => {
|
||||
lastName,
|
||||
email,
|
||||
password,
|
||||
accountType,
|
||||
address,
|
||||
phoneNumber,
|
||||
businessName,
|
||||
businessRegistrationNumber,
|
||||
businessType,
|
||||
contactName,
|
||||
businessEmail,
|
||||
expectedMonthlyVolume,
|
||||
note,
|
||||
} = req.body;
|
||||
|
||||
if (!firstName || !lastName || !email || !password) {
|
||||
await transaction.rollback();
|
||||
if (!firstName || !lastName || !email || !password || !accountType) {
|
||||
await transaction.rollback();
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message:
|
||||
"First name, last name, email and password are required",
|
||||
});
|
||||
}
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message:
|
||||
"First name, last name, email, password and account type are required",
|
||||
});
|
||||
}
|
||||
|
||||
const emailValid = validateEmail(email);
|
||||
if (accountType !== "customer" && accountType !== "business_customer") {
|
||||
await transaction.rollback();
|
||||
|
||||
if (!emailValid) {
|
||||
await transaction.rollback();
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message:
|
||||
"Invalid account type. Must be either 'customer' or 'business_customer'",
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message: "Invalid email address",
|
||||
});
|
||||
}
|
||||
const emailValid = validateEmail(email);
|
||||
|
||||
if (!emailValid) {
|
||||
await transaction.rollback();
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message: "Invalid email address",
|
||||
});
|
||||
}
|
||||
|
||||
const userExists = await User.findOne({ where: { email } });
|
||||
if (userExists) {
|
||||
@@ -66,14 +96,6 @@ if (!emailValid) {
|
||||
});
|
||||
}
|
||||
|
||||
// let pass;
|
||||
|
||||
// if(accountType === "admin" || accountType === "superadmin" || accountType === "manager" || accountType === "support_agent") {
|
||||
// pass = process.env.DEFAULT_PASSWORD;
|
||||
// }else{
|
||||
// pass = password;
|
||||
// }
|
||||
|
||||
const validatePasswordResult = validatePassword(password);
|
||||
|
||||
if (!validatePasswordResult) {
|
||||
@@ -95,13 +117,12 @@ if (!emailValid) {
|
||||
lastName,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
// accountType,
|
||||
accountType,
|
||||
accountStatus: "PENDING_VERIFICATION",
|
||||
emailVerifiedAt: null,
|
||||
emailVerifiedAt: null,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
const newProfile = await Profile.create(
|
||||
{
|
||||
@@ -117,78 +138,52 @@ if (!emailValid) {
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
//create customer and business customer
|
||||
if (accountType === "customer") {
|
||||
const customerData = {
|
||||
address,phoneNumber,
|
||||
};
|
||||
|
||||
await createCustomerDetails(
|
||||
newUser.id,
|
||||
customerData,
|
||||
transaction,
|
||||
);
|
||||
} else if (accountType === "business_customer") {
|
||||
const businessData = {
|
||||
businessName,
|
||||
businessRegistrationNumber,
|
||||
businessType,
|
||||
contactName,
|
||||
phoneNumber,
|
||||
businessEmail,
|
||||
expectedMonthlyVolume,
|
||||
note,
|
||||
};
|
||||
|
||||
await createBusinessCustomerDetails(
|
||||
newUser.id,
|
||||
businessData,
|
||||
transaction,
|
||||
);
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
const verificationToken = await createEmailVerification(newUser.id);
|
||||
|
||||
const confirmationLink =
|
||||
`${process.env.FRONTEND_URL}/verify-email?token=${verificationToken}`;
|
||||
|
||||
|
||||
try {
|
||||
|
||||
await sendMail({
|
||||
to:
|
||||
email,
|
||||
|
||||
subject:
|
||||
"Confirm Your ZUMRI Account",
|
||||
|
||||
templateName:
|
||||
"emailVerification",
|
||||
|
||||
templateVars: {
|
||||
|
||||
customer_name:
|
||||
firstName,
|
||||
|
||||
confirmation_link:
|
||||
confirmationLink,
|
||||
},
|
||||
|
||||
text:
|
||||
`Hello ${firstName}, please verify your ZUMRI account using this link: ${confirmationLink}`,
|
||||
});
|
||||
|
||||
|
||||
} catch (mailError) {
|
||||
|
||||
console.error(
|
||||
"VERIFICATION EMAIL ERROR:",
|
||||
mailError
|
||||
await sendVerificationEmail(
|
||||
newUser.email,
|
||||
newUser.firstName,
|
||||
verificationToken,
|
||||
);
|
||||
|
||||
|
||||
return res.status(201).send({
|
||||
success: true,
|
||||
|
||||
message:
|
||||
"Account created, but verification email could not be sent. Please request a new verification email.",
|
||||
|
||||
data: {
|
||||
id:
|
||||
newUser.id,
|
||||
|
||||
firstName:
|
||||
newUser.firstName,
|
||||
|
||||
lastName:
|
||||
newUser.lastName,
|
||||
|
||||
email:
|
||||
newUser.email,
|
||||
|
||||
accountType:
|
||||
newUser.accountType,
|
||||
|
||||
accountStatus:
|
||||
newUser.accountStatus,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error sending verification email:", error);
|
||||
}
|
||||
|
||||
await logActivity({
|
||||
user: req.user,
|
||||
user: newUser,
|
||||
description: `Created New User with ID: ${newUser.id}`,
|
||||
type: "CREATE_USER",
|
||||
module: "User Management",
|
||||
@@ -202,20 +197,16 @@ if (!emailValid) {
|
||||
firstName: newUser.firstName,
|
||||
lastName: newUser.lastName,
|
||||
email: newUser.email,
|
||||
// accountType: newUser.accountType,
|
||||
// role: newUser.role,
|
||||
// department: newUser.department,
|
||||
accountType: newUser.accountType,
|
||||
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (!transaction.finished) {
|
||||
await transaction.rollback();
|
||||
}
|
||||
if (!transaction.finished) {
|
||||
await transaction.rollback();
|
||||
}
|
||||
|
||||
console.error(
|
||||
"CREATE USER ERROR:",
|
||||
error
|
||||
);
|
||||
console.error("CREATE USER ERROR:", error);
|
||||
|
||||
res.status(500).send({
|
||||
success: false,
|
||||
@@ -226,168 +217,100 @@ if (!emailValid) {
|
||||
};
|
||||
|
||||
// Verify customer email
|
||||
exports.verifyEmail =
|
||||
async (req, res) => {
|
||||
exports.verifyEmail = async (req, res) => {
|
||||
const transaction = await db.sequelize.transaction();
|
||||
|
||||
const transaction =
|
||||
await db.sequelize.transaction();
|
||||
try {
|
||||
const { token } = req.body;
|
||||
|
||||
if (!token) {
|
||||
await transaction.rollback();
|
||||
|
||||
try {
|
||||
const {
|
||||
token,
|
||||
} = req.body;
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
|
||||
|
||||
if (!token) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
|
||||
return res.status(400).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Verification token is required",
|
||||
});
|
||||
}
|
||||
|
||||
const verification =
|
||||
await verifyEmailVerificationToken(
|
||||
token
|
||||
);
|
||||
|
||||
if (!verification) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
|
||||
return res.status(400).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Verification token is invalid or expired",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
const {
|
||||
userId,
|
||||
redisKey,
|
||||
} =
|
||||
verification;
|
||||
|
||||
const user =
|
||||
await User.findOne({
|
||||
where: {
|
||||
id:
|
||||
userId,
|
||||
},
|
||||
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
if (!user) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
await deleteEmailVerification(
|
||||
redisKey
|
||||
);
|
||||
|
||||
|
||||
return res.status(404).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"User not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
user.accountStatus ===
|
||||
"ACTIVE" &&
|
||||
user.emailVerifiedAt
|
||||
) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
|
||||
// Token is no longer needed
|
||||
await deleteEmailVerification(
|
||||
redisKey
|
||||
);
|
||||
|
||||
|
||||
return res.status(400).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Email is already verified",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
user.accountStatus =
|
||||
"ACTIVE";
|
||||
|
||||
|
||||
user.emailVerifiedAt =
|
||||
new Date();
|
||||
|
||||
|
||||
await user.save({
|
||||
transaction,
|
||||
});
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
await deleteEmailVerification(
|
||||
redisKey
|
||||
);
|
||||
|
||||
|
||||
|
||||
return res.status(200).send({
|
||||
success:
|
||||
true,
|
||||
|
||||
message:
|
||||
"Email verified successfully. Your account is now active.",
|
||||
});
|
||||
|
||||
|
||||
} catch (error) {
|
||||
|
||||
if (!transaction.finished) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
}
|
||||
|
||||
|
||||
console.error(
|
||||
"VERIFY EMAIL ERROR:",
|
||||
error
|
||||
);
|
||||
|
||||
|
||||
return res.status(500).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Failed to verify email",
|
||||
message: "Verification token is required",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const verification = await verifyEmailVerificationToken(token);
|
||||
|
||||
if (!verification) {
|
||||
await transaction.rollback();
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
|
||||
message: "Verification token is invalid or expired",
|
||||
});
|
||||
}
|
||||
|
||||
const { userId, redisKey } = verification;
|
||||
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
|
||||
transaction,
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
await transaction.rollback();
|
||||
|
||||
await deleteEmailVerification(redisKey);
|
||||
|
||||
return res.status(404).send({
|
||||
success: false,
|
||||
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (user.accountStatus === "ACTIVE" && user.emailVerifiedAt) {
|
||||
await transaction.rollback();
|
||||
|
||||
// Token is no longer needed
|
||||
await deleteEmailVerification(redisKey);
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
|
||||
message: "Email is already verified",
|
||||
});
|
||||
}
|
||||
|
||||
user.accountStatus = "ACTIVE";
|
||||
|
||||
user.emailVerifiedAt = new Date();
|
||||
|
||||
await user.save({
|
||||
transaction,
|
||||
});
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
await deleteEmailVerification(redisKey);
|
||||
|
||||
return res.status(200).send({
|
||||
success: true,
|
||||
|
||||
message: "Email verified successfully. Your account is now active.",
|
||||
});
|
||||
} catch (error) {
|
||||
if (!transaction.finished) {
|
||||
await transaction.rollback();
|
||||
}
|
||||
|
||||
console.error("VERIFY EMAIL ERROR:", error);
|
||||
|
||||
return res.status(500).send({
|
||||
success: false,
|
||||
|
||||
message: "Failed to verify email",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Get users with pagination (20 per page)
|
||||
exports.getAllUsers = async (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user