Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
4.4 kB
2
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) {
    if (products_section1.length > 0) {
      if (products_section1.length > 20) {
        return next(new AppError('The number of products of section 1 is greater than 20', 400));
      }
      let duplicate = new Set(products_section1);
      if (duplicate.size !== products_section1.length) {
        return next(new AppError('Duplicate product of section 1 in list', 400));
      }
      for (const product_id of products_section1) {
        try {
          var Id = new mongoose.Types.ObjectId(product_id);
        } catch (e) {
          return next(new AppError('Invalid id.', 401));
        }
        const prod = await Product.findById(Id);
        if (!prod) {
          return next(new AppError('product of section 1 not found', 400));
        }
      }
      obj.section1 = products_section1;
    } else {
      return next(new AppError('min of products of section 1 to add is 1', 400));
    }
   
  }
  if (products_section2) {
    if (products_section2.length > 0) {
      if (products_section2.length > 20) {
        return next(new AppError('The number of products of section 2 is greater than 20', 400));
      }
      let duplicate = new Set(products_section2);
      if (duplicate.size !== products_section2.length) {
        return next(new AppError('Duplicate product of section 2  in list', 400));
      }
      for (const product_id of products_section2) {
        try {
          var Id = new mongoose.Types.ObjectId(product_id);
        } catch (e) {
          return next(new AppError('Invalid id.', 401));
        }
        const prod = await Product.findById(Id);
        if (!prod) {
          return next(new AppError('product of section 2 not found', 400));
        }
      }
      obj.section2 = products_section2;
    } else {
      return next(new AppError('min of products of section 2 to add is 1', 400));
    }
    
  }
  if (products_section3) {
    if (products_section3.length > 0) {

      let duplicate = new Set(products_section3);
      if (duplicate.size !== products_section3.length) {
        return next(new AppError('Duplicate product in list', 400));
      }
      for (const product_id of products_section3) {
        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));
        }
      }     
      obj.section3 = products_section3;
    } else {
      return next(new AppError('min of products to add is 1', 400));
    }
    
  }
  if (categories) {
    if (categories.length > 0) {
      if (categories.length > 6) {
        return next(new AppError('The number of categories is greater than 6', 400));
      }
      let duplicate = new Set(categories.map((item) => item));
      if (duplicate.size !== categories.length) {
        return next(new AppError('Duplicate categories in list', 400));
      }
      for (const category_id of categories) {
        // = categ.category_id
        try {
          var Id = new mongoose.Types.ObjectId(category_id);
        } catch (e) {
          return next(new AppError('Invalid id.', 401));
        }
        const cat = await Category.findById(Id);
        if (!cat) {
          return next(new AppError('category not found', 400));
        }
      }
      obj.categories = categories;
    } else {
      return next(new AppError('min of products to add is 1', 400));
    }
    
  }

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