Class: Catalog::SynchronizeCatalogPrices

Inherits:
BaseService show all
Defined in:
app/services/catalog/synchronize_catalog_prices.rb

Overview

Operation to automatically process time synched catalog's price propagation rules

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ SynchronizeCatalogPrices

Returns a new instance of SynchronizeCatalogPrices.

Parameters:

  • options (Hash) (defaults to: {})

    service options (passed on to BaseService)

Options Hash (options):



13
14
15
16
# File 'app/services/catalog/synchronize_catalog_prices.rb', line 13

def initialize(options = {})
  @push_catalog_item_price_service = options[:push_catalog_item_price_service] || Catalog::PushCatalogItemPrice.new
  super
end

Instance Method Details

#process(catalogs_to_price_sync = nil, options = {}) ⇒ Result

Parameters:

  • catalogs_to_price_sync (Catalog, Array<Catalog>, ActiveRecord::Relation, nil) (defaults to: nil)

    child catalogs to sync (defaults to all active timed price-sync catalogs)

  • options (Hash) (defaults to: {})

    processing options

Options Hash (options):

  • force_sync (Boolean)

    update child prices even when still current per the pricing delay rules (default: false)

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/catalog/synchronize_catalog_prices.rb', line 22

def process(catalogs_to_price_sync = nil, options = {})
  if catalogs_to_price_sync.is_a?(Catalog)
    catalogs_to_price_sync = [catalogs_to_price_sync]
  else
    catalogs_to_price_sync ||= Catalog.active.price_sync_timed
    catalogs_to_price_sync = catalogs_to_price_sync
                             .joins(:parent_catalog)
                             .includes(:parent_catalog)
  end
  messages = []
  catalog_items_updated = []
  catalog_items_failed = []
  catalogs_to_price_sync.each do |child_catalog|
    unless child_catalog.parent_catalog_discount
      msg = " Catalog id : #{child_catalog.id} does not have a valid parent catalog discount, price sync is not applicable"
      messages << msg
      logger.error msg
      next
    end
    target_catalog_items = child_catalog.catalog_items
    target_catalog_items = block_given? ? yield(target_catalog_items) : target_catalog_items.not_discontinued
    target_catalog_items.each do |child_catalog_item|
      logger.info "Considering catalog item #{child_catalog_item.id} for update based on parent"
      unless options[:force_sync] || (child_catalog.price_sync_timed? && child_catalog_item.price_out_of_date?)
        logger.info ' -- catalog item price still considered current according to pricing delay rules'
        next
      end
      old_amount = child_catalog_item.amount
      parent_catalog_item = child_catalog_item.parent_catalog_item
      if parent_catalog_item
        new_amount = (parent_catalog_item.amount * (1.0 - child_catalog.parent_catalog_discount)).round(2)
        if new_amount == child_catalog_item.amount
          logger.info "Child catalog item id #{child_catalog_item.id} does not need a price change"
        elsif child_catalog_item.update(amount: new_amount)
          messages << msg = " -- #{old_amount} -> #{new_amount}"
          logger.info msg
          catalog_items_updated << child_catalog_item
        else
          child_catalog_item.errors.full_messages.each { |m| logger.error m }
          catalog_items_failed << child_catalog_item
          messages += child_catalog_item.errors.full_messages
        end
      else
        msg = "Child catalog item id #{child_catalog_item.id} does not have a discoverable parent"
        logger.error msg
        catalog_items_failed << child_catalog_item
        messages << msg
      end
    end
  end
  Result.new(all_items_synchronized: catalog_items_failed.empty?,
             catalogs: catalogs_with_price_sync,
             catalog_items_updated:,
             catalog_items_failed:,
             messages:)
end