added the update api

This commit is contained in:
ROSHAN GARG 2024-08-02 11:04:21 +05:30
parent 7d01e8c255
commit 7521d592a4
3 changed files with 54 additions and 4 deletions

View File

@ -110,14 +110,16 @@ export const createKyc = async (req, res) => {
export const getAllKyc = async (req, res) => {
try {
// Fetch all KYC documents from the database
console.log("req came here ");
const kycs = await KYC.find()
.populate("principal_distributer", "name")
.populate("addedBy");
console.log(kycs);
// Send the fetched data as a response
res.status(200).json(kycs);
} catch (error) {
// Handle any errors that occur during the fetch operation
console.log(error);
res.status(500).json({ message: "Server Error", error });
}
};
@ -145,6 +147,39 @@ export const getKycById = async (req, res) => {
}
};
export const updateKycStatus = async (req, res) => {
const { status, rejectionReason, userType } = req.body;
const { id } = req.params;
try {
// Find the KYC document by ID
const kyc = await KYC.findById(id);
if (!kyc) {
return res.status(404).json({ message: "KYC record not found" });
}
// Update the status
kyc.status = status;
// Add rejection reason to notes if status is reject
if (status === "reject") {
// kyc.rejection_reason = rejectionReason;
kyc.notes.push({
message: rejectionReason,
user: userType,
replyDate: new Date(),
});
}
// Save the updated KYC document
await kyc.save();
res.status(200).json({ message: "KYC status updated successfully" });
} catch (error) {
res.status(500).json({ message: "Error updating KYC status", error });
}
};
export const getAllPrincipalDistributers = async (req, res) => {
try {
// Fetch all users with the role "principal-distributer"

View File

@ -103,8 +103,12 @@ const KycSchema = new Schema(
},
user: {
type: String,
enum: ["Principal Distributer", "Sales Co-ordinator"],
default: "Sales Co-ordinator",
enum: [
"Principal Distributer",
"Sales Co-ordinator",
"Territory Manager",
],
required: true,
},
replyDate: {
type: Date,

View File

@ -7,10 +7,18 @@ import {
getAllKyc,
getAllPrincipalDistributers,
getKycById,
updateKycStatus,
} from "./KycController.js";
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
router.route("/kyc/create/").post(isAuthenticatedSalesCoOrdinator, createKyc);
// Pd routes
router
.route("/kyc/update/:id")
.patch(
isAuthenticatedUser,
authorizeRoles("principal-Distributor"),
updateKycStatus
);
router
.route("/kyc/getAll/")
.get(isAuthenticatedUser, authorizeRoles("principal-Distributor"), getAllKyc);
@ -21,6 +29,9 @@ router
authorizeRoles("principal-Distributor"),
getKycById
);
// sales coordinator routes
router.route("/kyc/create/").post(isAuthenticatedSalesCoOrdinator, createKyc);
router
.route("/kyc/get-pd/")
.get(isAuthenticatedSalesCoOrdinator, getAllPrincipalDistributers);