Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.6 kB
2
Indexable
exports.uploadProductsFromExcel = catchAsync(async (req, res, next) => {
  const file = req.file;

  if (!file) {
    return next(new AppError('Please upload a xcel file', 400));
  }

  try {
    const workbook = xlsx.readFile(file.path)
    //xlsx.read(req.file.buffer, { type: 'buffer' });
    const sheetName = workbook.SheetNames[0];
    const sheet = workbook.Sheets[sheetName];
    const products = xlsx.utils.sheet_to_json(sheet);
    TestQantityAndPrices(products,next)

      await AddQantityAndPrices(products);
      res.status(200).json({ message: 'Success' });


  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Failed to process the uploaded file' });
  }

});

const AddQantityAndPrices = async (batch) => {
  for (const productData of batch) {
    const {
      sku,
      quantity,
    
      initialPrice,
      firstPrice,
       secondPrice,
       thirdPrice,
      publicPrice
    } = productData;
    console.log("data : ",productData)
    const skuStr = String(sku);
    const currentProduct = await Product.findOne({ sku: skuStr });

    if (!currentProduct) {
        continue
    }

    let filter = {};

    if (quantity) {
      filter.quantity = quantity;
    }

    if (initialPrice || publicPrice || firstPrice || secondPrice || thirdPrice) {
      filter.pricing = currentProduct.pricing || {}; 

      if (initialPrice) {
        filter.pricing.initialPrice = initialPrice;
      }

      if (publicPrice) {
        filter.pricing.publicPrice = publicPrice;
      }
      const discountPrices = currentProduct.pricing?.discountPrices || [];

      if (firstPrice) {
        discountPrices[0] = firstPrice;  
      }
      if (secondPrice) {
        discountPrices[1] = secondPrice; 
      }
      if (thirdPrice) {
        discountPrices[2] = thirdPrice;  
      }
      filter.pricing.discountPrices = discountPrices;
    }

     await Product.updateOne(
      { sku: skuStr },  
      { $set: filter }  
     );
    }
}


    const TestQantityAndPrices = async (batch,next) => {
      for (const productData of batch) {
        const {
          sku,
          quantity,
        
          initialPrice,
          firstPrice,
           secondPrice,
           thirdPrice,
          publicPrice
        } = productData;
    
    
        const skuStr = String(sku); 
        if(!sku ){
          return next(new AppError('sku is require', 400));
        }
        if(quantity){
          if(quantity < 0){
            return next(new AppError('quantity of product wwith sku '+sku+' is negative', 400)); 
          }
        }
        if(initialPrice){
          if(initialPrice < 0){
            return next(new AppError('initialPrice of product wwith sku '+sku+' is negative', 400)); 
          }
        }
        if(firstPrice){
          if(firstPrice < 0){
            return next(new AppError('firstPrice of product wwith sku '+sku+' is negative', 400)); 
          }
        }
        if(secondPrice){
          if(secondPrice < 0){
            return next(new AppError('secondPrice of product wwith sku '+sku+' is negative', 400)); 
          }
        }
        if(thirdPrice){
          if(thirdPrice < 0){
            return next(new AppError('thirdPrice of product wwith sku '+thirdPrice+' is negative', 400)); 
          }
        }
        if(publicPrice){
          if(publicPrice < 0){
            return next(new AppError('publicPrice of product wwith sku '+publicPrice+' is negative', 400)); 
          }
        }
  }
  
};
Leave a Comment