Sha256: 773a88a6ed0111ac63f1835b53c0e316fccfe3b5f95587ca514e9c82282331a9

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Workarea
  module Listrak
    class ProductExporter
      include Sidekiq::Worker
      include Sidekiq::CallbacksWorker

      sidekiq_options(
        enqueue_on: {
          Catalog::Product => :save,
          Shipping::Sku => :save,
          Pricing::Sku => :save,
          Pricing::Price => :save,
          with: -> { ProductExporter.perform_with(self) }
        }
      )

      def self.perform_with(model)
        case model.class.name
        when "Workarea::Pricing::Price"
          [model.sku.class.name, model.sku.id]
        else
          [model.class.name, model.id]
        end
      end

      def perform(class_name, id)
        product, skus =
          if class_name == "Workarea::Catalog::Product"
            prod = Catalog::Product.find(id)
            [prod, prod.skus]
          else
            [Catalog::Product.find_by_sku(id), [id]]
          end

        return unless product.present?

        inventory = Inventory::Collection.new product.skus
        pricing = Inventory::Collection.new product.skus

        listrak_products = skus.map do |sku|
          Listrak::Models::ProductForm.new product,
            pricing_sku: pricing.for_sku(sku),
            inventory_sku: inventory.for_sku(sku)
        end

        Workarea::Listrak.data.products.import(listrak_products)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workarea-listrak-5.0.4 app/workers/workarea/listrak/product_exporter.rb
workarea-listrak-5.0.3 app/workers/workarea/listrak/product_exporter.rb
workarea-listrak-5.0.2 app/workers/workarea/listrak/product_exporter.rb
workarea-listrak-5.0.1 app/workers/workarea/listrak/product_exporter.rb