This commit is contained in:
Sibunnayak 2025-01-11 16:37:13 +05:30
parent cc70e28ab6
commit 3b96d39973
2 changed files with 72 additions and 0 deletions

View File

@ -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: `
<p>Dear ${name || "User"},</p>
<p>We are pleased to inform you that your Principal Distributor account has been successfully updated. Please find your account details below:</p>
<p><strong>Name:</strong> ${name || "N/A"}</p>
<p><strong>Mobile Number:</strong> ${phone || "N/A"}</p>
<p><strong>Email:</strong> ${email || "N/A"}</p>
<br/>
<p>Your password remains the same as before.</p>
<p>You can log in to your account using the following link:</p>
<p><a href="https://pd.cnapp.co.in/#/login" target="_blank" style="color: #0066cc; text-decoration: none;">Click here to log in</a></p>
<br/>
<p>For convenience, you can also download our mobile app from the following link:</p>
<ul>
<li><a href="https://drive.google.com/file/d/1GOpyyEwfbE0cMzvHHx0Fr7UOFzrQVIlp/view?usp=sharing" target="_blank" style="color: #0066cc; text-decoration: none;">Download Mobile Application</a></li>
</ul>
<br/>
<p>If you did not request this update or have any concerns, please contact our support team immediately.</p>
<br/>
<p>Best regards,</p>
<p><strong>Cheminova Support Team</strong></p>
`,
});
}
// 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;

View File

@ -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);