Class: Edi::AmazonVc::OldCatalogItemInformationProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/amazon_vc/old_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



148
149
150
151
# File 'app/services/edi/amazon_vc/old_catalog_item_information_processor.rb', line 148

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, ecl_ve: nil) ⇒ Object

we only deal with one ecl but also p[ass in ecl_ve which has the results of a PATCH call on the same Listing Item endpoint with mode=VALIDATION_PREVIEW]



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
142
143
144
145
# File 'app/services/edi/amazon_vc/old_catalog_item_information_processor.rb', line 5

def process(ecl, locale:, catalog_item: nil, ecl_ve: nil) # we only deal with one ecl but also p[ass in ecl_ve which has the results of a PATCH call on the same Listing Item endpoint with mode=VALIDATION_PREVIEW]
  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}" }

    # add pull date to json hash

    json_hash[:catalog_information_refresh_datetime] = ecl.updated_at

    # 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
    json_hash.fetch(:attributes, {})

    # 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"
    # }

    # # Get reported product type
    pt = json_hash.fetch(:summaries, [])&.first&.fetch(:productType)
    # Record this in the third_party_product_type
    catalog_item.third_party_product_type = pt
    catalog_item.amazon_reported_product_type = pt

    # # Get  itemName
    # item_name = json_hash.fetch(:summaries, [])&.first&.fetch(:itemName)
    # item.send(:"amazon_title_#{locale}=", item_name)
    # # Also record this in the third_party_name
    # catalog_item.send(:"third_party_name_#{locale}=", item_name)

    # 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 = language_tag_to_locale_to_set(locale)
    #     item.send(:"amazon_feature_#{feature_index}_#{locale_to_set}=", bullet)
    #   end
    # end

    ecl.notes = nil

    validation_errors = []
    if ecl_ve&.data.present? && (json_ve_hash = JSON.parse(ecl_ve.data).with_indifferent_access).present?
      Rails.logger.debug { "Edi::Amazon::CatalogItemInformationProcessor#process, json_ve_hash: #{json_ve_hash}" }
      unless %w[accepted valid].include?(json_ve_hash.fetch(:status).downcase)
        json_ve_hash.fetch(:issues).each do |issue_hash|
          validation_errors << issue_hash
        end
      end
    end

    json_hash[:validation_errors] = validation_errors

    # if item.save && catalog_item.save
    if catalog_item.save
      res = true
    else
      # ecl.notes = "Item cannot be saved: #{item.errors_to_s}. #{catalog_item.errors_to_s}"
      ecl.notes = "Catalog Item cannot be saved: #{catalog_item.errors_to_s}"
    end

    existing_amazon_info = catalog_item.retailer_information || {}
    existing_amazon_info[locale] = json_hash
    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)
    ecl.complete! if ecl.can_complete?
    ecl_ve.edi_documents.create(catalog_item: catalog_item) if ecl_ve
    ecl_ve&.complete! if ecl_ve&.can_complete?

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