This commit is contained in:
Sibunnayak 2024-07-24 11:39:21 +05:30
parent db5adde072
commit dd96bb3aff
3 changed files with 27 additions and 15 deletions

View File

@ -81,7 +81,7 @@ export const AddshippingAddressByAdmin = async (req, res) => {
// Validate PAN number format
const panNumberRegex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/;
if (!panNumberRegex.test(panNumber)) {
return res.status(400).json({ msg: "Invalid PAN number format" });
return res.status(400).json({ message: "Invalid PAN number format" });
}
// Create shipping address object

View File

@ -87,14 +87,25 @@ export const registerUser = async (req, res) => {
try {
const { name, email, password, phone, accessTo, role } = req.body;
let findUser = await User.findOne({ email });
if (findUser) {
return res
.status(400)
.json({ success: false, message: "User already exists" });
// Check if user already exists
let user = await User.findOne({ email });
if (user) {
// If user exists, update their details if needed
user.name = name;
user.password = password; // In a real application, you should hash this
user.phone = phone;
user.role = role;
user.accessTo = accessTo;
// Save updates
await user.save();
// Respond with success and userId
return res.status(200).json({ success: true, message: "User updated successfully", userId: user._id });
}
const newUser = new User({
// Create a new user if not found
user = new User({
name,
email,
password,
@ -106,10 +117,10 @@ export const registerUser = async (req, res) => {
// Generate uniqueId
const currentYear = new Date().getFullYear().toString().slice(-2);
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
newUser.uniqueId = `${currentYear}-${randomChars}`;
user.uniqueId = `${currentYear}-${randomChars}`;
// Save the new user to the database
await newUser.save();
await user.save();
// Send email with the new user details
await sendEmail({
@ -122,13 +133,14 @@ export const registerUser = async (req, res) => {
<br/>Password: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
});
// Respond with success and token
res.status(201).json({ success: true, message: "User created successfully", userId: newUser._id });
// Respond with success and userId
res.status(201).json({ success: true, message: "User created successfully", userId: user._id });
} catch (error) {
console.error(error);
res.status(400).json({ success: false, message: error.message });
}
};
// 2.Login User
export const loginUser = async (req, res, next) => {
const { email, password } = req.body;

View File

@ -53,7 +53,7 @@ const userSchema = new mongoose.Schema(
},
role: {
type: String,
default: "principal-Distributor",
default: "user",
},
accessTo: {},
resetPasswordToken: String,