Class: Edi::Amazon::OrderStatusVerifier

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

Overview

Verifies that recently-invoiced Heatwave orders have reached the expected
"Shipped" status on Amazon Seller Central.

Uses the SP-API Orders v0 getOrders endpoint to pull order status in batch:
GET /orders/v0/orders?MarketplaceIds=id&AmazonOrderIds=id1,id2,...
See: https://developer-docs.amazon.com/sp-api/docs/orders-api-v0-reference#getorders

Acceptable Amazon OrderStatus values for a fulfilled order:

  • Shipped (standard MFN fulfillment confirmed)
  • InvoiceUnconfirmed (some EU marketplaces; invoice not yet processed by Amazon)

All other statuses are flagged as mismatches, including:

  • Unshipped / PartiallyShipped — ship-confirm didn't land (e.g. multibox-kit bug)
  • Canceled — Amazon canceled but we already invoiced (revenue/inventory issue)

Defined Under Namespace

Classes: Mismatch, Result

Constant Summary collapse

ACCEPTABLE_AMAZON_STATUSES =
%w[Shipped InvoiceUnconfirmed].freeze
BATCH_SIZE =
50
CONFIRM_GRACE_PERIOD =

Minimum age after the last ship-confirm ECL before we flag a mismatch.
Prevents false positives for orders just confirmed moments ago.

1.hour

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

#processObject



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
# File 'app/services/edi/amazon/order_status_verifier.rb', line 37

def process
  orders = candidate_orders
  return Result.new(partner: orchestrator.partner, checked_count: 0) if orders.empty?

  orders_by_po = orders.index_by(&:edi_po_number)
  mismatches = []
  errors = []
  checked = 0

  orders_by_po.keys.each_slice(BATCH_SIZE) do |po_batch|
    amazon_statuses = fetch_amazon_statuses(po_batch)

    if amazon_statuses.nil?
      errors << "SP-API request failed for batch starting with #{po_batch.first}"
      next
    end

    po_batch.each do |po|
      order = orders_by_po[po]
      amazon_status = amazon_statuses[po]
      checked += 1

      if amazon_status.nil?
        errors << "No Amazon status returned for #{po} (#{order.reference_number})"
        next
      end

      next if ACCEPTABLE_AMAZON_STATUSES.include?(amazon_status)

      mismatches << Mismatch.new(
        order: order,
        amazon_order_id: po,
        amazon_status: amazon_status,
        heatwave_state: order.state
      )
    end
  end

  result = Result.new(
    partner: orchestrator.partner,
    checked_count: checked,
    mismatches: mismatches,
    errors: errors
  )

  notify(result) unless result.ok?

  result
end