mapping RD/PD

This commit is contained in:
Sibunnayak 2024-09-06 16:22:23 +05:30
parent 154f416c7a
commit fe1648f856
3 changed files with 125 additions and 49 deletions

View File

@ -44,14 +44,25 @@ export const getDistributors = async (req, res) => {
if (!["PrincipalDistributor", "RetailDistributor"].includes(type)) {
return res.status(400).json({ message: "Invalid distributor type" });
}
let filter = { role: "principal-Distributor" };
let query={status: "approved"};
// 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;
query.mappedSC = req.user._id;
} else {
// Otherwise, filter by req.user._id
filter.mappedby = req.user._id;
query.mappedTM = req.user._id;
}
console.log("filter",filter);
console.log("query",query);
let distributors;
// console.log("type",type);
if (type === "PrincipalDistributor") {
// Fetch all PrincipalDistributors
const principalDistributors = await User.find({
role: "principal-Distributor",
});
const principalDistributors = await User.find(filter);
// console.log("principalDistributors",principalDistributors);
// Map each PrincipalDistributor to include their ShippingAddress
distributors = await Promise.all(
@ -67,7 +78,7 @@ export const getDistributors = async (req, res) => {
);
} else {
// For RetailDistributor, fetch approved KYC documents
distributors = await KYC.find({ status: "approved" });
distributors = await KYC.find(query);
}
res.status(200).json(distributors);

View File

@ -261,21 +261,58 @@ export const getAllKycRejected = async (req, res) => {
// 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" })
// Extract query parameters from the request
const { page = 1, show = 10, name, principaldistributor } = req.query;
const skip = (page - 1) * show;
// Build the main query object
const query = { status: "approved" };
// If a trade name is provided, add it to the query
if (name) {
query.trade_name = new RegExp(name, "i"); // Case-insensitive search for trade name
}
// If a principal distributor name is provided, find the matching distributor IDs
let principalDistributerIds = [];
if (principaldistributor) {
const matchingDistributors = await mongoose.model('User').find({
name: new RegExp(principaldistributor, "i") // Case-insensitive search for principal distributor name
}, '_id'); // Only return the _id field
principalDistributerIds = matchingDistributors.map(distributor => distributor._id);
// If matching distributors are found, add the IDs to the main query
if (principalDistributerIds.length > 0) {
query.principal_distributer = { $in: principalDistributerIds };
}
}
// Find the KYC records with pagination and populate specific fields
const kycs = await KYC.find(query)
.sort({ createdAt: -1 })
.populate("principal_distributer", "name")
.populate("addedBy");
// console.log(kycs);
// Send the fetched data as a response
res.status(200).json(kycs);
.populate("principal_distributer", "name") // Only include the 'name' field
.populate("addedBy")
.skip(skip)
.limit(parseInt(show));
// Get total count of documents that match the query
const total_data = await KYC.countDocuments(query);
// Send the response with pagination data
res.status(200).json({
success: true,
total_data,
total_pages: Math.ceil(total_data / show),
kycs,
});
} catch (error) {
// Handle any errors that occur during the fetch operation
console.log(error);
console.error(error);
res.status(500).json({ message: "Server Error", error });
}
};
// Get Single KYC
export const getKycById = async (req, res) => {
try {
@ -388,10 +425,10 @@ export const getAllPrincipalDistributers = async (req, res) => {
// Otherwise, filter by req.user._id
filter.mappedby = req.user._id;
}
console.log(filter);
// console.log(filter);
// Fetch the principal distributors based on the filter
const principalDistributers = await User.find(filter);
console.log(principalDistributers);
// console.log(principalDistributers);
// Send the fetched data as a response
if (principalDistributers.length > 0) {
res.status(200).json(principalDistributers);
@ -462,17 +499,19 @@ export const saveFCMTokenForTM = async (req, res) => {
};
export const getAllKycApprovedbytmid = async (req, res) => {
try {
const { id } = req.params; // Extracting `addedBy` ID from req.params
const { id } = req.params; // Extracting `mappedTM` ID from req.params
// Extract query parameters with default values
const { tradename, page = 1, show = 10 } = req.query;
const skip = (page - 1) * show;
// Build query object
const query = { status: "approved", mappedTM: id };
if (tradename) {
query.trade_name = new RegExp(tradename, "i");
}
const skip = (page - 1) * show;
// Fetch KYC records with pagination and population
const retaildistributor = await KYC.find(query)
.sort({ createdAt: -1 })
.populate("principal_distributer")
@ -481,12 +520,18 @@ export const getAllKycApprovedbytmid = async (req, res) => {
.populate("addedBy")
.skip(skip)
.limit(parseInt(show));
// Get total count of documents matching the query
const total_data = await KYC.countDocuments(query);
// Send the fetched data as a response
// Calculate total pages
const total_pages = Math.ceil(total_data / show);
// Send the response
res.status(200).json({
success: true,
total_data: total_data,
total_pages: Math.ceil(total_data / page),
total_pages: total_pages,
retaildistributor,
});
} catch (error) {

View File

@ -996,35 +996,55 @@ export const updateProfile = catchAsyncErrors(async (req, res, next) => {
// 9.Get all users(admin)
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
// Assuming your User model is imported as 'User'
const { page = 1, show = 10, name, mobileNumber } = req.query;
// Create a filter object
const filter = { role: "principal-Distributor" };
try {
// Extract query parameters
const { page = 1, show = 10, name, mobileNumber, SBU } = req.query;
if (name) {
filter.name = { $regex: name, $options: "i" }; // Case-insensitive regex search
// Create a filter object
const filter = { role: "principal-Distributor" };
// Add case-insensitive name search
if (name) {
filter.name = { $regex: name, $options: "i" }; // Case-insensitive regex search
}
// Add mobileNumber search
if (mobileNumber) {
filter.phone = mobileNumber;
}
// Add SBU filter if provided
if (SBU) {
filter.SBU = { $regex: SBU, $options: "i" }; // Case-insensitive regex search for SBU
}
// Calculate pagination values
const limit = parseInt(show, 10);
const skip = (parseInt(page, 10) - 1) * limit;
// Find users with the filter, pagination, and sorting
const users = await User.find(filter)
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit);
// Count total users matching the filter
const totalUsers = await User.countDocuments(filter);
// Respond with users and pagination info
res.status(200).json({
success: true,
users,
total_data: totalUsers,
total_pages: Math.ceil(totalUsers / limit),
});
} catch (error) {
// Handle errors
console.error(error);
res.status(500).json({ message: "Server Error", error });
}
if (mobileNumber) {
filter.phone = mobileNumber;
}
// Calculate pagination values
const limit = parseInt(show, 10);
const skip = (parseInt(page, 10) - 1) * limit;
// Count total users matching the filter
// Find users with pagination and filters
const users = await User.find(filter)
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit);
const totalUsers = await User.countDocuments(filter);
res.status(200).json({
success: true,
users,
totalUsers,
});
});
export const getAllPrincipalDistributorbytmId = catchAsyncErrors(
async (req, res, next) => {
const { page = 1, show = 10, name, mobileNumber } = req.query;