Retailers,PD,TM,SC resetpassword by admin

This commit is contained in:
Sibunnayak 2024-11-20 09:48:10 +05:30
parent 45ef34c3de
commit 8dec31d038
8 changed files with 280 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import {
uploadRetaildistributors, uploadRetaildistributors,
updateretaildistributorwithKYC, updateretaildistributorwithKYC,
generateRetailerReport, generateRetailerReport,
ResetPassword,
} from "./RetailDistributorController.js"; } from "./RetailDistributorController.js";
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js"; import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
@ -32,6 +33,12 @@ router
router.route("/rd-login").post(loginRD); router.route("/rd-login").post(loginRD);
router.route("/rd-get-me").get(isAuthenticatedRD, getmyProfileRD); router.route("/rd-get-me").get(isAuthenticatedRD, getmyProfileRD);
router.post("/forgot-password", forgotPasswordRD); router.post("/forgot-password", forgotPasswordRD);
router.put(
"/rd/reset-password/:id",
isAuthenticatedUser,
authorizeRoles("admin"),
ResetPassword
);
router.put( router.put(
"/rd-password/update", "/rd-password/update",
isAuthenticatedRD, isAuthenticatedRD,

View File

@ -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) => { export const UpdateProfileRD = async (req, res) => {
const { name, mobile_number } = req.body; // Only expecting name from the request body const { name, mobile_number } = req.body; // Only expecting name from the request body

View File

@ -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 //Update mobile Number
export const updateMobileNumber = async (req, res) => { export const updateMobileNumber = async (req, res) => {
const { id } = req.params; const { id } = req.params;

View File

@ -21,6 +21,7 @@ import {
unmapSalesCoOrdinator, unmapSalesCoOrdinator,
getAllSalesCoOrdinatorforTM_App, getAllSalesCoOrdinatorforTM_App,
uploadSalesCoordinators, uploadSalesCoordinators,
ResetPassword,
} from "./SalesCoOrdinatorController.js"; } from "./SalesCoOrdinatorController.js";
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
@ -97,6 +98,12 @@ router.post(
verifyUpdatedMobileOtp verifyUpdatedMobileOtp
); );
router.post("/forgot-password", forgotPassword); router.post("/forgot-password", forgotPassword);
router.put(
"/reset-password/:id",
isAuthenticatedUser,
authorizeRoles("admin"),
ResetPassword
);
router.patch( router.patch(
"/profile/update/:id", "/profile/update/:id",
isAuthenticatedUser, isAuthenticatedUser,

View File

@ -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 //Update mobile Number
export const updateMobileNumber = async (req, res) => { export const updateMobileNumber = async (req, res) => {
const { id } = req.params; const { id } = req.params;

View File

@ -18,6 +18,7 @@ import {
logout, logout,
uploadTerritoryManagers, uploadTerritoryManagers,
getAllTerritoryManagerdropdown, getAllTerritoryManagerdropdown,
ResetPassword,
} from "./TerritoryManagerController.js"; } from "./TerritoryManagerController.js";
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js"; import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
@ -74,6 +75,12 @@ router.post(
verifyUpdatedMobileOtp verifyUpdatedMobileOtp
); );
router.post("/forgot-password", forgotPassword); router.post("/forgot-password", forgotPassword);
router.put(
"/reset-password/:id",
isAuthenticatedUser,
authorizeRoles("admin"),
ResetPassword
);
router.patch( router.patch(
"/profile/update/:id", "/profile/update/:id",
isAuthenticatedUser, isAuthenticatedUser,

View File

@ -560,7 +560,70 @@ export const forgotPassword = async (req, res, next) => {
.json({ message: "Something went wrong!", error: error?.message || "" }); .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 // 5.Reset Password
export const resetPassword = catchAsyncErrors(async (req, res, next) => { export const resetPassword = catchAsyncErrors(async (req, res, next) => {
// creating token hash // creating token hash

View File

@ -24,6 +24,7 @@ import {
saveFCMTokenForUser, saveFCMTokenForUser,
getAllPD, getAllPD,
generatePrincipalDistributorReport, generatePrincipalDistributorReport,
ResetPasswordAdmin,
} from "./userController.js"; } from "./userController.js";
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.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/login").post(loginUser);
router.route("/user/password/forgot").post(forgotPassword); 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); router.route("/user/password/reset/:token").put(resetPassword);