Untitled
exports.getAllLayoutData = catchAsync(async (req, res, next) => { 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 }); } const rests = await Layout.aggregate([ { $lookup: { from: 'products', localField: 'section1', foreignField: '_id', pipeline: [ { $lookup: { from: 'productvariants', localField: 'variants', foreignField: '_id', pipeline: [ { $project: { pricing: 1, quantity: 1 } }, { $group: { _id: null, totalQuantity: { $sum: '$quantity' }, initialPrice: { $avg: '$pricing.initialPrice' }, publicPrice: { $avg: '$pricing.publicPrice' } } } ], as: 'variant' } }, { $lookup: { from: 'categories', localField: 'categories', foreignField: '_id', pipeline: [{ $project: { category: 1 } }], as: 'categories' } }, { $lookup: { from: 'offers', localField: 'offer', foreignField: '_id', pipeline: [{ $project: { discount: 1, status: 1 } }], as: 'offer' } }, // filter { $match: { $and: [ { $or: [ { isvariant: true, 'variant.0': { $exists: true } }, { isvariant: false } ] }, { visibility: true } ] } }], as: 'section1' } }, { $lookup: { from: 'products', localField: 'section2', foreignField: '_id', pipeline: [ { $lookup: { from: 'productvariants', localField: 'variants', foreignField: '_id', pipeline: [ { $project: { pricing: 1, quantity: 1 } }, { $group: { _id: null, totalQuantity: { $sum: '$quantity' }, initialPrice: { $avg: '$pricing.initialPrice' }, publicPrice: { $avg: '$pricing.publicPrice' } } } ], as: 'variant' } }, { $lookup: { from: 'categories', localField: 'categories', foreignField: '_id', pipeline: [{ $project: { category: 1 } }], as: 'categories' } }, { $lookup: { from: 'offers', localField: 'offer', foreignField: '_id', pipeline: [{ $project: { discount: 1, status: 1 } }], as: 'offer' } }, // filter { $match: { $and: [ { $or: [ { isvariant: true, 'variant.0': { $exists: true } }, { isvariant: false } ] }, { visibility: true } ] } }], as: 'section2' } }, { $lookup: { from: 'products', localField: 'section3', foreignField: '_id', pipeline: [ { $lookup: { from: 'productvariants', localField: 'variants', foreignField: '_id', pipeline: [ { $project: { pricing: 1, quantity: 1 } }, { $group: { _id: null, totalQuantity: { $sum: '$quantity' }, initialPrice: { $avg: '$pricing.initialPrice' }, publicPrice: { $avg: '$pricing.publicPrice' } } } ], as: 'variant' } }, { $lookup: { from: 'categories', localField: 'categories', foreignField: '_id', pipeline: [{ $project: { category: 1 } }], as: 'categories' } }, { $lookup: { from: 'offers', localField: 'offer', foreignField: '_id', pipeline: [{ $project: { discount: 1, status: 1 } }], as: 'offer' } }, // filter { $match: { $and: [ { $or: [ { isvariant: true, 'variant.0': { $exists: true } }, { isvariant: false } ] }, { visibility: true } ] } }], as: 'section3' } }, // categories { $lookup: { from: 'categories', localField: 'categories', foreignField: '_id', pipeline: [{ $project: { category: 1 } }], as: 'categories' } }, ]); // console.log(rests[]) const data = rests.map(layout => { const sectionsData = {}; // Process each section separately (section1, section2, section3) ['section1', 'section2', 'section3'].forEach(section => { sectionsData[section] = layout[section].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) { if (buyer) { if (req?.user.role == 'retailer') { obj.publicPrice = product.pricing.retailerPrices[buyer.pricingType.toString()]; } else if (req?.user.role == 'distributeur') { obj.publicPrice = Number.parseFloat(product.pricing.distributeurPrices[buyer.pricingType.toString()]).toFixed(2); Number.parseFloat(total).toFixed(2); } } else { obj.publicPrice = product.pricing.publicPrice; } obj.quantity = product?.quantity; } else { obj.quantity = product?.variant[0]?.totalQuantity; obj.publicPrice = product?.variant[0]?.publicPrice; } result.push(obj); return result; }, []); }); return sectionsData; }); const resp = {}; resp.data = data[0]; resp.data.categories = rests[0].categories if (rests) { res.status(200).json({ data }); } else { return next(new AppError('Internal Server Error', 500)); } });
Leave a Comment