Class: Edi::Amazon::ListingMessageProcessor

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

Defined Under Namespace

Classes: Result

Constant Summary

Constants included from Edi::AddressAbbreviator

Edi::AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

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, #options, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#build_json(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, prefetch_media: false, use_fba_sku: false) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
# File 'app/services/edi/amazon/listing_message_processor.rb', line 67

def build_json(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, prefetch_media: false, use_fba_sku: false)
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchlistingsitem
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchoperation
  sku_to_use = catalog_item_or_variation.reported_vendor_sku(orchestrator.partner)
  sku_to_use = catalog_item_or_variation.amazon_fba_sku if use_fba_sku
  listing_hash = case http_method
                 when 'PUT'
                   # we use PUT to create a new listing from attributes on a pending onboard amazon item, or fully update an existing listing from attributes
                   # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#put-listings2021-08-01itemsselleridsku
                   {
                     productType: catalog_item_or_variation.amazon_product_type_in_effect,
                     requirements: 'LISTING_PRODUCT_ONLY', # do offers separately, then use 'LISTING_PRODUCT_ONLY' and 'LISTING_OFFER_ONLY' in separate put messages for offers
                     sku: sku_to_use,
                     attributes: get_amazon_json_attributes(catalog_item_or_variation, fba: use_fba_sku)
                   }
                 else
                   # we use PATCH operations to partially update listing attributes on an onboarded amazon item
                   # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchlistingsitem
                   {
                     productType: catalog_item_or_variation.amazon_product_type_in_effect,
                     sku: sku_to_use,
                     patches: get_amazon_json_patches(catalog_item_or_variation, attribute_actions:, fba: use_fba_sku)
                   }
                 end

  # At this point, it is a very good idea to PING the media urls so that they are warmed up and cached
  # and ready to serve, the amazon api is very very sensitive and has aggressive timeouts
  if prefetch_media
    media_locations = collect_media_locations(listing_hash)
    media_locations.each do |url|
      response = HTTP.head(url)
      logger.info "Prefetching Media URL #{url}: #{response.status}"
    rescue StandardError => e
      logger.error "Error while prefetching Media URL #{url}"
      logger.error e
    end
  end

  listing_hash.to_json
end

#collect_media_locations(hash) ⇒ Object

Provided an attribute hash will find all the media locations



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/edi/amazon/listing_message_processor.rb', line 49

def collect_media_locations(hash)
  media_locations = []

  hash.each do |key, value|
    if key == :media_location
      media_locations << value
    elsif value.is_a?(Hash)
      media_locations.concat(collect_media_locations(value))
    elsif value.is_a?(Array)
      value.each do |element|
        media_locations.concat(collect_media_locations(element)) if element.is_a?(Hash)
      end
    end
  end

  media_locations
end

#get_amazon_json_attributes(catalog_item_or_variation, fba: false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/services/edi/amazon/listing_message_processor.rb', line 128

def get_amazon_json_attributes(catalog_item_or_variation, fba: false)
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#putlistingsitem
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#listingsitemputrequest
  # "attributes": {
  #   "size": [
  #     {
  #       "value": "Programmable",
  #       "language_tag": "en_US",
  #       "marketplace_id": "ATVPDKIKX0DER"
  #     }
  #   ],
  #   "brand": [
  #     {
  #       "value": "WarmlyYours",
  #       "language_tag": "en_US",
  #       "marketplace_id": "ATVPDKIKX0DER"
  #     }
  #   ],
  #   "color": [
  #     {
  #       "value": "White",
  #       "language_tag": "en_US",
  #       "marketplace_id": "ATVPDKIKX0DER"
  #     }
  #   ],
  #   "style": [
  #     {
  #       "value": "Programmable",
  #       "language_tag": "en_US",
  #       "marketplace_id": "ATVPDKIKX0DER"
  #     }
  #   ],...
  amazon_product_listing_generator = catalog_item_or_variation.amazon_json_generator(marketplace_id: orchestrator.marketplace, fba:, language_tag: orchestrator.try(:language_tag),
business_price_available: orchestrator.try(:business_price_available))
  amazon_product_listing_generator.build_attributes
end

#get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil, fba: false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/services/edi/amazon/listing_message_processor.rb', line 108

def get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil, fba: false)
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#listingsitempatchrequest
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchoperation
  # "patches": [
  #   {
  #     "op": "replace",
  #     "path": "/attributes/fulfillment_availability",
  #     "value": [
  #       {
  #         "fulfillment_channel_code": "DEFAULT",
  #         "quantity": 10
  #       }
  #     ]
  #   }
  # ]
  amazon_product_listing_generator = catalog_item_or_variation.amazon_json_generator(attribute_actions:, marketplace_id: orchestrator.marketplace, fba:, language_tag: orchestrator.try(:language_tag),
business_price_available: orchestrator.try(:business_price_available))
  amazon_product_listing_generator.get_patches
end

#process(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, use_fba_sku: false) ⇒ Object



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

def process(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, use_fba_sku: false)
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchlistingsitem
  # see: https://developer-docs.amazon.com/sp-api/docs/building-listings-management-workflows-guide#should-i-submit-in-bulk-using-the-json_listings_feed-or-individually-with-the-listings-items-api
  # see: https://developer-docs.amazon.com/sp-api/docs/listings-feed-type-values#listings-feed
  ecl = nil
  EdiCommunicationLog.transaction do
    logger.info "Creating listing item message for partner #{orchestrator.partner}"
    data_json = build_json(catalog_item_or_variation, http_method:, attribute_actions:, prefetch_media: true, use_fba_sku:)

    ecl = EdiCommunicationLog.create_outbound_file_from_data(
      data: data_json,
      data_type: 'json',
      file_extension: 'cii',
      partner: orchestrator.partner,
      category: 'listing_data',
      resources: catalog_item_or_variation,
      file_info: {}
    )
  end
  Result.ok(ecl)
rescue StandardError => e
  catalog_item_id = catalog_item_or_variation.try(:id) || catalog_item_or_variation.try(:catalog_item_id)
  logger.error "ListingMessageProcessor#process failed for catalog_item #{catalog_item_id}: #{e.class} - #{e.message}"
  logger.error e.backtrace&.first(5)&.join("\n")
  ErrorReporting.error(e, catalog_item_id: catalog_item_id, http_method: http_method, use_fba_sku: use_fba_sku)
  Result.fail("#{e.class}: #{e.message}")
end