Class: Edi::Amazon::ListingItemInformationProcessor

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

Overview

Processes pulled Amazon Listings Item payloads: records the item's ASIN and
stores the listing payload in retailer_information — per marketplace for
CatalogItem, and on the Amazon parent row for VariantGroup (also moving the
catalog item up to onboarded state).

Constant Summary

Constants included from RequestIdentifiable

RequestIdentifiable::REQUEST_ID_HEADERS

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

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

Methods included from RequestIdentifiable

#partner_request_id

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) ⇒ Boolean

Process one listing_item_information ECL (we only deal with one).

Parameters:

  • ecl (EdiCommunicationLog, nil)

    the pulled listing payload log

  • locale (String)

    marketplace locale (e.g. 'en_US')

  • catalog_item_or_amazon_variation (CatalogItem, VariantGroup, nil) (defaults to: nil)

    the record the payload belongs to (derived from the SKU when omitted)

  • _use_amazon_master_data (Boolean) (defaults to: false)

    unused

Returns:

  • (Boolean)

    whether processing saved its record



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
# File 'app/services/edi/amazon/listing_item_information_processor.rb', line 16

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 VariantGroup
      res = record_amazon_variation_listing(catalog_item_or_amazon_variation, json_hash, asin, marketplace_identifier, ecl)
    end
  else
    Rails.logger.debug { "Edi::Amazon::ListingItemInformationProcessor#process, json_hash: #{json_hash}" }
    ecl.error! if ecl.can_error?
  end
  res
end