Class: Catalog::CreateCatalogItem

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

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(catalog, catalog_item_params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/catalog/create_catalog_item.rb', line 7

def process(catalog, catalog_item_params)
  catalog_item = catalog.catalog_items.build(catalog_item_params)
  errors = []
  if catalog.parent_catalog.present? && catalog_item.add_kit_components_to_catalog.to_b
    catalog_item.item.kit_components.select{|kc| kc.catalog_items.joins(:store_item).where(catalog_id: catalog.id, store_items: {location: catalog_item.store_item.location}).empty? }.each do |kc|
      kci = kc.catalog_items.find_by(catalog_id: catalog.parent_catalog.id)
      raise "Kit component #{kc.id} /#{kc.sku} does not exist in parent catalog id #{catalog.parent_catalog.id} / #{catalog.parent_catalog.name}" unless kci
      r = Catalog::CreateCatalogItem.new.process(catalog, kci.deep_dup.attributes.with_indifferent_access.merge(state: 'active_hidden')) # better to hide from feed since they are automatically added per Elodie
      errors.concat(r&.catalog_item&.errors&.full_messages)
    end
  end
  if catalog_item.save
    Result.new(catalog_item: catalog_item, catalog_item_created: true, messages: ["Catalog Item #{catalog_item.id} created"])
  else
    errors.concat(catalog_item.errors.full_messages)
    Result.new(catalog_item: catalog_item, catalog_item_created: false, messages: catalog_item.errors.full_messages )
  end
end