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.



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

def initialize(target_catalog, options={})
  @target_catalog = target_catalog
  @parent_catalog = target_catalog.parent_catalog
  fail(ArgumentError, 'Missing target catalog', caller) unless @parent_catalog
  fail(ArgumentError, "No parent catalog in target catalog #{@target_catalog.id}", caller) unless @parent_catalog
  fail(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



42
43
44
45
46
# File 'app/services/catalog_item/onboarder.rb', line 42

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/catalog_item/onboarder.rb', line 48

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



34
35
36
37
38
39
40
# File 'app/services/catalog_item/onboarder.rb', line 34

def import_items(items)
  return unless items.present?
  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



30
31
32
# File 'app/services/catalog_item/onboarder.rb', line 30

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



20
21
22
23
24
25
26
27
28
# File 'app/services/catalog_item/onboarder.rb', line 20

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 unless product_line_ids.present?
  items = 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



65
66
67
68
69
70
# File 'app/services/catalog_item/onboarder.rb', line 65

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