Class: Edi::Amazon::CatalogItemInformationProcessor

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

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

#language_tag_to_locale_to_set(language_tag) ⇒ Object

Transform an amazon language tag into a mobility parameter



144
145
146
147
# File 'app/services/edi/amazon/catalog_item_information_processor.rb', line 144

def language_tag_to_locale_to_set(language_tag)
  locale = language_tag.downcase
  (locale == 'en_us' ? 'en' : locale) # The us locale will be treated as the default locale
end

#process(ecl, locale:, catalog_item: nil, use_amazon_master_data: false) ⇒ Object

we only deal with one ecl



5
6
7
8
9
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/services/edi/amazon/catalog_item_information_processor.rb', line 5

def process(ecl, locale:, catalog_item: nil, use_amazon_master_data: false) # we only deal with one ecl
  res = false

  if ecl&.data.present? && (json_hash = JSON.parse(ecl.data).with_indifferent_access).present?
    Rails.logger.debug { "Edi::Amazon::CatalogItemInformationProcessor#process, json_hash: #{json_hash}" }

    # The json hash will contain the following sections (keys):
    # [0] "asin",
    # [1] "attributes",
    # [2] "dimensions",
    # [3] "identifiers",
    # [4] "images",
    # [5] "productTypes",
    # [6] "relationships",
    # [7] "salesRanks",
    # [8] "summaries"

    # What's the asin
    asin = json_hash[:asin]
    # Find the catalog item if we don't have it from the arguments
    catalog_item ||= ecl.orchestrator.customer.catalog.catalog_items.amazons_with_asins.find_by(items: { amazon_asin: asin })
    # derive the item
    item = catalog_item&.item

    # Let's start with the attributes
    cii_attributes = json_hash.fetch(:attributes, {})
    # What's our marketplace identifier for this catalog item
    marketplace_identifier = catalog_item.amazon_marketplace.marketplace_identifier

    # The attributes will have the following keys (example)
    # [ 0] "item_weight",
    # [ 1] "model_name",
    # [ 2] "bullet_point",
    # [ 3] "item_package_quantity",
    # [ 4] "item_dimensions",
    # [ 5] "brand",
    # [ 6] "cpsia_cautionary_statement",
    # [ 7] "externally_assigned_product_identifier",
    # [ 8] "number_of_items",
    # [ 9] "item_package_dimensions",
    # [10] "size",
    # [11] "warranty_description",
    # [12] "part_number",
    # [13] "style",
    # [14] "color",
    # [15] "contains_liquid_contents",
    # [16] "item_package_weight",
    # [17] "manufacturer",
    # [18] "number_of_boxes",
    # [19] "recommended_browse_nodes",
    # [20] "model_number",
    # [21] "supplier_declared_dg_hz_regulation",
    # [22] "item_name",
    # [23] "list_price",
    # [24] "batteries_required",
    # [25] "voltage",
    # [26] "product_site_launch_date",
    # [27] "material",
    # [28] "manufacturer_contact_information",
    # [29] "batteries_included",
    # [30] "package_level",
    # [31] "item_type_keyword",
    # [32] "item_type_name",
    # [33] "item_shape"

    # Let's process our item name, for instance it might be like this
    #     [0] {
    #       "language_tag" => "en_CA",
    #              "value" => "WarmlyYours TempZone Electric Radiant Floor Heating Roll, Easy Floor Heating System Installation, 240V, 156 sq. ft",
    #     "marketplace_id" => "A2EUQ1WTGCTBG2"
    # },
    # [1] {
    #       "language_tag" => "en_US",
    #              "value" => "WarmlyYours TempZone Electric Radiant Floor Heating, Flex Roll, Easy Installation, 240V, 156 sq ft",
    #     "marketplace_id" => "ATVPDKIKX0DER"
    # }

    # Therefore we will process each locale provided and save with mobility in the proper place
    if use_amazon_master_data
      cii_attributes.fetch(:item_name, []).each do |attrs|
        locale_to_set = language_tag_to_locale_to_set(attrs[:language_tag])
        value = attrs[:value]
        item.send(:"amazon_title_#{locale_to_set}=", value)
        # Also record this in the third_party_name
        catalog_item.send(:"third_party_name_#{locale_to_set}=", value)
      end

      cii_attributes.fetch(:bullet_point, []).group_by { |a| a[:language_tag].downcase }.each do |locale, attrs_bullets|
        attrs_bullets.each_with_index do |attrs_bullet, index|
          feature_index = index + 1
          bullet = attrs_bullet[:value]
          next if feature_index > 9 || bullet.blank?

          locale_to_set = (locale == 'en_us' ? 'en' : locale) # The us locale will be treated as the default locale
          feature_field_name = "amazon_feature_#{feature_index}_#{locale_to_set}="
          item.send(feature_field_name, bullet)
        end
      end
    end

    # Populate amazon_reported_product_type from this catalog item data
    pt = json_hash.fetch(:productTypes, []).detect { |pd| pd['marketplaceId'] == marketplace_identifier }&.fetch('productType')
    # Record this in the third_party_product_type
    catalog_item.third_party_product_type = pt
    catalog_item.amazon_reported_product_type = pt

    ecl.notes = nil

    item.skip_item_calc = true
    if item.save && catalog_item.save
      res = true
      ecl.complete! if ecl.can_complete?
      ecl.edi_documents.create(item: item)
    else
      ecl.notes = "Item cannot be saved: #{item.errors_to_s}. #{catalog_item.errors_to_s}"
      if ecl.can_error?
        ecl.error!
      else
        ecl.save
      end
    end

    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][:catalog] = { payload: json_hash, datetime: Time.current }
    catalog_item.update_columns(retailer_information: existing_amazon_info, amazon_info_datetime: Time.current)
    Amazon::MatchBrowseNode.new.process(catalog_item)
    ecl.edi_documents.create(catalog_item: catalog_item)

  else
    Rails.logger.debug { "Edi::Amazon::CatalogItemInformationProcessor#process, json_hash: #{json_hash}" }
    ecl.error! if ecl.can_error?
  end
  res
end