Class: AmazonItemOperationWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/amazon_item_operation_worker.rb

Overview

Sidekiq worker: amazon item operation.

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object

Push Amazon Seller API listings items data

Parameters:

  • options (Hash) (defaults to: {})

    job options

Options Hash (options):

  • catalog_item_ids (Array<Integer>)

    catalog item IDs to operate on

  • operation (String, Array<String>)

    operation(s) to perform (PUT, DELETE, DISCONTINUE, PATCH, PUSH_PRICE, PUSH_INVENTORY, PULL_CATALOG_INFORMATION, PULL_BUY_BOX_STATUS, PULL_LISTING_INFORMATION, PULL_LISTING_SCHEMA)

  • fbm_vs_fba (String)

    fulfillment channel for PUT/PATCH operations

  • attribute_actions (Hash)

    attribute actions for the PATCH operation

  • redirect_to (String)

    path to redirect to when the job completes



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
# File 'app/workers/amazon_item_operation_worker.rb', line 21

def perform(options = {})
  catalog_item_ids = options[:catalog_item_ids]
  operations = [options[:operation]].flatten.presence.compact.uniq
  if operations.present? && catalog_item_ids.present?
    # Eager load associations to prevent N+1 queries:
    # - store_item + item: needed for sku_and_name
    # - catalog + amazon_marketplace: needed for amazon_catalog_item? check
    catalog_items = CatalogItem.amazons_sellers
                               .includes(store_item: :item, catalog: :amazon_marketplace)
                               .where(id: catalog_item_ids)
    total_operations = operations.size * catalog_items.size
    total total_operations
    i = 0
    operations.each do |operation|
      catalog_items.each_with_index do |catalog_item, _index|
        i += 1
        at i, "Performing #{operation} on catalog item #{catalog_item.id} #{catalog_item.sku_and_name} #{catalog_item.amazon_asin}"
        begin
          case operation
          when 'PUT'
            catalog_item.amazon_send_put_listing_information(fbm_vs_fba: options[:fbm_vs_fba])
          when 'DELETE'
            catalog_item.amazon_delete_information
          when 'DISCONTINUE'
            amazon_discontinue_catalog_item(catalog_item)
          when 'PATCH'
            catalog_item.amazon_send_patch_listing_information(attribute_actions: options[:attribute_actions], fbm_vs_fba: options[:fbm_vs_fba])
          when 'PUSH_PRICE'
            catalog_item.push_price_message
          when 'PUSH_INVENTORY'
            catalog_item.push_inventory_message
          when 'PULL_CATALOG_INFORMATION'
            catalog_item.amazon_pull_catalog_information
          when 'PULL_BUY_BOX_STATUS'
            catalog_item.amazon_pull_buy_box_status
          when 'PULL_LISTING_INFORMATION'
            catalog_item.amazon_pull_listing_information
          when 'PULL_LISTING_SCHEMA'
            catalog_item.amazon_pull_listing_schema
          else
            store error_message: "Invalid operation #{operation}"
          end
          catalog_item.reload

          # Broadcast real-time updates to Amazon dashboard
          # Skip amazon check since we already know from amazons_sellers scope
          catalog_item.broadcast_amazon_dashboard_update(skip_amazon_check: true)
        rescue StandardError => e
          logger.error e
          ErrorReporting.error e, catalog_item_id: catalog_item.id, operation: operation
          store error_message: "#{operation} error: #{e.message}"
        end
      end
    end
  end
  store redirect_to: options[:redirect_to]
end