mapping RD
BIN
public/temp/tmp-1-1725516898089
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-1-1725517453448
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-1-1725517726185
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-1-1725517759615
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-1-1725518149346
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-1-1725596013284
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-10-1725516924774
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-10-1725517460034
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-11-1725516924777
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-11-1725517460037
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-12-1725516924779
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-12-1725517460048
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-13-1725517152823
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-13-1725517465401
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-14-1725517152825
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-14-1725517465404
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-15-1725517152828
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-15-1725517465407
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-16-1725517152830
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-16-1725517465410
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-17-1725517152833
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-17-1725517465413
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-18-1725517152835
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-18-1725517465415
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-19-1725517187702
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-2-1725516898093
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-2-1725517453451
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-2-1725517726188
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-2-1725517759618
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-2-1725518149349
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-2-1725596013295
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-20-1725517187704
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-21-1725517187706
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-22-1725517187708
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-23-1725517187710
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-24-1725517187711
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-3-1725516898096
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-3-1725517453456
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-3-1725517726191
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-3-1725517759621
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-3-1725518149352
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-3-1725596013305
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-4-1725516898098
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-4-1725517453459
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-4-1725517726194
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-4-1725517759623
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-4-1725518149354
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-4-1725596013322
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-5-1725516898101
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-5-1725517453462
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-5-1725517726199
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-5-1725517759626
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-5-1725518149357
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-5-1725596013329
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-6-1725516898103
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-6-1725517453464
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-6-1725517726201
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-6-1725517759629
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-6-1725518149370
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-6-1725596013343
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-7-1725516924758
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-7-1725517460027
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-8-1725516924759
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-8-1725517460029
Normal file
After Width: | Height: | Size: 197 KiB |
BIN
public/temp/tmp-9-1725516924762
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
public/temp/tmp-9-1725517460031
Normal file
After Width: | Height: | Size: 197 KiB |
@ -110,6 +110,92 @@ export const createKyc = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
export const createretaildistributor = async (req, res) => {
|
||||
const {
|
||||
name,
|
||||
trade_name,
|
||||
address,
|
||||
state,
|
||||
city,
|
||||
district,
|
||||
pincode,
|
||||
mobile_number,
|
||||
pan_number,
|
||||
aadhar_number,
|
||||
gst_number,
|
||||
} = req.body;
|
||||
|
||||
try {
|
||||
if (!mongoose.Types.ObjectId.isValid(req.user._id)) {
|
||||
return res.status(400).json({ message: "Please login again" });
|
||||
}
|
||||
|
||||
if (!req.files) {
|
||||
return res.status(400).json({ message: "No files were uploaded." });
|
||||
}
|
||||
|
||||
const { selfieEntranceImg, panImg, aadharImg, gstImg, pesticideLicenseImg } = req.files;
|
||||
// console.log(req.files);
|
||||
const fertilizerLicenseImg = req.files.fertilizerLicenseImg || null;
|
||||
|
||||
if (!panImg || !aadharImg || !gstImg || !selfieEntranceImg || !pesticideLicenseImg) {
|
||||
return res.status(400).json({ message: "Please upload all required images." });
|
||||
}
|
||||
|
||||
// Upload images to Cloudinary
|
||||
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 pan_img = await uploadImage(panImg, "KYC/pan");
|
||||
const aadhar_img = await uploadImage(aadharImg, "KYC/aadhar");
|
||||
const gst_img = await uploadImage(gstImg, "KYC/gst");
|
||||
const pesticide_license_img = await uploadImage(pesticideLicenseImg, "KYC/pesticide_license");
|
||||
const fertilizer_license_img= await uploadImage(fertilizerLicenseImg, "KYC/fertilizer_license");
|
||||
const selfie_entrance_img = await uploadImage(selfieEntranceImg, "KYC/selfie_entrance");
|
||||
|
||||
// Create KYC document
|
||||
const kycData = {
|
||||
name,
|
||||
trade_name,
|
||||
address,
|
||||
state,
|
||||
city,
|
||||
district,
|
||||
pincode,
|
||||
mobile_number,
|
||||
pan_number,
|
||||
pan_img,
|
||||
aadhar_number,
|
||||
aadhar_img,
|
||||
gst_number,
|
||||
gst_img,
|
||||
pesticide_license_img,
|
||||
fertilizer_license_img: fertilizer_license_img || null, // Optional field
|
||||
selfie_entrance_img,
|
||||
status: "approved",
|
||||
};
|
||||
|
||||
const kyc = await KYC.create(kycData);
|
||||
|
||||
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 {
|
||||
@ -165,40 +251,6 @@ export const getAllKycApproved = async (req, res) => {
|
||||
res.status(500).json({ message: "Server Error", error });
|
||||
}
|
||||
};
|
||||
export const getAllKycApprovedbytmid = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params; // Extracting `addedBy` ID from req.params
|
||||
const { tradename, page = 1, show = 10 } = req.query; // Extracting filters and pagination from req.query
|
||||
|
||||
const query = { status: "approved", addedBy: id }; // Base query with status and addedBy
|
||||
|
||||
if (tradename) {
|
||||
query.trade_name = new RegExp(tradename, "i"); // Adding trade_name filter with case-insensitive regex
|
||||
}
|
||||
|
||||
const skip = (page - 1) * show; // Calculating the number of documents to skip
|
||||
|
||||
// Fetch KYC documents from the database based on query, sorted by creation date, with pagination
|
||||
const retaildistributor = await KYC.find(query)
|
||||
.sort({ createdAt: -1 })
|
||||
.populate("principal_distributer", "name")
|
||||
.populate("addedBy")
|
||||
.skip(skip)
|
||||
.limit(parseInt(show));
|
||||
const total_data = await KYC.countDocuments(query);
|
||||
// Send the fetched data as a response
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
total_data: total_data,
|
||||
total_pages: Math.ceil(total_data / page),
|
||||
retaildistributor
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle any errors that occur during the fetch operation
|
||||
console.error(error);
|
||||
res.status(500).json({ message: "Server Error", error });
|
||||
}
|
||||
};
|
||||
// Get Single KYC
|
||||
export const getKycById = async (req, res) => {
|
||||
try {
|
||||
@ -295,14 +347,26 @@ export const updateKycStatus = async (req, res) => {
|
||||
|
||||
export const getAllPrincipalDistributers = async (req, res) => {
|
||||
try {
|
||||
// Fetch all users with the role "principal-distributer"
|
||||
const principalDistributers = await User.find({
|
||||
role: "principal-Distributor",
|
||||
});
|
||||
// Define the filter for the query
|
||||
let filter = { role: "principal-Distributor" };
|
||||
|
||||
// Check the user type and adjust the filter accordingly
|
||||
if (req.userType === "SalesCoOrdinator") {
|
||||
// If userType is "SalesCoOrdinator", filter by req.user.mappedBy
|
||||
filter.mappedBy = req.user.mappedBy;
|
||||
} else {
|
||||
// Otherwise, filter by req.user._id
|
||||
filter.mappedBy = req.user._id;
|
||||
}
|
||||
|
||||
// Fetch the principal distributors based on the filter
|
||||
const principalDistributers = await User.find(filter);
|
||||
|
||||
// Send the fetched data as a response
|
||||
if (principalDistributers) {
|
||||
if (principalDistributers.length > 0) {
|
||||
res.status(200).json(principalDistributers);
|
||||
} else {
|
||||
res.status(404).json({ message: "No Principal Distributors found" });
|
||||
}
|
||||
} catch (error) {
|
||||
// Handle any errors that occur during the fetch operation
|
||||
@ -310,6 +374,7 @@ export const getAllPrincipalDistributers = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Fcm token storing
|
||||
export const saveFCMTokenForSC = async (req, res) => {
|
||||
const { fcmToken } = req.body;
|
||||
@ -366,3 +431,79 @@ export const saveFCMTokenForTM = async (req, res) => {
|
||||
res.status(500).send("Internal Server Error");
|
||||
}
|
||||
};
|
||||
export const getAllKycApprovedbytmid = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params; // Extracting `addedBy` ID from req.params
|
||||
const { tradename, page = 1, show = 10 } = req.query;
|
||||
|
||||
const query = { status: "approved", mappedTM: id };
|
||||
|
||||
if (tradename) {
|
||||
query.trade_name = new RegExp(tradename, "i");
|
||||
}
|
||||
|
||||
const skip = (page - 1) * show;
|
||||
|
||||
const retaildistributor = await KYC.find(query)
|
||||
.sort({ createdAt: -1 })
|
||||
.populate("principal_distributer")
|
||||
.populate("mappedTM")
|
||||
.populate("mappedSC")
|
||||
.populate("addedBy")
|
||||
.skip(skip)
|
||||
.limit(parseInt(show));
|
||||
const total_data = await KYC.countDocuments(query);
|
||||
// Send the fetched data as a response
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
total_data: total_data,
|
||||
total_pages: Math.ceil(total_data / page),
|
||||
retaildistributor
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle any errors that occur during the fetch operation
|
||||
console.error(error);
|
||||
res.status(500).json({ message: "Server Error", error });
|
||||
}
|
||||
};
|
||||
|
||||
export const updateKYCMapped = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params; // KYC document ID from params
|
||||
const { principal_distributor, mappedTM, mappedSC } = req.body; // Fields to update from the request body
|
||||
|
||||
// Find the KYC document by ID
|
||||
const kyc = await KYC.findById(id);
|
||||
|
||||
// If KYC not found, return 404
|
||||
if (!kyc) {
|
||||
return res.status(404).json({ message: "KYC record not found" });
|
||||
}
|
||||
|
||||
// Update the fields only if they are provided in the request body
|
||||
if (principal_distributor) {
|
||||
kyc.principal_distributer = principal_distributor;
|
||||
}
|
||||
if (mappedTM) {
|
||||
kyc.mappedTM = mappedTM;
|
||||
}
|
||||
if (mappedSC) {
|
||||
kyc.mappedSC = mappedSC;
|
||||
}
|
||||
|
||||
// Save the updated KYC document
|
||||
await kyc.save();
|
||||
|
||||
// Send the updated KYC data as a response
|
||||
res.status(200).json({
|
||||
message: "KYC record updated successfully",
|
||||
data: kyc,
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle any errors during the update
|
||||
res.status(500).json({
|
||||
message: "Error updating KYC record",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
};
|
@ -39,7 +39,7 @@ const KycSchema = new Schema(
|
||||
principal_distributer: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
// required: true,
|
||||
},
|
||||
pan_number: {
|
||||
type: String,
|
||||
@ -95,6 +95,14 @@ const KycSchema = new Schema(
|
||||
// required: true,
|
||||
enum: ["SalesCoOrdinator", "TerritoryManager"],
|
||||
},
|
||||
mappedTM: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "TerritoryManager",
|
||||
},
|
||||
mappedSC: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "SalesCoOrdinator",
|
||||
},
|
||||
notes: [
|
||||
{
|
||||
message: {
|
||||
@ -119,5 +127,13 @@ const KycSchema = new Schema(
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
// Pre-save middleware to set 'mapped' based on 'userType'
|
||||
KycSchema.pre("save", function (next) {
|
||||
if (this.userType === "SalesCoOrdinator" && this.addedBy) {
|
||||
this.mappedSC = this.addedBy;
|
||||
} else if (this.userType === "TerritoryManager" && this.addedBy) {
|
||||
this.mappedTM = this.addedBy;
|
||||
}
|
||||
next();
|
||||
});
|
||||
export const KYC = model("KYC", KycSchema);
|
||||
|
@ -4,6 +4,7 @@ const router = express.Router();
|
||||
|
||||
import {
|
||||
createKyc,
|
||||
createretaildistributor,
|
||||
getAllKyc,
|
||||
getAllKycApproved,
|
||||
getAllKycApprovedbytmid,
|
||||
@ -12,6 +13,7 @@ import {
|
||||
getKycById,
|
||||
saveFCMTokenForSC,
|
||||
saveFCMTokenForTM,
|
||||
updateKYCMapped,
|
||||
updateKycStatus,
|
||||
} from "./KycController.js";
|
||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||
@ -31,9 +33,7 @@ router
|
||||
router
|
||||
.route("/kyc/getAllapproved/")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApproved);
|
||||
router
|
||||
.route("/kyc/getAllapprovedbytmid/:id")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApprovedbytmid);
|
||||
|
||||
router
|
||||
.route("/kyc/get-single-kyc/:id")
|
||||
.get(
|
||||
@ -48,6 +48,9 @@ router.route("/kyc/create/").post(isAuthenticatedSalesCoOrdinator, createKyc);
|
||||
router
|
||||
.route("/kyc/create-tm/")
|
||||
.post(isAuthenticatedTerritoryManager, createKyc);
|
||||
router
|
||||
.route("/kyc/create-admin/")
|
||||
.post(isAuthenticatedUser,authorizeRoles("admin"), createretaildistributor);
|
||||
|
||||
router
|
||||
.route("/kyc/getAllrejected/")
|
||||
@ -75,4 +78,11 @@ router
|
||||
router
|
||||
.route("/kyc/save-fcm-tm/")
|
||||
.post(isAuthenticatedTerritoryManager, saveFCMTokenForTM);
|
||||
//mapping part
|
||||
router
|
||||
.route("/kyc/getAllapprovedbytmid/:id")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApprovedbytmid);
|
||||
router
|
||||
.route("/kyc/mapped/:id")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), updateKYCMapped);
|
||||
export default router;
|
||||
|