salescoordinator password change update
This commit is contained in:
parent
f8ea26a441
commit
881549f390
@ -39,8 +39,8 @@ export const register = async (req, res) => {
|
|||||||
otpExpires,
|
otpExpires,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Generate uniqueId if not already present
|
// Generate uniqueId if not already present
|
||||||
if (!salesCoordinator.uniqueId) {
|
if (!salesCoordinator.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();
|
||||||
salesCoordinator.uniqueId = `${currentYear}-${randomChars}`;
|
salesCoordinator.uniqueId = `${currentYear}-${randomChars}`;
|
||||||
@ -507,7 +507,7 @@ export const ChangePassword = async (req, res) => {
|
|||||||
const { id } = req.params; // Retrieve id from req.params
|
const { id } = req.params; // Retrieve id from req.params
|
||||||
const { oldPassword, newPassword, confirmPassword } = req.body;
|
const { oldPassword, newPassword, confirmPassword } = req.body;
|
||||||
const userId = id || req.user._id; // Use the ID from the URL or from the authenticated user
|
const userId = id || req.user._id; // Use the ID from the URL or from the authenticated user
|
||||||
|
// console.log(userId);
|
||||||
if (!oldPassword) {
|
if (!oldPassword) {
|
||||||
return res.status(400).json({ message: "Please Enter Old password" });
|
return res.status(400).json({ message: "Please Enter Old password" });
|
||||||
}
|
}
|
||||||
@ -518,26 +518,34 @@ export const ChangePassword = async (req, res) => {
|
|||||||
return res.status(400).json({ message: "Please Enter Confirm Password" });
|
return res.status(400).json({ message: "Please Enter Confirm Password" });
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const SalesCoOrdinator = await SalesCoOrdinator.findById(userId).select("+password");
|
const Salescoordinator = await SalesCoOrdinator.findById(userId).select(
|
||||||
|
"+password"
|
||||||
|
);
|
||||||
|
|
||||||
if (!SalesCoOrdinator) {
|
if (!Salescoordinator) {
|
||||||
return res.status(404).json({ message: "SalesCoOrdinator not found" });
|
return res.status(404).json({ message: "SalesCoOrdinator not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const isPasswordMatched = await SalesCoOrdinator.comparePassword(oldPassword);
|
const isPasswordMatched = await Salescoordinator.comparePassword(
|
||||||
|
oldPassword
|
||||||
|
);
|
||||||
|
|
||||||
if (!isPasswordMatched) {
|
if (!isPasswordMatched) {
|
||||||
return res.status(400).json({ message: "Old password is incorrect" });
|
return res.status(400).json({ message: "Old password is incorrect" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newPassword !== confirmPassword) {
|
if (newPassword !== confirmPassword) {
|
||||||
return res.status(400).json({ message: "New password and confirm Password do not match" });
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "New password and confirm Password do not match" });
|
||||||
}
|
}
|
||||||
|
|
||||||
SalesCoOrdinator.password = newPassword;
|
Salescoordinator.password = newPassword;
|
||||||
await SalesCoOrdinator.save();
|
await Salescoordinator.save();
|
||||||
|
|
||||||
return res.status(200).json({ success: true, message: "Password updated successfully" });
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, message: "Password updated successfully" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error updating password:", error);
|
console.error("Error updating password:", error);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
|
Loading…
Reference in New Issue
Block a user