Class: Catalog::PullAmazonCatalogSuppressionData

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/catalog/pull_amazon_catalog_suppression_data.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

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 BaseService

Instance Method Details

#process(options: {}) ⇒ Object

Below is a typical errors set:
[
[ 0] {
"code" => "ITEM_SUPPRESSED",
"message" => "Item B00F1GX1PY is suppressed",
"details" => "Review the suppression reasons under Items > Direct Fulfillment Inventory."
},
[ 1] {
"code" => "ITEM_SUPPRESSED",
"message" => "Item B00F1GX01Y is suppressed",
"details" => "Review the suppression reasons under Items > Direct Fulfillment Inventory."
},
[ 2] {
"code" => "ITEM_SUPPRESSED",
"message" => "Item B00F1GWY7U is suppressed",
"details" => "Review the suppression reasons under Items > Direct Fulfillment Inventory."
},
[ 3] {
"code" => "ITEM_SUPPRESSED",
"message" => "Item B00F1GX546 is suppressed",
"details" => "Review the suppression reasons under Items > Direct Fulfillment Inventory."
},
...
]



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
# File 'app/services/catalog/pull_amazon_catalog_suppression_data.rb', line 36

def process(options: {})
  messages = []
  catalog_items_updated = []
  catalog_items_failed = []

  logger.info '=========================================================================='
  logger.info 'Amazon Vendor Central Direct Fulfillment Catalog Item Suppression Data Service processing start.'
  logger.info "options: #{options}"
  logger.info '=========================================================================='
  # Catalog.where(id: [74, 17]).each do |catalog| # just for a local test
  Catalog.where(id: CatalogConstants::AMAZON_VC_DIRECT_FULFILLMENT_CATALOG_IDS).each do |catalog|
    orch = catalog&.load_orchestrator
    logger.info '=========================================================================='
    logger.info 'Amazon Vendor Central Direct Fulfillment Catalog Item Suppression Data Service processing catalog:'
    logger.info "#{catalog.name}, orchestrator partner: #{orch.partner}."
    logger.info '=========================================================================='

    # First we find the most recent EdiCommunicationLog
    # we want to grab the six most recent inventory_advice ECLs and select one with an exception )that is don;t use one that's too old/stale) for this catalog partner
    ecl = EdiCommunicationLog.order(updated_at: :desc).find_by(partner: orch.partner.to_s, state: 'exception', category: 'inventory_advice')

    unless ecl
      return Result.new(catalog_item_suppression_data_pulled: false,
                        messages: ['No appropriate EDI Communication Logs found!'],
                        catalog_items_updated:,
                        catalog_items_failed:)
    end

    logger.info '=========================================================================='
    logger.info 'Amazon Vendor Central Direct Fulfillment Catalog Item Suppression Data Service processing EDI Communication Log:'
    logger.info "ID #{ecl.id}, partner: #{ecl.partner}, category: #{ecl.category}, state: #{ecl.state}, updated_at: #{ecl.updated_at}."
    logger.info '=========================================================================='
    ErrorReporting.scoped({ edi_log_id: ecl.id, partner: ecl.partner }) do
      errors_str = ecl.notes&.split(%(,"errors":))&.last&.split(%(}}}))&.first || '[]'
      item_suppression_errors = JSON.parse(errors_str)
      items_str = ecl.data&.split(%("items":))&.last&.split(%(}}))&.first || '[]'
      items = JSON.parse(items_str)
      asins = items.collect { |item_hash| item_hash.fetch('buyerProductIdentifier') }

      logger.info '=========================================================================='
      logger.info 'Amazon Vendor Central Direct Fulfillment Catalog Item Suppression Data Service found:'
      logger.info " #{item_suppression_errors.size} item suppression errors, #{items.size} items, #{asins.size} ASINs."
      logger.info '=========================================================================='

      catalog.catalog_items.by_asins(asins).each do |catalog_item|
        ErrorReporting.scoped({ catalog_item_id: catalog_item.id }) do
          logger.info '=========================================================================='
          logger.info 'Amazon Vendor Central Direct Fulfillment Catalog Item Suppression Data Service processing catalog item:'
          logger.info "ID #{catalog_item.id}, SKU: #{catalog_item.sku}, ASIN: #{catalog_item.amazon_asin}, amazon_item_suppression_pull_datetime: #{catalog_item.amazon_item_suppression_pull_datetime}."

          # Skip if our last update of these item_suppressed_status and item_suppressed_status_message were more recent than the EDI Communication Log in question's updated at
          # We need a fresh EDI Communication Log to actually change anything
          skip = catalog_item.amazon_item_suppression_pull_datetime && catalog_item.amazon_item_suppression_pull_datetime >= ecl.updated_at
          logger.info "Skipping because EDI Communication Log is not recent enough: #{skip}."
          next if skip

          item_suppression_hash = item_suppression_errors.detect { |item_suppr_hash| item_suppr_hash.fetch('message')&.to_s&.index(catalog_item.amazon_asin) }
          if item_suppression_hash
            catalog_item.update_columns(item_suppressed_status: item_suppression_hash.fetch('code'), item_suppressed_status_message: item_suppression_hash.fetch('message'), amazon_item_suppression_pull_datetime: Time.current)
            logger.info "Found item suppression data for this ASIN: #{item_suppression_hash}, updated item suppression status fields."
          else
            logger.info 'No item suppression data found for this ASIN:, resetting item suppression status fields.'
            catalog_item.update_columns(item_suppressed_status: nil, item_suppressed_status_message: nil, amazon_item_suppression_pull_datetime: Time.current) # reset the item_suppressed_status_message since no error was detected
          end
          catalog_items_updated << catalog_item
          logger.info '=========================================================================='
        end
      end
    end
  end
  logger.info '=========================================================================='
  logger.info 'Amazon Vendor Central Direct Fulfillment Catalog Item Suppression Data Worker processing end.'
  logger.info '=========================================================================='
  Result.new(catalog_item_suppression_data_pulled: catalog_items_failed.empty?,
             messages:,
             catalog_items_updated:,
             catalog_items_failed:)
end