diff --git a/resources/KYC/KycController.js b/resources/KYC/KycController.js
index f02ea0c..a6d2878 100644
--- a/resources/KYC/KycController.js
+++ b/resources/KYC/KycController.js
@@ -381,9 +381,8 @@ export const getKycById = async (req, res) => {
export const updateKycStatus = async (req, res) => {
const { status, rejectionReason, user } = req.body;
-
const { id } = req.params;
- // console.log(user, rejectionReason, status);
+
try {
// Find the KYC document by ID
const kyc = await KYC.findById(id);
@@ -391,10 +390,14 @@ export const updateKycStatus = async (req, res) => {
if (!kyc) {
return res.status(404).json({ message: "KYC record not found" });
}
+
+ // Check if the user has access
if (kyc.principal_distributer.toString() !== req.user._id.toString()) {
return res.status(403).json({ message: "Access denied" });
}
+
const trade_name = kyc.trade_name;
+
if (status === "approved") {
kyc.status = status;
@@ -408,7 +411,6 @@ export const updateKycStatus = async (req, res) => {
msg: `Your KYC for ${trade_name} has been approved.`,
kyc_ref: kyc._id,
added_for: kyc.addedBy,
- // userType: req.userType,
});
// Generate a secure password for RetailDistributor
@@ -428,21 +430,21 @@ export const updateKycStatus = async (req, res) => {
const retailDistributor = new RetailDistributor(retailDistributorData);
await retailDistributor.save();
+
// Send email with the new password
await sendEmail({
- to: `${kyc.email}`, // Change to your recipient
- from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
+ to: kyc.email,
+ from: process.env.SEND_EMAIL_FROM,
subject: `Cheminova Account Created`,
html: `Your Retail Distributor Account is created successfully.
name is: ${kyc.name}
-
-
MobileNumber is: ${kyc.mobile_number}
+
Mobile Number is: ${kyc.mobile_number}
Email is: ${kyc.email}
password is: ${password}
If you have not requested this email, please ignore it.`,
});
}
+
if (status === "reject") {
- console.log("inside reject ");
await rejectKYC(
kyc.addedBy,
"KYC application Rejected",
@@ -453,17 +455,11 @@ export const updateKycStatus = async (req, res) => {
msg: `KYC for ${trade_name} has been rejected. Reason: ${rejectionReason}`,
kyc_ref: kyc._id,
added_for: kyc.addedBy,
- // userType: req.userType,
});
}
- // Update the status
- // if (status) {
- // kyc.status = status;
- // }
- // Add rejection reason to notes if status is reject
+ // Update rejection reason in notes if status is rejected
if (kyc.status === "reject" || status === "reject") {
- // kyc.rejection_reason = rejectionReason;
if (status) {
kyc.status = status;
}
@@ -478,7 +474,15 @@ export const updateKycStatus = async (req, res) => {
// Save the updated KYC document
await kyc.save();
- res.status(200).json({ message: "KYC status updated successfully", kyc });
+ // Populate principal_distributer and addedBy fields
+ const updatedKyc = await KYC.findById(kyc._id)
+ .populate("principal_distributer", "name") // Populate specific fields if needed
+ .populate("addedBy");
+
+ // Send the populated KYC in the response
+ res
+ .status(200)
+ .json({ message: "KYC status updated successfully", kyc: updatedKyc });
} catch (error) {
console.log(error);
res.status(500).json({ message: "Error updating KYC status", error });