Untitled
unknown
plain_text
10 days ago
2.6 kB
2
Indexable
Never
exports.addSection3 = catchAsync(async (req, res, next) => { let { products } = req.body; if (products) { if (products.length > 0) { let duplicate = new Set(products); if (duplicate.size !== products.length) { return next(new AppError('Duplicate product in list', 400)); } for (const product_id of products) { try { var Id = new mongoose.Types.ObjectId(product_id); } catch (e) { return next(new AppError('Invalid id.', 400)); } const prod = await Product.findById(Id); if (!prod) { return next(new AppError('product not found', 400)); } } const result = await Layout.updateOne({}, { section3: products }); if (result) { res.status(200).json({ message: 'Products have been added to section 3 successfully', result }); } else { return next(new AppError('Internal Server Error', 500)); } } else { return next(new AppError('min of products to add is 1', 400)); } } else { return next(new AppError('products is required', 500)); } }); exports.getProductsSection3 = catchAsync(async (req, res, next) => { try { const layout = await Layout.findOne(); const productsSection3 = await Product.find({ _id: { $in: layout.section3 },visibility:true }).populate({ path: 'offer', select: ['discount', 'status'] }); res.status(200).json({ status: 'success', data: { Section3: productsSection3 } }); } catch (error) { return next(new AppError('Error retrieving products from section 2')); } }); // modifier exports.getAllLayoutData = catchAsync(async (req, res, next) => { const pathsAndFields = [ { path: 'section1', select: ['name', 'images', 'pricing', 'quantity'], populate: { path: 'offer', select: ['discount', 'status'] } }, { path: 'section2', select: ['name', 'images', 'pricing', 'quantity'], populate: { path: 'offer', select: ['discount', 'status'] } }, { path: 'section3', select: ['name', 'images', 'pricing', 'quantity'], populate: { path: 'offer', select: ['discount', 'status'] } }, { path: 'categories', select: 'category' } ]; const layout = await Layout.findOne({}).populate(pathsAndFields); if (layout) { res.status(200).json({ layout }); } else { return next(new AppError('Internal Server Error', 500)); } });
Leave a Comment