Untitled
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)); } });
Leave a Comment