Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
2.4 kB
4
Indexable
Never
exports.addAll = catchAsync(async (req, res, next) => {
  let { categories, products_section1, products_section2, products_section3, imagesmobile, imagesdesktop } = req.body;
  let obj = {};
  if (products_section1) {
    const prod_s1 = await addsec1(products_section1, next);
    obj.section1 = prod_s1;
  }
  if (products_section2) {
    const prod_s2 = await addSec2(products_section2, next);
    obj.section2 = prod_s2;
  }
  if (products_section2) {
    const prod_s3 = await addSec3(products_section3, next);
    obj.section3 = prod_s3;
  }
  if (categories) {
    const cats = await addCats(categories, next);
    obj.categories = cats;
  }

  if (imagesmobile) {
    const imagesformobile = await addMobileBanner(imagesmobile, next);
    obj.mobileimages = imagesformobile;
  }
  if (imagesdesktop) {
    const imagesfordesktop = await addDesktopBanner(imagesdesktop, next);
    obj.desktopimages = imagesfordesktop;
  }

  const result = await Layout.updateOne({}, obj);
  if (result) {
    res.status(200).json({
      message: 'Layout is successfully updated',
      result
    });
  } else {
    return next(new AppError('Internal Server Error', 500));
  }
});








const addSec3 = async (products, 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