mapping RD/PD
This commit is contained in:
parent
154f416c7a
commit
fe1648f856
@ -44,14 +44,25 @@ export const getDistributors = async (req, res) => {
|
|||||||
if (!["PrincipalDistributor", "RetailDistributor"].includes(type)) {
|
if (!["PrincipalDistributor", "RetailDistributor"].includes(type)) {
|
||||||
return res.status(400).json({ message: "Invalid distributor 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;
|
let distributors;
|
||||||
// console.log("type",type);
|
// console.log("type",type);
|
||||||
if (type === "PrincipalDistributor") {
|
if (type === "PrincipalDistributor") {
|
||||||
// Fetch all PrincipalDistributors
|
// Fetch all PrincipalDistributors
|
||||||
const principalDistributors = await User.find({
|
const principalDistributors = await User.find(filter);
|
||||||
role: "principal-Distributor",
|
|
||||||
});
|
|
||||||
// console.log("principalDistributors",principalDistributors);
|
// console.log("principalDistributors",principalDistributors);
|
||||||
// Map each PrincipalDistributor to include their ShippingAddress
|
// Map each PrincipalDistributor to include their ShippingAddress
|
||||||
distributors = await Promise.all(
|
distributors = await Promise.all(
|
||||||
@ -67,7 +78,7 @@ export const getDistributors = async (req, res) => {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// For RetailDistributor, fetch approved KYC documents
|
// For RetailDistributor, fetch approved KYC documents
|
||||||
distributors = await KYC.find({ status: "approved" });
|
distributors = await KYC.find(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json(distributors);
|
res.status(200).json(distributors);
|
||||||
|
@ -261,21 +261,58 @@ export const getAllKycRejected = async (req, res) => {
|
|||||||
// Get All KYC Approved
|
// Get All KYC Approved
|
||||||
export const getAllKycApproved = async (req, res) => {
|
export const getAllKycApproved = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// Fetch all KYC documents from the database
|
// Extract query parameters from the request
|
||||||
// console.log("req came here ");
|
const { page = 1, show = 10, name, principaldistributor } = req.query;
|
||||||
const kycs = await KYC.find({ status: "approved" })
|
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 })
|
.sort({ createdAt: -1 })
|
||||||
.populate("principal_distributer", "name")
|
.populate("principal_distributer", "name") // Only include the 'name' field
|
||||||
.populate("addedBy");
|
.populate("addedBy")
|
||||||
// console.log(kycs);
|
.skip(skip)
|
||||||
// Send the fetched data as a response
|
.limit(parseInt(show));
|
||||||
res.status(200).json(kycs);
|
|
||||||
|
// 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) {
|
} catch (error) {
|
||||||
// Handle any errors that occur during the fetch operation
|
console.error(error);
|
||||||
console.log(error);
|
|
||||||
res.status(500).json({ message: "Server 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 {
|
||||||
@ -388,10 +425,10 @@ export const getAllPrincipalDistributers = async (req, res) => {
|
|||||||
// Otherwise, filter by req.user._id
|
// Otherwise, filter by req.user._id
|
||||||
filter.mappedby = req.user._id;
|
filter.mappedby = req.user._id;
|
||||||
}
|
}
|
||||||
console.log(filter);
|
// console.log(filter);
|
||||||
// Fetch the principal distributors based on the filter
|
// Fetch the principal distributors based on the filter
|
||||||
const principalDistributers = await User.find(filter);
|
const principalDistributers = await User.find(filter);
|
||||||
console.log(principalDistributers);
|
// console.log(principalDistributers);
|
||||||
// Send the fetched data as a response
|
// Send the fetched data as a response
|
||||||
if (principalDistributers.length > 0) {
|
if (principalDistributers.length > 0) {
|
||||||
res.status(200).json(principalDistributers);
|
res.status(200).json(principalDistributers);
|
||||||
@ -462,17 +499,19 @@ export const saveFCMTokenForTM = async (req, res) => {
|
|||||||
};
|
};
|
||||||
export const getAllKycApprovedbytmid = async (req, res) => {
|
export const getAllKycApprovedbytmid = async (req, res) => {
|
||||||
try {
|
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 { tradename, page = 1, show = 10 } = req.query;
|
||||||
|
const skip = (page - 1) * show;
|
||||||
|
|
||||||
|
// Build query object
|
||||||
const query = { status: "approved", mappedTM: id };
|
const query = { status: "approved", mappedTM: id };
|
||||||
|
|
||||||
if (tradename) {
|
if (tradename) {
|
||||||
query.trade_name = new RegExp(tradename, "i");
|
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)
|
const retaildistributor = await KYC.find(query)
|
||||||
.sort({ createdAt: -1 })
|
.sort({ createdAt: -1 })
|
||||||
.populate("principal_distributer")
|
.populate("principal_distributer")
|
||||||
@ -481,12 +520,18 @@ export const getAllKycApprovedbytmid = async (req, res) => {
|
|||||||
.populate("addedBy")
|
.populate("addedBy")
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(parseInt(show));
|
.limit(parseInt(show));
|
||||||
|
|
||||||
|
// Get total count of documents matching the query
|
||||||
const total_data = await KYC.countDocuments(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({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
total_data: total_data,
|
total_data: total_data,
|
||||||
total_pages: Math.ceil(total_data / page),
|
total_pages: total_pages,
|
||||||
retaildistributor,
|
retaildistributor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -996,35 +996,55 @@ export const updateProfile = catchAsyncErrors(async (req, res, next) => {
|
|||||||
|
|
||||||
// 9.Get all users(admin)
|
// 9.Get all users(admin)
|
||||||
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
|
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
|
||||||
// Assuming your User model is imported as 'User'
|
try {
|
||||||
const { page = 1, show = 10, name, mobileNumber } = req.query;
|
// Extract query parameters
|
||||||
// Create a filter object
|
const { page = 1, show = 10, name, mobileNumber, SBU } = req.query;
|
||||||
const filter = { role: "principal-Distributor" };
|
|
||||||
|
|
||||||
if (name) {
|
// Create a filter object
|
||||||
filter.name = { $regex: name, $options: "i" }; // Case-insensitive regex search
|
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(
|
export const getAllPrincipalDistributorbytmId = catchAsyncErrors(
|
||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
const { page = 1, show = 10, name, mobileNumber } = req.query;
|
const { page = 1, show = 10, name, mobileNumber } = req.query;
|
||||||
|
Loading…
Reference in New Issue
Block a user