diff --git a/resources/user/userController.js b/resources/user/userController.js index af359e2..80131af 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -16,6 +16,7 @@ import validator from "validator"; import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js"; import { generatePassword } from "../../Utils/generatepassword.js"; import { PdOrder } from "../PD_Orders/pdOrderModal.js"; +import bcrypt from "bcryptjs"; export const uploadPrincipaldistributors = async (req, res) => { try { @@ -494,7 +495,75 @@ export const registerUser = async (req, res) => { res.status(400).json({ success: false, message: error.message }); } }; +export const UpdateUser = async (req, res) => { + try { + const { name, email, phone, accessTo, role, PD_ID, SBU } = req.body; + const id = req.params.id; + // Check if ID is provided and valid + if (!id) { + return res.status(400).json({ success: false, message: "User ID is required" }); + } + + // Find the user by ID + let user = await User.findById(id); + if (!user) { + return res.status(404).json({ success: false, message: "User not found" }); + } + + // Update user details only if provided + user.name = name || user.name; + user.email = email || user.email; + user.phone = phone || user.phone; + user.role = role || user.role; + user.accessTo = accessTo || user.accessTo; + user.SBU = SBU || user.SBU; + user.uniqueId = PD_ID || user.uniqueId; + + // Save the updated user details to the database + await user.save(); + + // If the user is a Principal Distributor, send an email + if (user.role === "principal-Distributor" && email) { + await sendEmail({ + to: email, + from: process.env.SEND_EMAIL_FROM, + subject: `Welcome to Cheminova - Account Updated Successfully`, + html: ` +

Dear ${name || "User"},

+

We are pleased to inform you that your Principal Distributor account has been successfully updated. Please find your account details below:

+

Name: ${name || "N/A"}

+

Mobile Number: ${phone || "N/A"}

+

Email: ${email || "N/A"}

+
+

Your password remains the same as before.

+

You can log in to your account using the following link:

+

Click here to log in

+
+

For convenience, you can also download our mobile app from the following link:

+ +
+

If you did not request this update or have any concerns, please contact our support team immediately.

+
+

Best regards,

+

Cheminova Support Team

+ `, + }); + } + + // Respond with success + res.status(200).json({ + success: true, + message: "User updated successfully", + userId: user._id, + }); + } catch (error) { + console.error("Error updating user:", error); + res.status(500).json({ success: false, message: "Internal Server Error" }); + } +}; // 2.Login User export const loginUser = async (req, res, next) => { const { email, password } = req.body; diff --git a/resources/user/userRoute.js b/resources/user/userRoute.js index ac62581..a64381b 100644 --- a/resources/user/userRoute.js +++ b/resources/user/userRoute.js @@ -25,12 +25,15 @@ import { getAllPD, generatePrincipalDistributorReport, ResetPasswordAdmin, + UpdateUser, } from "./userController.js"; import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; const router = express.Router(); router.route("/user/register").post(registerUser); +// Route to update a user by ID +router.put('/user/update/:id', UpdateUser); router.route("/user/login").post(loginUser);