Retailers,PD,TM,SC resetpassword by admin
This commit is contained in:
parent
45ef34c3de
commit
8dec31d038
@ -17,6 +17,7 @@ import {
|
||||
uploadRetaildistributors,
|
||||
updateretaildistributorwithKYC,
|
||||
generateRetailerReport,
|
||||
ResetPassword,
|
||||
} from "./RetailDistributorController.js";
|
||||
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||
@ -32,6 +33,12 @@ router
|
||||
router.route("/rd-login").post(loginRD);
|
||||
router.route("/rd-get-me").get(isAuthenticatedRD, getmyProfileRD);
|
||||
router.post("/forgot-password", forgotPasswordRD);
|
||||
router.put(
|
||||
"/rd/reset-password/:id",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
ResetPassword
|
||||
);
|
||||
router.put(
|
||||
"/rd-password/update",
|
||||
isAuthenticatedRD,
|
||||
|
@ -588,7 +588,70 @@ export const forgotPasswordRD = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
export const ResetPassword = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
// console.log(id);
|
||||
if (!id) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Invalid request. ID is required." });
|
||||
}
|
||||
|
||||
try {
|
||||
const Retailers = await RetailDistributor.findById(id);
|
||||
|
||||
if (!Retailers) {
|
||||
return res.status(404).json({ message: "Retailers not found" });
|
||||
}
|
||||
|
||||
// Generate a new random password
|
||||
const newPassword = password.randomPassword({
|
||||
length: 12,
|
||||
characters: [
|
||||
{ characters: password.upper, exactly: 1 },
|
||||
{ characters: password.symbols, exactly: 1 },
|
||||
password.lower,
|
||||
password.digits,
|
||||
],
|
||||
});
|
||||
// console.log(newPassword);
|
||||
// Update the Retailer's password
|
||||
Retailers.password = newPassword;
|
||||
await Retailers.save();
|
||||
|
||||
// Send email with the new credentials
|
||||
await sendEmail({
|
||||
to: `${Retailers.email}`, // Recipient email
|
||||
from: `${process.env.SEND_EMAIL_FROM}`, // Sender email
|
||||
subject: "Cheminova Account Credentials",
|
||||
html: `
|
||||
<p>Dear ${Retailers.name},</p>
|
||||
<p>Your account credentials have been updated. Please find your new login details below:</p>
|
||||
<p><strong>Email:</strong> ${Retailers.email}</p>
|
||||
<p><strong>Password:</strong> ${newPassword}</p>
|
||||
<p>Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.</p>
|
||||
<br/>
|
||||
<p>If you did not request this change, please contact our support team immediately.</p>
|
||||
<br/>
|
||||
<p>Best regards,</p>
|
||||
<p>Cheminova Support Team</p>
|
||||
`,
|
||||
});
|
||||
// console.log(Retailers);
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: `Account credentials sent to ${Retailers.email} successfully.`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error resetting password:", error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message:
|
||||
error.message || "Something went wrong while resetting the password.",
|
||||
});
|
||||
}
|
||||
};
|
||||
export const UpdateProfileRD = async (req, res) => {
|
||||
const { name, mobile_number } = req.body; // Only expecting name from the request body
|
||||
|
||||
|
@ -714,7 +714,70 @@ export const forgotPassword = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
export const ResetPassword = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
// console.log(id);
|
||||
if (!id) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Invalid request. ID is required." });
|
||||
}
|
||||
|
||||
try {
|
||||
const salescoordinator = await SalesCoOrdinator.findById(id);
|
||||
|
||||
if (!salescoordinator) {
|
||||
return res.status(404).json({ message: "Sales Coordinator not found" });
|
||||
}
|
||||
|
||||
// Generate a new random password
|
||||
const newPassword = password.randomPassword({
|
||||
length: 12,
|
||||
characters: [
|
||||
{ characters: password.upper, exactly: 1 },
|
||||
{ characters: password.symbols, exactly: 1 },
|
||||
password.lower,
|
||||
password.digits,
|
||||
],
|
||||
});
|
||||
// console.log(newPassword);
|
||||
// Update the Sales Coordinator's password
|
||||
salescoordinator.password = newPassword;
|
||||
await salescoordinator.save();
|
||||
|
||||
// Send email with the new credentials
|
||||
await sendEmail({
|
||||
to: `${salescoordinator.email}`, // Recipient email
|
||||
from: `${process.env.SEND_EMAIL_FROM}`, // Sender email
|
||||
subject: "Cheminova Account Credentials",
|
||||
html: `
|
||||
<p>Dear ${salescoordinator.name},</p>
|
||||
<p>Your account credentials have been updated. Please find your new login details below:</p>
|
||||
<p><strong>Email:</strong> ${salescoordinator.email}</p>
|
||||
<p><strong>Password:</strong> ${newPassword}</p>
|
||||
<p>Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.</p>
|
||||
<br/>
|
||||
<p>If you did not request this change, please contact our support team immediately.</p>
|
||||
<br/>
|
||||
<p>Best regards,</p>
|
||||
<p>Cheminova Support Team</p>
|
||||
`,
|
||||
});
|
||||
// console.log(salescoordinator);
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: `Account credentials sent to ${salescoordinator.email} successfully.`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error resetting password:", error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message:
|
||||
error.message || "Something went wrong while resetting the password.",
|
||||
});
|
||||
}
|
||||
};
|
||||
//Update mobile Number
|
||||
export const updateMobileNumber = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
@ -21,6 +21,7 @@ import {
|
||||
unmapSalesCoOrdinator,
|
||||
getAllSalesCoOrdinatorforTM_App,
|
||||
uploadSalesCoordinators,
|
||||
ResetPassword,
|
||||
} from "./SalesCoOrdinatorController.js";
|
||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
||||
@ -97,6 +98,12 @@ router.post(
|
||||
verifyUpdatedMobileOtp
|
||||
);
|
||||
router.post("/forgot-password", forgotPassword);
|
||||
router.put(
|
||||
"/reset-password/:id",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
ResetPassword
|
||||
);
|
||||
router.patch(
|
||||
"/profile/update/:id",
|
||||
isAuthenticatedUser,
|
||||
|
@ -566,7 +566,70 @@ export const forgotPassword = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
export const ResetPassword = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
// console.log(id);
|
||||
if (!id) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Invalid request. ID is required." });
|
||||
}
|
||||
|
||||
try {
|
||||
const territorymanager = await TerritoryManager.findById(id);
|
||||
|
||||
if (!territorymanager) {
|
||||
return res.status(404).json({ message: "Territory Manager not found" });
|
||||
}
|
||||
|
||||
// Generate a new random password
|
||||
const newPassword = password.randomPassword({
|
||||
length: 12,
|
||||
characters: [
|
||||
{ characters: password.upper, exactly: 1 },
|
||||
{ characters: password.symbols, exactly: 1 },
|
||||
password.lower,
|
||||
password.digits,
|
||||
],
|
||||
});
|
||||
// console.log(newPassword);
|
||||
// Update the territory manager's password
|
||||
territorymanager.password = newPassword;
|
||||
await territorymanager.save();
|
||||
|
||||
// Send email with the new credentials
|
||||
await sendEmail({
|
||||
to: `${territorymanager.email}`, // Recipient email
|
||||
from: `${process.env.SEND_EMAIL_FROM}`, // Sender email
|
||||
subject: "Cheminova Account Credentials",
|
||||
html: `
|
||||
<p>Dear ${territorymanager.name},</p>
|
||||
<p>Your account credentials have been updated. Please find your new login details below:</p>
|
||||
<p><strong>Email:</strong> ${territorymanager.email}</p>
|
||||
<p><strong>Password:</strong> ${newPassword}</p>
|
||||
<p>Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.</p>
|
||||
<br/>
|
||||
<p>If you did not request this change, please contact our support team immediately.</p>
|
||||
<br/>
|
||||
<p>Best regards,</p>
|
||||
<p>Cheminova Support Team</p>
|
||||
`,
|
||||
});
|
||||
// console.log(territorymanager);
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: `Account credentials sent to ${territorymanager.email} successfully.`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error resetting password:", error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message:
|
||||
error.message || "Something went wrong while resetting the password.",
|
||||
});
|
||||
}
|
||||
};
|
||||
//Update mobile Number
|
||||
export const updateMobileNumber = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
@ -18,6 +18,7 @@ import {
|
||||
logout,
|
||||
uploadTerritoryManagers,
|
||||
getAllTerritoryManagerdropdown,
|
||||
ResetPassword,
|
||||
} from "./TerritoryManagerController.js";
|
||||
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||
@ -74,6 +75,12 @@ router.post(
|
||||
verifyUpdatedMobileOtp
|
||||
);
|
||||
router.post("/forgot-password", forgotPassword);
|
||||
router.put(
|
||||
"/reset-password/:id",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
ResetPassword
|
||||
);
|
||||
router.patch(
|
||||
"/profile/update/:id",
|
||||
isAuthenticatedUser,
|
||||
|
@ -560,7 +560,70 @@ export const forgotPassword = async (req, res, next) => {
|
||||
.json({ message: "Something went wrong!", error: error?.message || "" });
|
||||
}
|
||||
};
|
||||
export const ResetPasswordAdmin = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
// console.log(id);
|
||||
if (!id) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Invalid request. ID is required." });
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await User.findById(id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({ message: "Principal Distributor not found" });
|
||||
}
|
||||
|
||||
// Generate a new random password
|
||||
const newPassword = password.randomPassword({
|
||||
length: 12,
|
||||
characters: [
|
||||
{ characters: password.upper, exactly: 1 },
|
||||
{ characters: password.symbols, exactly: 1 },
|
||||
password.lower,
|
||||
password.digits,
|
||||
],
|
||||
});
|
||||
// console.log(newPassword);
|
||||
// Update the Principal Distributor's password
|
||||
user.password = newPassword;
|
||||
await user.save();
|
||||
|
||||
// Send email with the new credentials
|
||||
await sendEmail({
|
||||
to: `${user.email}`, // Recipient email
|
||||
from: `${process.env.SEND_EMAIL_FROM}`, // Sender email
|
||||
subject: "Cheminova Account Credentials",
|
||||
html: `
|
||||
<p>Dear ${user.name},</p>
|
||||
<p>Your account credentials have been updated. Please find your new login details below:</p>
|
||||
<p><strong>Email:</strong> ${user.email}</p>
|
||||
<p><strong>Password:</strong> ${newPassword}</p>
|
||||
<p>Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.</p>
|
||||
<br/>
|
||||
<p>If you did not request this change, please contact our support team immediately.</p>
|
||||
<br/>
|
||||
<p>Best regards,</p>
|
||||
<p>Cheminova Support Team</p>
|
||||
`,
|
||||
});
|
||||
// console.log(user);
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: `Account credentials sent to ${user.email} successfully.`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error resetting password:", error);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message:
|
||||
error.message || "Something went wrong while resetting the password.",
|
||||
});
|
||||
}
|
||||
};
|
||||
// 5.Reset Password
|
||||
export const resetPassword = catchAsyncErrors(async (req, res, next) => {
|
||||
// creating token hash
|
||||
|
@ -24,6 +24,7 @@ import {
|
||||
saveFCMTokenForUser,
|
||||
getAllPD,
|
||||
generatePrincipalDistributorReport,
|
||||
ResetPasswordAdmin,
|
||||
} from "./userController.js";
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
|
||||
@ -34,6 +35,12 @@ router.route("/user/register").post(registerUser);
|
||||
router.route("/user/login").post(loginUser);
|
||||
|
||||
router.route("/user/password/forgot").post(forgotPassword);
|
||||
router.put(
|
||||
"/user/reset-password/:id",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
ResetPasswordAdmin
|
||||
);
|
||||
|
||||
router.route("/user/password/reset/:token").put(resetPassword);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user