Class: Catalog::UpdateCatalogItem

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

Overview

Service object: update catalog item.

Defined Under Namespace

Classes: RESULT

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(catalog_item_id_or_catalog_item, catalog_item_params, force_coupon_sync: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'app/services/catalog/update_catalog_item.rb', line 10

def process(catalog_item_id_or_catalog_item, catalog_item_params, force_coupon_sync: true)
  catalog_item = catalog_item_id_or_catalog_item if catalog_item_id_or_catalog_item.is_a? CatalogItem
  catalog_item ||= CatalogItem.find(catalog_item_id_or_catalog_item)
  messages = []
  amount_before = catalog_item.amount
  CatalogItem.transaction do
    if catalog_item.update(catalog_item_params)
      changes = catalog_item.saved_changes.symbolize_keys
      # messages << "Catalog item #{catalog_item.id} updated"
      # Has the price changed? then it's time to require vendor update to all children
      if amount_before != catalog_item.amount
        Rails.configuration.event_store.publish(
          Events::PriceUpdated.new(data: { catalog_item_id: catalog_item.id, price_was: amount_before, price_now: catalog_item.amount })
        )
      end
      coupon_ids = []
      coupon_ids << catalog_item.coupon_id if force_coupon_sync && catalog_item.coupon_id.present?
      coupon_ids += catalog_item.saved_change_to_coupon_id || []
      coupon_ids = coupon_ids.compact.uniq
      # Deleted coupon or coupon added, we sync our promo coupons
      if coupon_ids.present?
        Coupon.where(id: coupon_ids).find_each do |coupon|
          Coupon::PromoItemSync.new.process_coupon(coupon)
        end
      end
      catalog_item.reload
      RESULT.new(catalog_item:, catalog_item_updated: true, changes:, messages:)
    else
      RESULT.new(catalog_item:, catalog_item_updated: false, messages: catalog_item.errors.full_messages)
    end
  end
rescue ActiveRecord::RecordNotUnique => e
  # Extract constraint name for a more helpful message
  constraint = e.message[/unique constraint "([^"]+)"/, 1] || 'unknown'
  field = case constraint
          when /amazon_fnsku/ then 'Amazon FNSKU'
          when /store_item_id/ then 'Store Item ID'
          when /third_party_sku/ then 'Third Party SKU'
          when /third_party_part_number/ then 'Third Party Part Number'
          when /third_party_promo_part_number/ then 'Promo Part Number'
          when /tpn/ then 'TPN'
          else 'identifier'
          end
  RESULT.new(catalog_item:, catalog_item_updated: false, messages: ["#{field} already exists in this catalog. Please use a unique value."])
end