From dd96bb3aff39cb79965d28102551bb9594349a8b Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Wed, 24 Jul 2024 11:39:21 +0530 Subject: [PATCH] update --- .../ShippingAddressController.js | 2 +- resources/user/userController.js | 38 ++++++++++++------- resources/user/userModel.js | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/resources/ShippingAddresses/ShippingAddressController.js b/resources/ShippingAddresses/ShippingAddressController.js index 5e40f5a..cdd374f 100644 --- a/resources/ShippingAddresses/ShippingAddressController.js +++ b/resources/ShippingAddresses/ShippingAddressController.js @@ -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 diff --git a/resources/user/userController.js b/resources/user/userController.js index 56a4423..916c004 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -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. -
Name: ${name}
-
Mobile Number: ${phone}
-
Password: ${password}

If you have not requested this email, please ignore it.`, +
Name: ${name}
+
Mobile Number: ${phone}
+
Password: ${password}

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; diff --git a/resources/user/userModel.js b/resources/user/userModel.js index b25c7ec..b79422d 100644 --- a/resources/user/userModel.js +++ b/resources/user/userModel.js @@ -53,7 +53,7 @@ const userSchema = new mongoose.Schema( }, role: { type: String, - default: "principal-Distributor", + default: "user", }, accessTo: {}, resetPasswordToken: String,