Untitled
unknown
plain_text
a year ago
1.8 kB
10
Indexable
exports.getRetailersByFilter = catchAsync(async (req, res, next) => {
const { email, pricingType, status } = req.query;
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
let search = { role: 'retailer' };
let obj = {};
if (req.user.role == 'salesman') {
obj.sales_man = req.user.profile;
}
if (email) {
search.email = { $regex: email, $options: 'i' };
}
if (pricingType == 1 || pricingType == 2 || pricingType == 3) {
obj.pricingType = parseInt(pricingType);
}
if (status == 'true') {
search.status = true;
} else if (status == 'false') {
search.status = false;
}
const count = await User.find(search).count();
let retailers = null;
let countPages = count % limit == 0 ? parseInt(count / limit) : parseInt(count / limit) + 1;
retailers = await User.aggregate([
{ $match: search },
{
$lookup: {
from: 'retailers',
localField: 'profile',
foreignField: '_id',
pipeline: [{ $match: obj }, { $project: { pricingType: 1 } }],
as: 'Pricing'
}
},
{ $match: { Pricing: { $ne: [] } } }
])
.skip((page - 1) * limit)
.limit(limit);
//.map((resul) => resul.PricingType)
const data = retailers.reduce((result, retailer) => {
let obj = {};
obj._id = retailer._id;
obj.name = retailer.name;
obj.joiningDate = retailer.createdAt;
obj.email = retailer.email;
obj.phone = retailer.phone;
obj.status = retailer.status;
obj.profileImage = retailer.photo;
obj.pricingType = retailer.Pricing[0].pricingType;
result.push(obj);
return result;
}, []);
res.status(200).json({
status: 'success',
data: data,
count
});
});Editor is loading...
Leave a Comment