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 { name, trade_name, address, state, city, district, pincode, mobile_number, principal_distributer, pan_number, aadhar_number, gst_number, notes, } = req.body; const { selfie_entrance_img, pan_img, aadhar_img, gst_img, pesticide_license_img, } = req.files; let fertilizer_license_img; if (req.files && req.files.fertilizer_license_img) { fertilizer_license_img = req.files.fertilizer_license_img; } if (!req?.user) return res.status(400).json({ message: "Please login!" }); const userType = req.userType; try { if (!mongoose.Types.ObjectId.isValid(req.user._id)) { return res.status(400).json({ message: "Please login again" }); } // Upload images to Cloudinary and store only public_id and url const uploadImage = async (image, folder) => { if (!image) return null; const result = await cloudinary.v2.uploader.upload(image.tempFilePath, { folder, }); return { public_id: result.public_id, url: result.secure_url, }; }; const panImg = await uploadImage(pan_img, "KYC/pan"); const aadharImg = await uploadImage(aadhar_img, "KYC/aadhar"); const gstImg = await uploadImage(gst_img, "KYC/gst"); const pesticideLicenseImg = await uploadImage( pesticide_license_img, "KYC/pesticide_license" ); const fertilizerLicenseImg = await uploadImage( fertilizer_license_img, "KYC/fertilizer_license" ); const selfieEntranceImg = await uploadImage( selfie_entrance_img, "KYC/selfie_entrance" ); // Create KYC document const kyc = await KYC.create({ name, trade_name, address, state, city, district, pincode, mobile_number, principal_distributer, pan_number, pan_img: panImg, aadhar_number, aadhar_img: aadharImg, gst_number, gst_img: gstImg, pesticide_license_img: pesticideLicenseImg, fertilizer_license_img: fertilizerLicenseImg || {}, selfie_entrance_img: selfieEntranceImg, addedBy: req.user._id, userType: userType, notes, }); if (kyc) { return res .status(201) .json({ success: true, kyc, message: "KYC created" }); } } catch (error) { res.status(500).json({ success: false, message: error.message ? error.message : "Something went wrong", }); } }; // Get All KYC 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 }); } }; // Get all KYC Rejected export const getAllKycRejected = async (req, res) => { try { // Fetch all KYC documents from the database // console.log("req came here "); const kycs = await KYC.find({ status: "reject", addedBy: req.user._id }) .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 }); } }; // Get All KYC Approved export const getAllKycApproved = async (req, res) => { try { // Fetch all KYC documents from the database // console.log("req came here "); const kycs = await KYC.find({ status: "approved" }) .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 }); } }; // 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 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); if (!kyc) { return res.status(404).json({ message: "KYC record not found" }); } // Update the status if (status) { kyc.status = status; } // Add rejection reason to notes if status is reject if (kyc.status === "reject" || status === "reject") { // kyc.rejection_reason = rejectionReason; kyc.notes.push({ message: rejectionReason, user: user, replyDate: new Date(), }); } // Save the updated KYC document await kyc.save(); res.status(200).json({ message: "KYC status updated successfully", kyc }); } catch (error) { console.log(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" 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 }); } };