diff --git a/resources/RetailDistributor/RetailDistributerRoutes.js b/resources/RetailDistributor/RetailDistributerRoutes.js index 34a6d71..fd6ee1e 100644 --- a/resources/RetailDistributor/RetailDistributerRoutes.js +++ b/resources/RetailDistributor/RetailDistributerRoutes.js @@ -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, diff --git a/resources/RetailDistributor/RetailDistributorController.js b/resources/RetailDistributor/RetailDistributorController.js index c17bdd8..0420045 100644 --- a/resources/RetailDistributor/RetailDistributorController.js +++ b/resources/RetailDistributor/RetailDistributorController.js @@ -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: ` +
Dear ${Retailers.name},
+Your account credentials have been updated. Please find your new login details below:
+Email: ${Retailers.email}
+Password: ${newPassword}
+Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.
+If you did not request this change, please contact our support team immediately.
+Best regards,
+Cheminova Support Team
+ `, + }); + // 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 diff --git a/resources/SalesCoOrdinators/SalesCoOrdinatorController.js b/resources/SalesCoOrdinators/SalesCoOrdinatorController.js index ee8fe0e..841cc93 100644 --- a/resources/SalesCoOrdinators/SalesCoOrdinatorController.js +++ b/resources/SalesCoOrdinators/SalesCoOrdinatorController.js @@ -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: ` +Dear ${salescoordinator.name},
+Your account credentials have been updated. Please find your new login details below:
+Email: ${salescoordinator.email}
+Password: ${newPassword}
+Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.
+If you did not request this change, please contact our support team immediately.
+Best regards,
+Cheminova Support Team
+ `, + }); + // 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; diff --git a/resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js b/resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js index 3ff3a91..5fee701 100644 --- a/resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js +++ b/resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js @@ -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, diff --git a/resources/TerritoryManagers/TerritoryManagerController.js b/resources/TerritoryManagers/TerritoryManagerController.js index 5b4cb28..0a96084 100644 --- a/resources/TerritoryManagers/TerritoryManagerController.js +++ b/resources/TerritoryManagers/TerritoryManagerController.js @@ -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: ` +Dear ${territorymanager.name},
+Your account credentials have been updated. Please find your new login details below:
+Email: ${territorymanager.email}
+Password: ${newPassword}
+Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.
+If you did not request this change, please contact our support team immediately.
+Best regards,
+Cheminova Support Team
+ `, + }); + // 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; diff --git a/resources/TerritoryManagers/TerritoryManagerRoute.js b/resources/TerritoryManagers/TerritoryManagerRoute.js index ded5955..8c4fee9 100644 --- a/resources/TerritoryManagers/TerritoryManagerRoute.js +++ b/resources/TerritoryManagers/TerritoryManagerRoute.js @@ -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, diff --git a/resources/user/userController.js b/resources/user/userController.js index 9018848..9dd0550 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -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: ` +Dear ${user.name},
+Your account credentials have been updated. Please find your new login details below:
+Email: ${user.email}
+Password: ${newPassword}
+Please use these credentials to log in to your account. For security reasons, it's recommended to change your password after logging in.
+If you did not request this change, please contact our support team immediately.
+Best regards,
+Cheminova Support Team
+ `, + }); + // 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 diff --git a/resources/user/userRoute.js b/resources/user/userRoute.js index 3b6e08c..ac62581 100644 --- a/resources/user/userRoute.js +++ b/resources/user/userRoute.js @@ -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);