update
This commit is contained in:
parent
db5adde072
commit
dd96bb3aff
@ -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
|
||||
|
@ -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({
|
||||
@ -117,18 +128,19 @@ export const registerUser = async (req, res) => {
|
||||
from: process.env.SEND_EMAIL_FROM,
|
||||
subject: `Cheminova Account Created`,
|
||||
html: `Your Principal Distributor Account is created successfully.
|
||||
<br/>Name: <strong>${name}</strong><br/>
|
||||
<br/>Mobile Number: <strong>${phone}</strong><br/>
|
||||
<br/>Password: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
|
||||
<br/>Name: <strong>${name}</strong><br/>
|
||||
<br/>Mobile Number: <strong>${phone}</strong><br/>
|
||||
<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;
|
||||
|
@ -53,7 +53,7 @@ const userSchema = new mongoose.Schema(
|
||||
},
|
||||
role: {
|
||||
type: String,
|
||||
default: "principal-Distributor",
|
||||
default: "user",
|
||||
},
|
||||
accessTo: {},
|
||||
resetPasswordToken: String,
|
||||
|
Loading…
Reference in New Issue
Block a user