Class: Edi::Amazon::ListingItemInformationProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/amazon/listing_item_information_processor.rb

Overview

Service object: listing item information processor.

Constant Summary

Constants included from Edi::AddressAbbreviator

Edi::AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseEdiService

#duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from Edi::AddressAbbreviator

#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#process(ecl, locale:, catalog_item_or_amazon_variation: nil, _use_amazon_master_data: false) ⇒ Object

we only deal with one ecl



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/edi/amazon/listing_item_information_processor.rb', line 10

def process(ecl, locale:, catalog_item_or_amazon_variation: nil, _use_amazon_master_data: false)
  res = false

  if ecl&.data.present? && (json_hash = JSON.parse(ecl.data).with_indifferent_access).present?
    Rails.logger.debug { "Edi::Amazon::ListingItemInformationProcessor#process, json_hash: #{json_hash}" }
    # The following keys are present normally:
    # sku
    # summaries
    # productTypes
    # relationships
    # attributes
    # fulfillmentAvailability
    sku = json_hash.dig(:sku)
    # What's the asin
    asin = json_hash.dig(:summaries, 0, :asin)
    # Find the catalog item if we don't have it from the arguments
    catalog_item_or_amazon_variation ||= ecl.orchestrator.customer.catalog.catalog_items.joins(:item).where('catalog_items.third_party_part_number = :sku OR items.sku = :sku', sku: sku).first

    # What's our marketplace identifier for this catalog item
    marketplace_identifier = ecl.orchestrator.marketplace || catalog_item_or_amazon_variation.amazon_marketplace.marketplace_identifier

    # Populate amazon_listing_reported_product_type from this listing item data
    pt = json_hash.fetch(:productTypes, []).find { |pd| pd['marketplaceId'] == marketplace_identifier }&.fetch('productType')

    # Record the asin if blank
    ecl.notes = nil

    case catalog_item_or_amazon_variation
    when CatalogItem
      # derive the item
      catalog_item = catalog_item_or_amazon_variation
      item = catalog_item&.item

      item.amazon_asin ||= asin
      existing_amazon_info = catalog_item.retailer_information&.with_indifferent_access || {}
      # This is a cleanup step to migrate legacy data
      existing_amazon_info[locale] ||= {}
      existing_amazon_info[locale] = existing_amazon_info[locale].slice(:catalog, :listing)
      existing_amazon_info[locale][:listing] = { payload: json_hash, datetime: Time.current }
      catalog_item.update_columns(retailer_information: existing_amazon_info, amazon_info_datetime: Time.current, amazon_listing_reported_product_type: pt) # Record this in the amazon_listing_reported_product_type

      Item.transaction do
        if item.save
          res = true
          ecl.complete! if ecl.can_complete?
          ecl.edi_documents.create(catalog_item: catalog_item)

          catalog_item.items_are_onboarded if catalog_item.amazon_asin.present? && catalog_item.can_items_are_onboarded?
        else
          ecl.notes = "Item cannot be saved: #{item.errors_to_s}"
          if ecl.can_error?
            ecl.error!
          else
            ecl.save
          end
        end
      end
    when AmazonVariation
      # derive the item
      amz_var = catalog_item_or_amazon_variation
      amz_var.retailer_information ||= {}
      amz_var.retailer_information[marketplace_identifier] ||= {}
      amz_var.retailer_information[marketplace_identifier][:asin] = asin
      amz_var.retailer_information[marketplace_identifier][:listing] = { payload: json_hash, datetime: Time.current }

      AmazonVariation.transaction do
        if amz_var.save
          res = true
          ecl.complete! if ecl.can_complete?
          ecl.edi_documents.create(amazon_variation: amz_var)
        else
          ecl.notes = "amazon variation cannot be saved: #{amz_var.errors_to_s}"
          if ecl.can_error?
            ecl.error!
          else
            ecl.save
          end
        end
      end
    end
  else
    Rails.logger.debug { "Edi::Amazon::ListingItemInformationProcessor#process, json_hash: #{json_hash}" }
    ecl.error! if ecl.can_error?
  end
  res
end