updated the KYC conroller
This commit is contained in:
parent
457ebcadb9
commit
f216fd1b3c
@ -381,9 +381,8 @@ export const getKycById = async (req, res) => {
|
|||||||
|
|
||||||
export const updateKycStatus = async (req, res) => {
|
export const updateKycStatus = async (req, res) => {
|
||||||
const { status, rejectionReason, user } = req.body;
|
const { status, rejectionReason, user } = req.body;
|
||||||
|
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
// console.log(user, rejectionReason, status);
|
|
||||||
try {
|
try {
|
||||||
// Find the KYC document by ID
|
// Find the KYC document by ID
|
||||||
const kyc = await KYC.findById(id);
|
const kyc = await KYC.findById(id);
|
||||||
@ -391,10 +390,14 @@ export const updateKycStatus = async (req, res) => {
|
|||||||
if (!kyc) {
|
if (!kyc) {
|
||||||
return res.status(404).json({ message: "KYC record not found" });
|
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()) {
|
if (kyc.principal_distributer.toString() !== req.user._id.toString()) {
|
||||||
return res.status(403).json({ message: "Access denied" });
|
return res.status(403).json({ message: "Access denied" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const trade_name = kyc.trade_name;
|
const trade_name = kyc.trade_name;
|
||||||
|
|
||||||
if (status === "approved") {
|
if (status === "approved") {
|
||||||
kyc.status = status;
|
kyc.status = status;
|
||||||
|
|
||||||
@ -408,7 +411,6 @@ export const updateKycStatus = async (req, res) => {
|
|||||||
msg: `Your KYC for ${trade_name} has been approved.`,
|
msg: `Your KYC for ${trade_name} has been approved.`,
|
||||||
kyc_ref: kyc._id,
|
kyc_ref: kyc._id,
|
||||||
added_for: kyc.addedBy,
|
added_for: kyc.addedBy,
|
||||||
// userType: req.userType,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Generate a secure password for RetailDistributor
|
// Generate a secure password for RetailDistributor
|
||||||
@ -428,21 +430,21 @@ export const updateKycStatus = async (req, res) => {
|
|||||||
|
|
||||||
const retailDistributor = new RetailDistributor(retailDistributorData);
|
const retailDistributor = new RetailDistributor(retailDistributorData);
|
||||||
await retailDistributor.save();
|
await retailDistributor.save();
|
||||||
|
|
||||||
// Send email with the new password
|
// Send email with the new password
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
to: `${kyc.email}`, // Change to your recipient
|
to: kyc.email,
|
||||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
from: process.env.SEND_EMAIL_FROM,
|
||||||
subject: `Cheminova Account Created`,
|
subject: `Cheminova Account Created`,
|
||||||
html: `Your Retail Distributor Account is created successfully.
|
html: `Your Retail Distributor Account is created successfully.
|
||||||
<br/>name is: <strong>${kyc.name}</strong>
|
<br/>name is: <strong>${kyc.name}</strong>
|
||||||
<br/>
|
<br/>Mobile Number is: <strong>${kyc.mobile_number}</strong><br/>
|
||||||
<br/>MobileNumber is: <strong>${kyc.mobile_number}</strong><br/>
|
|
||||||
<br/>Email is: <strong>${kyc.email}</strong><br/>
|
<br/>Email is: <strong>${kyc.email}</strong><br/>
|
||||||
<br/>password is: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
|
<br/>password is: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status === "reject") {
|
if (status === "reject") {
|
||||||
console.log("inside reject ");
|
|
||||||
await rejectKYC(
|
await rejectKYC(
|
||||||
kyc.addedBy,
|
kyc.addedBy,
|
||||||
"KYC application Rejected",
|
"KYC application Rejected",
|
||||||
@ -453,17 +455,11 @@ export const updateKycStatus = async (req, res) => {
|
|||||||
msg: `KYC for ${trade_name} has been rejected. Reason: ${rejectionReason}`,
|
msg: `KYC for ${trade_name} has been rejected. Reason: ${rejectionReason}`,
|
||||||
kyc_ref: kyc._id,
|
kyc_ref: kyc._id,
|
||||||
added_for: kyc.addedBy,
|
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") {
|
if (kyc.status === "reject" || status === "reject") {
|
||||||
// kyc.rejection_reason = rejectionReason;
|
|
||||||
if (status) {
|
if (status) {
|
||||||
kyc.status = status;
|
kyc.status = status;
|
||||||
}
|
}
|
||||||
@ -478,7 +474,15 @@ export const updateKycStatus = async (req, res) => {
|
|||||||
// Save the updated KYC document
|
// Save the updated KYC document
|
||||||
await kyc.save();
|
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) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(500).json({ message: "Error updating KYC status", error });
|
res.status(500).json({ message: "Error updating KYC status", error });
|
||||||
|
Loading…
Reference in New Issue
Block a user