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 // Validate PAN number format
const panNumberRegex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/; const panNumberRegex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/;
if (!panNumberRegex.test(panNumber)) { 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 // Create shipping address object

View File

@ -87,14 +87,25 @@ export const registerUser = async (req, res) => {
try { try {
const { name, email, password, phone, accessTo, role } = req.body; const { name, email, password, phone, accessTo, role } = req.body;
let findUser = await User.findOne({ email }); // Check if user already exists
if (findUser) { let user = await User.findOne({ email });
return res if (user) {
.status(400) // If user exists, update their details if needed
.json({ success: false, message: "User already exists" }); 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, name,
email, email,
password, password,
@ -106,10 +117,10 @@ export const registerUser = async (req, res) => {
// Generate uniqueId // Generate uniqueId
const currentYear = new Date().getFullYear().toString().slice(-2); const currentYear = new Date().getFullYear().toString().slice(-2);
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase(); const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
newUser.uniqueId = `${currentYear}-${randomChars}`; user.uniqueId = `${currentYear}-${randomChars}`;
// Save the new user to the database // Save the new user to the database
await newUser.save(); await user.save();
// Send email with the new user details // Send email with the new user details
await sendEmail({ await sendEmail({
@ -117,18 +128,19 @@ export const registerUser = async (req, res) => {
from: process.env.SEND_EMAIL_FROM, from: process.env.SEND_EMAIL_FROM,
subject: `Cheminova Account Created`, subject: `Cheminova Account Created`,
html: `Your Principal Distributor Account is created successfully. html: `Your Principal Distributor Account is created successfully.
<br/>Name: <strong>${name}</strong><br/> <br/>Name: <strong>${name}</strong><br/>
<br/>Mobile Number: <strong>${phone}</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/>Password: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
}); });
// Respond with success and token // Respond with success and userId
res.status(201).json({ success: true, message: "User created successfully", userId: newUser._id }); res.status(201).json({ success: true, message: "User created successfully", userId: user._id });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(400).json({ success: false, message: error.message }); res.status(400).json({ success: false, message: error.message });
} }
}; };
// 2.Login User // 2.Login User
export const loginUser = async (req, res, next) => { export const loginUser = async (req, res, next) => {
const { email, password } = req.body; const { email, password } = req.body;

View File

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