Class: Catalog::AmazonBuyBoxRecoveryService

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

Overview

Hourly service to recover Buy Box for non-winners in Amazon US and Canada catalogs.
This service is more responsive than the nightly AmazonPricingAutomationService,
focusing only on lowering prices to win back the Buy Box (no price increases).

Key differences from AmazonPricingAutomationService:

  • Only processes non-buy-box winners (items where we're losing)
  • Pulls fresh Buy Box data before pricing decisions
  • Only lowers prices (never raises) - delegates to AmazonPriceLoweringService
  • Runs hourly for faster response to competitor price changes

Defined Under Namespace

Classes: Result

Constant Summary collapse

RATE_LIMIT_DELAY =

Rate limiting: Amazon SP-API allows ~0.5 requests/sec for Buy Box data
We use 2 seconds between items to stay well within limits

2.0.seconds

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

#initializeAmazonBuyBoxRecoveryService

Returns a new instance of AmazonBuyBoxRecoveryService.



21
22
23
24
# File 'app/services/catalog/amazon_buy_box_recovery_service.rb', line 21

def initialize
  super
  @lowering_service = Catalog::AmazonPriceLoweringService.new
end

Instance Method Details

#process(options = {}) ⇒ Object



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

def process(options = {})
  options = options.symbolize_keys if options.is_a?(Hash)
  catalog_ids = options[:catalog_ids] || [CatalogConstants::AMAZON_SC_US_CATALOG_ID, CatalogConstants::AMAZON_SC_CA_CATALOG_ID]
  limit = options[:limit]&.to_i

  messages = []
  processed_count = 0
  price_lowered_count = 0
  flagged_count = 0
  skipped_count = 0

  # Find non-buy-box winners that might be recoverable
  catalog_items = find_non_buy_box_winners(catalog_ids, limit)
  total_items = catalog_items.count

  logger.info "AmazonBuyBoxRecoveryService: Found #{total_items} non-buy-box winners to process"

  catalog_items.find_each.with_index do |catalog_item, index|
    yield(at: index + 1, total: total_items, message: "Processing #{catalog_item.sku}") if block_given?

    begin
      result = process_catalog_item(catalog_item)
      processed_count += 1

      case result[:action]
      when :price_lowered
        price_lowered_count += 1
        messages << "#{catalog_item.sku}: Price lowered to #{catalog_item.reload.amount}"
      when :flagged
        flagged_count += 1
        messages << "#{catalog_item.sku}: Flagged - #{result[:reason]}"
      when :skipped
        skipped_count += 1
        logger.debug "#{catalog_item.sku}: Skipped - #{result[:reason]}"
      end

      # Rate limiting between API calls
      sleep(RATE_LIMIT_DELAY) if index < total_items - 1
    rescue StandardError => e
      logger.error "Error processing catalog item #{catalog_item.id}: #{e.message}"
      logger.error e.backtrace.first(5).join("\n")
      messages << "#{catalog_item.sku}: Error - #{e.message}"
    end
  end

  Result.new(
    processed_count: processed_count,
    price_lowered_count: price_lowered_count,
    flagged_count: flagged_count,
    skipped_count: skipped_count,
    messages: messages
  )
end