Your IP : 216.73.216.220


Current Path : /home/deltalab/PMS/partner-manager-backend/rest/queries/
Upload File :
Current File : //home/deltalab/PMS/partner-manager-backend/rest/queries/listing.js

/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
const { listingModel } = require('../../models/mongoose/listing');
const { productModel } = require('../../models/mongoose/product');
const ims = require('../../services/ims');
const utility = require('../../services/utility');

async function updateProducts(entries, isDiscount, decoded) {
  const products = [];
  for (const entry of entries) {
    const reference = await productModel.find({ 'offers._id': entry.productId });
    if (reference) {
      if (!utility.checkProductOwner(reference, decoded)) {
        continue;
      }

      if (reference.price === entry.price) {
        console.log('prezzo uguale, salto');
        continue;
      }

      for (let j = 0; j < reference.offers.length; j++) {
        if (reference.offers[j]._id.equals(entry.productId)) {
          const offer = reference.offers[j];
          reference.showMsrp = true;
          offer.price = entry.price;
        }
      }
      await reference.save();
      const response = await ims.referenceUpdate(reference, reference.sku);
      products.push(reference);
    }
  }
  return products;
}

async function restoreProducts(entries) {
  const products = [];
  for (const entry of entries) {
    const product = await productModel.findById(entry.product._id);
    if (product) {
      // TODO: controllare che il prodotto sia mio
      product.price = product.originalPrice;
      product.originalPrice = 0;
      product.showMsrp = false;
      await product.save();
      const response = await ims.referenceUpdate(product, product.sku);
      products.push(product);
    }
  }
  return products;
}

async function addOffer(offer, listingId) {
  const listing = await listingModel.findById(listingId);
  let found = false;
  if (listing) {
    for (const offerEntry of listing.entries) {
      if (offerEntry.productId.equals(offer._id)) {
        found = true;
        break;
      }
    }
    if (!found) {
      // await ims.addProductToIMSListing(product.imsgid, args.listingIds); //not needed in magento environment
      listing.entries.push({ productId: offer._id, price: 0.00 });
      listing.save();
    }
  }
  return true;
}

async function getOffers(listingId) {
  const listing = await listingModel.findById(listingId);
  const fullEntries = [];
  if (listing) {
    for (const offerEntry of listing.entries) {
      const reference = await productModel.findOne({ 'offers._id': offerEntry.productId });
      if (!reference) {
        break;
      }
      for (let j = 0; j < reference.offers.length; j++) {
        if (reference.offers[j]._id.equals(offerEntry.productId)) {
          fullEntries.push({ product: reference.offers[j], price: offerEntry.price });
        }
      }
    }
  }
  return fullEntries;
}

module.exports = {
  updateProducts,
  restoreProducts,
  addOffer,
  getOffers,
};