Class: Edi::Walmart::OrderRetriever

Inherits:
Retriever show all
Defined in:
app/services/edi/walmart/order_retriever.rb

Overview

Retrieves orders from Walmart Marketplace API and stores them in EdiCommunicationLog
See: https://developer.walmart.com/us-marketplace/docs/order-management-api-overview

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

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



84
85
86
87
88
89
90
91
# File 'app/services/edi/walmart/order_retriever.rb', line 84

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :http_walmart_seller_api
    Transport::HttpWalmartSellerApiConnection.new(profile: transporter_profile)
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#process(partner:, category:, transporter: :http_walmart_seller_api, transporter_profile: nil, remote_path: nil, data_type: 'json', orchestrator:, store_to_upload: false, file_info: {}) ⇒ Object



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
# File 'app/services/edi/walmart/order_retriever.rb', line 7

def process(partner:, category:, transporter: :http_walmart_seller_api,
            transporter_profile: nil,
            remote_path: nil,
            data_type: 'json',
            orchestrator:,
            store_to_upload: false,
            file_info: {})

  Rails.logger.debug { "Edi::Walmart::OrderRetriever, remote_path: #{remote_path}" }
  transport = instantiate_transporter(transporter, transporter_profile)
  res = transport.send_data('', remote_path, 'GET')
  Rails.logger.debug { "Edi::Walmart::OrderRetriever, res: #{res}" }
  edi_logs = []

  EdiCommunicationLog.transaction do
    if res[:success] && (data = res[:http_result]&.body.to_s).present?
      json_hash = JSON.parse(data).with_indifferent_access
      Rails.logger.debug { "Edi::Walmart::OrderRetriever, json_hash keys: #{json_hash.keys}" }

      # Walmart returns orders in the 'list' > 'elements' structure
      # See: https://developer.walmart.com/us-marketplace/docs/get-all-orders
      # {
      #   "list": {
      #     "meta": { "totalCount": 100, "limit": 200, "nextCursor": "..." },
      #     "elements": {
      #       "order": [{ ... }, { ... }]
      #     }
      #   }
      # }
      orders = json_hash.dig(:list, :elements, :order) || []

      if orders.any?
        # For each order, we already have all the data we need in one call
        # (unlike Amazon which requires separate calls for address and items)
        data_blob = json_hash.to_json
        edi_log = EdiCommunicationLog.new(
          partner: partner,
          category: category,
          data: data_blob,
          data_type: data_type,
          transmit_datetime: Time.current,
          file_info: file_info.merge(orders_count: orders.size)
        )
        edi_log.save!
        logger.info "Walmart orders batch saved to edi log #{edi_log.id}, #{orders.size} orders"
        edi_logs << edi_log

        # Handle pagination if there's a next cursor
        next_cursor = json_hash.dig(:list, :meta, :nextCursor)
        if next_cursor.present?
          # Recursively fetch the next page
          next_remote_path = build_next_page_url(remote_path, next_cursor)
          next_logs = process(
            partner: partner,
            category: category,
            transporter: transporter,
            transporter_profile: transporter_profile,
            remote_path: next_remote_path,
            data_type: data_type,
            orchestrator: orchestrator,
            store_to_upload: store_to_upload,
            file_info: file_info
          )
          edi_logs.concat(next_logs)
        end
      else
        Rails.logger.info "Edi::Walmart::OrderRetriever - No orders found"
      end
    else
      Rails.logger.warn "Edi::Walmart::OrderRetriever - API call failed or empty response"
    end
  end

  Rails.logger.debug { "Edi::Walmart::OrderRetriever, edi_logs: #{edi_logs.map(&:id)}" }
  edi_logs
end