mapping RD

This commit is contained in:
Sibunnayak 2024-09-06 12:18:26 +05:30
parent 4f2f7d7114
commit 92f2f9d525
69 changed files with 211 additions and 44 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

View File

@ -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 // Get All KYC
export const getAllKyc = async (req, res) => { export const getAllKyc = async (req, res) => {
try { try {
@ -165,40 +251,6 @@ export const getAllKycApproved = async (req, res) => {
res.status(500).json({ message: "Server Error", error }); 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 // Get Single KYC
export const getKycById = async (req, res) => { export const getKycById = async (req, res) => {
try { try {
@ -295,14 +347,26 @@ export const updateKycStatus = async (req, res) => {
export const getAllPrincipalDistributers = async (req, res) => { export const getAllPrincipalDistributers = async (req, res) => {
try { try {
// Fetch all users with the role "principal-distributer" // Define the filter for the query
const principalDistributers = await User.find({ let filter = { role: "principal-Distributor" };
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 // Send the fetched data as a response
if (principalDistributers) { if (principalDistributers.length > 0) {
res.status(200).json(principalDistributers); res.status(200).json(principalDistributers);
} else {
res.status(404).json({ message: "No Principal Distributors found" });
} }
} catch (error) { } catch (error) {
// Handle any errors that occur during the fetch operation // Handle any errors that occur during the fetch operation
@ -310,6 +374,7 @@ export const getAllPrincipalDistributers = async (req, res) => {
} }
}; };
// Fcm token storing // Fcm token storing
export const saveFCMTokenForSC = async (req, res) => { export const saveFCMTokenForSC = async (req, res) => {
const { fcmToken } = req.body; const { fcmToken } = req.body;
@ -366,3 +431,79 @@ export const saveFCMTokenForTM = async (req, res) => {
res.status(500).send("Internal Server Error"); 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,
});
}
};

View File

@ -39,7 +39,7 @@ const KycSchema = new Schema(
principal_distributer: { principal_distributer: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: "User", ref: "User",
required: true, // required: true,
}, },
pan_number: { pan_number: {
type: String, type: String,
@ -95,6 +95,14 @@ const KycSchema = new Schema(
// required: true, // required: true,
enum: ["SalesCoOrdinator", "TerritoryManager"], enum: ["SalesCoOrdinator", "TerritoryManager"],
}, },
mappedTM: {
type: mongoose.Schema.Types.ObjectId,
ref: "TerritoryManager",
},
mappedSC: {
type: mongoose.Schema.Types.ObjectId,
ref: "SalesCoOrdinator",
},
notes: [ notes: [
{ {
message: { message: {
@ -119,5 +127,13 @@ const KycSchema = new Schema(
}, },
{ timestamps: true } { 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); export const KYC = model("KYC", KycSchema);

View File

@ -4,6 +4,7 @@ const router = express.Router();
import { import {
createKyc, createKyc,
createretaildistributor,
getAllKyc, getAllKyc,
getAllKycApproved, getAllKycApproved,
getAllKycApprovedbytmid, getAllKycApprovedbytmid,
@ -12,6 +13,7 @@ import {
getKycById, getKycById,
saveFCMTokenForSC, saveFCMTokenForSC,
saveFCMTokenForTM, saveFCMTokenForTM,
updateKYCMapped,
updateKycStatus, updateKycStatus,
} from "./KycController.js"; } from "./KycController.js";
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js"; import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
@ -31,9 +33,7 @@ router
router router
.route("/kyc/getAllapproved/") .route("/kyc/getAllapproved/")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApproved); .get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApproved);
router
.route("/kyc/getAllapprovedbytmid/:id")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllKycApprovedbytmid);
router router
.route("/kyc/get-single-kyc/:id") .route("/kyc/get-single-kyc/:id")
.get( .get(
@ -48,6 +48,9 @@ router.route("/kyc/create/").post(isAuthenticatedSalesCoOrdinator, createKyc);
router router
.route("/kyc/create-tm/") .route("/kyc/create-tm/")
.post(isAuthenticatedTerritoryManager, createKyc); .post(isAuthenticatedTerritoryManager, createKyc);
router
.route("/kyc/create-admin/")
.post(isAuthenticatedUser,authorizeRoles("admin"), createretaildistributor);
router router
.route("/kyc/getAllrejected/") .route("/kyc/getAllrejected/")
@ -75,4 +78,11 @@ router
router router
.route("/kyc/save-fcm-tm/") .route("/kyc/save-fcm-tm/")
.post(isAuthenticatedTerritoryManager, saveFCMTokenForTM); .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; export default router;