From 02f7e34fc6601f44d7c669dd0bc2fbfd5dc12ca9 Mon Sep 17 00:00:00 2001 From: ROSHAN GARG Date: Wed, 31 Jul 2024 14:25:14 +0530 Subject: [PATCH] created apis for get kyc , get pd --- resources/KYC/KycController.js | 56 ++++++++++++++++++++++++++++++++++ resources/KYC/KycRoutes.js | 21 ++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/resources/KYC/KycController.js b/resources/KYC/KycController.js index 6ea7045..324e300 100644 --- a/resources/KYC/KycController.js +++ b/resources/KYC/KycController.js @@ -1,6 +1,7 @@ import mongoose from "mongoose"; import cloudinary from "../../Utils/cloudinary.js"; import { KYC } from "./KycModel.js"; +import User from "../user/userModel.js"; export const createKyc = async (req, res) => { const { @@ -104,3 +105,58 @@ export const createKyc = async (req, res) => { }); } }; +// Get All KYC +export const getAllKyc = async (req, res) => { + try { + // Fetch all KYC documents from the database + const kycs = await KYC.find() + .populate("principal_distributer", "name") + .populate("addedBy"); + + // Send the fetched data as a response + res.status(200).json(kycs); + } catch (error) { + // Handle any errors that occur during the fetch operation + res.status(500).json({ message: "Server Error", error }); + } +}; +// Get Single KYC +export const getKycById = async (req, res) => { + try { + // Get the KYC ID from the request parameters + const { id } = req.params; + + // Fetch the KYC document from the database by ID + const kyc = await KYC.findById(id) + .populate("principal_distributer", "name") + .populate("addedBy"); + + // Check if the KYC document exists + if (!kyc) { + return res.status(404).json({ message: "KYC document not found" }); + } + + // Send the fetched KYC document as a response + res.status(200).json(kyc); + } catch (error) { + // Handle any errors that occur during the fetch operation + res.status(500).json({ message: "Server Error", error }); + } +}; + +export const getAllPrincipalDistributers = async (req, res) => { + try { + // Fetch all users with the role "principal-distributer" + const principalDistributers = await User.find({ + role: "principal-Distributor", + }); + + // Send the fetched data as a response + if (principalDistributers) { + res.status(200).json(principalDistributers); + } + } catch (error) { + // Handle any errors that occur during the fetch operation + res.status(500).json({ message: "Server Error", error }); + } +}; diff --git a/resources/KYC/KycRoutes.js b/resources/KYC/KycRoutes.js index 48dd38c..6e859c2 100644 --- a/resources/KYC/KycRoutes.js +++ b/resources/KYC/KycRoutes.js @@ -2,8 +2,27 @@ import express from "express"; const router = express.Router(); -import { createKyc } from "./KycController.js"; +import { + createKyc, + getAllKyc, + getAllPrincipalDistributers, + getKycById, +} from "./KycController.js"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; +import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; router.route("/kyc/create/").post(isAuthenticatedSalesCoOrdinator, createKyc); +router + .route("/kyc/getAll/") + .get(isAuthenticatedUser, authorizeRoles("principal-Distributor"), getAllKyc); +router + .route("/kyc/get-single-kyc/:id") + .get( + isAuthenticatedUser, + authorizeRoles("principal-Distributor"), + getKycById + ); +router + .route("/kyc/get-pd/") + .get(isAuthenticatedSalesCoOrdinator, getAllPrincipalDistributers); export default router;