Class: CatalogItem::Onboarder

Inherits:
Object
  • Object
show all
Defined in:
app/services/catalog_item/onboarder.rb

Overview

This class is responsible for creating / updating catalog items
based on a parent catalog

Defined Under Namespace

Classes: MismatchedCatalogStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_catalog, _options = {}) ⇒ Onboarder

Returns a new instance of Onboarder.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
# File 'app/services/catalog_item/onboarder.rb', line 10

def initialize(target_catalog, _options = {})
  @target_catalog = target_catalog
  @parent_catalog = target_catalog.parent_catalog
  raise(ArgumentError, 'Missing target catalog', caller) unless @parent_catalog
  raise(ArgumentError, "No parent catalog in target catalog #{@target_catalog.id}", caller) unless @parent_catalog
  raise(MismatchedCatalogStore, "Catalog #{@parent_catalog.id} and #{@parent_catalog.id} do not pull from the same store/company", caller) if @target_catalog.store_id != @parent_catalog.store_id

  @price_sync = CatalogItem::PriceSync.initialize_for_catalog(@target_catalog)
  @last_results = {}
end

Instance Attribute Details

#parent_catalogObject (readonly)

Returns the value of attribute parent_catalog.



5
6
7
# File 'app/services/catalog_item/onboarder.rb', line 5

def parent_catalog
  @parent_catalog
end

#target_catalogObject (readonly)

Returns the value of attribute target_catalog.



5
6
7
# File 'app/services/catalog_item/onboarder.rb', line 5

def target_catalog
  @target_catalog
end

Instance Method Details

#find_source_catalog_item(item) ⇒ Object



46
47
48
49
50
# File 'app/services/catalog_item/onboarder.rb', line 46

def find_source_catalog_item(item)
  parent_catalog.catalog_items.joins(:store_item)
                .where(store_items: { item_id: item.id, location: 'AVAILABLE' })
                .first
end

#import_item(item) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/catalog_item/onboarder.rb', line 52

def import_item(item)
  # Find item in source catalog
  source_catalog_item = find_source_catalog_item(item)
  return { save: :error, errors: ["Missing source catalog item, item id #{item.id} not found in source catalog id #{@parent_catalog.id}"] } unless source_catalog_item

  tci = target_catalog.catalog_items.where(store_item_id: source_catalog_item.store_item_id).first_or_initialize(
    max_discount: source_catalog_item.max_discount
  )
  @price_sync.sync_price_from_parent(tci)
  if tci.save
    record_product_line_in_target_catalog(item)
    { save: :ok, catalog_item_id: tci.id }
  else
    { save: :error, errors: tci.errors.full_messages, catalog_item_id: tci.id }
  end
end

#import_items(items) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/services/catalog_item/onboarder.rb', line 37

def import_items(items)
  return if items.blank?

  items.find_each do |item|
    @last_results[item.id] = import_item(item)
  end
  @last_results
end

#import_missing_items_based_on_product_lines(product_line_ids = nil) ⇒ Object



33
34
35
# File 'app/services/catalog_item/onboarder.rb', line 33

def import_missing_items_based_on_product_lines(product_line_ids = nil)
  import_items missing_items_based_on_product_lines(product_line_ids)
end

#missing_items_based_on_product_lines(product_line_ids = nil) ⇒ Object

Detect all new qualifying items in parent catalog based on product lines in target



22
23
24
25
26
27
28
29
30
31
# File 'app/services/catalog_item/onboarder.rb', line 22

def missing_items_based_on_product_lines(product_line_ids = nil)
  product_line_ids = [product_line_ids].flatten if product_line_ids # Normalize integer passed in
  product_line_ids ||= target_catalog.product_line_ids
  return if product_line_ids.blank?

  ViewItem.not_in_catalog([target_catalog.id])
          .product_category_id_in(ProductCategory.all_non_publication_goods_ids)
          .primary_product_line_id_in(product_line_ids)
          .is_discontinued_eq(false)
end

#record_product_line_in_target_catalog(item) ⇒ Object



69
70
71
72
# File 'app/services/catalog_item/onboarder.rb', line 69

def record_product_line_in_target_catalog(item)
  # Track product line
  target_catalog.product_line_ids << item.primary_product_line.id if item.primary_product_line && !item.primary_product_line.id.in?(target_catalog.product_line_ids)
end