Untitled
unknown
plain_text
a year ago
6.1 kB
7
Indexable
exports.getProducts = catchAsync(async (req, res, next) => {
let { categories, min_price, max_price, offer ,name} = req.query;
const obj = {};
if(offer){
obj.offer = { $exists: true };
}
if(name){
obj.$or = [
{ 'name.fr': { $regex: '.*' + name + '.*', $options: 'i' } }, // Match in French
{ 'name.en': { $regex: '.*' + name + '.*', $options: 'i' } }, // Match in English
];
}
let buyer = null;
if (req?.user.role == 'retailer') {
buyer = await Retailer.findById({ _id: req?.user.profile });
}else if (req?.user.role == 'distributeur') {
buyer = await Distributeur.findById({ _id: req?.user.profile });
}
// categories
if (categories) {
categories = JSON.parse(categories)
const ids_categories = [];
for(const category_name of categories){
let category = await Category.find({category:category_name});
if (category.length < 1) {
return next(new AppError('category not found', 400));
}
ids_categories.push(category[0]._id)
}
obj.categories = {$in : ids_categories}
}
const filterprice = {};
if (min_price) {
if (req?.user?.role == 'retailer') {
if (buyer?.pricingType) {
const type_p = buyer?.pricingType.toString();
filterprice['pricing.retailerPrices.' + type_p] = {
...filterprice['pricing.retailerPrices.' + type_p],
$gte: parseFloat(min_price)
};
}
}
if (req?.user?.role == 'distributeur') {
if (buyer?.pricingType) {
const type_p = buyer?.pricingType.toString();
filterprice['pricing.distributeurPrices.' + type_p] = {
...filterprice['pricing.distributeurPrices.' + type_p],
$gte: parseFloat(min_price)
};
}
} else {
filterprice['pricing.publicPrice'] = {
...filterprice['pricing.publicPrice'],
$gte: parseFloat(min_price)
};
}
}
if (max_price) {
if (req?.user?.role == 'retailer') {
if (buyer?.pricingType) {
const type_p = buyer?.pricingType.toString();
filterprice['pricing.retailerPrices.' + type_p] = {
...filterprice['pricing.retailerPrices.' + type_p],
$lte: parseFloat(max_price)
};
}
}
if (req?.user?.role == 'distributeur') {
if (buyer?.pricingType) {
const type_p = buyer?.pricingType.toString();
filterprice['pricing.distributeurPrices.' + type_p] = {
...filterprice['pricing.distributeurPrices.' + type_p],
$lte: parseFloat(max_price)
};
}
} else {
filterprice['pricing.publicPrice'] = {
...filterprice['pricing.publicPrice'],
$lte: parseFloat(max_price)
};
}
}
const rests = await Product.aggregate([
{ $match: obj },
{
$lookup: {
from: 'productvariants',
localField: 'variants',
foreignField: '_id',
pipeline: [{ $match: filterprice }, { $project: { quantity: 1, pricing: 1 } }],
as: 'variantsDetails'
}
},
{
$addFields: {
firstVariant: {
$cond: {
if: {
$and: [{ $eq: ['$isvariant', true] }, { $gt: [{ $size: '$variantsDetails' }, 0] }]
},
then: {
variant: { $arrayElemAt: ['$variants', 0] }
},
else: null
}
}
}
},
{
$match: {
$or: [{ $and: [{ isvariant: false }, filterprice] }, { firstVariant: { $ne: null } }]
}
},
{
$lookup: {
from: 'categories',
localField: 'categories',
foreignField: '_id',
pipeline: [{ $project: { category: 1 } }],
as: 'categories'
}
},
{
$lookup: {
from: 'groups',
localField: 'groups',
foreignField: '_id',
pipeline: [{ $project: { group: 1 } }],
as: 'groups'
}
},
{
$lookup: {
from: 'subcategories',
localField: 'subcategories',
foreignField: '_id',
pipeline: [{ $project: { subcategory: 1 } }],
as: 'subcategories'
}
},
{
$lookup: {
from: 'brand',
localField: 'brand',
foreignField: '_id',
pipeline: [{ $project: { brand: 1 } }],
as: 'brand'
}
},
{
$lookup: {
from: 'offers',
localField: 'offer',
foreignField: '_id',
pipeline: [{ $match: { status: true } }, { $project: { discount: 1, status: 1 } }],
as: 'offer'
}
}
]);
const data = rests.reduce((result, product) => {
let obj = {};
obj._id = product?._id;
obj.name = product?.name;
obj.description = product?.description ;
obj.image = product?.images[0];
obj.visibility = product?.visibility;
obj.categories = product?.categories;
obj.isVariant = product.isvariant;
obj.isNew = product.isNew;
obj.offer = product.offer?.at(0)?.discount ?? null;
if (!product.isvariant) {
obj.quantity = product?.quantity;
if (buyer) {
if (req?.user.role == 'retailer') {
obj.publicPrice = Number.parseFloat(product.pricing.retailerPrices[buyer.pricingType.toString()]).toFixed(2);
} else if (req?.user.role == 'distributeur') {
obj.publicPrice = Number.parseFloat(product.pricing.distributeurPrices[buyer.pricingType.toString()]).toFixed(2);
}
} else {
obj.publicPrice = product.pricing.publicPrice;
}
} else {
obj.quantity = product?.firstVariant?.totalQuantity;
obj.publicPrice = product?.firstVariant?.publicPrice;
}
result.push(obj);
return result;
}, []);
//});
res.status(200).json({
data
});
});Editor is loading...
Leave a Comment