Class: Edi::Walmart::FeedMessageSender

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/walmart/feed_message_sender.rb

Constant Summary

Constants included from AddressAbbreviator

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 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

#ecl_in_queueObject



48
49
50
51
52
53
# File 'app/services/edi/walmart/feed_message_sender.rb', line 48

def ecl_in_queue
  EdiCommunicationLog.requiring_processing
                     .where(partner: orchestrator.partner)
                     .where(category: feed_category)
                     .order(:created_at)
end

#instantiate_transporter(transporter, transporter_profile = nil, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
# File 'app/services/edi/walmart/feed_message_sender.rb', line 55

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

#process(edi_communication_logs = nil) ⇒ Object

This process is the same for any feed, but in our case we start with implementing inventory



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

def process(edi_communication_logs = nil)
  feed_transport = instantiate_transporter(orchestrator.transporter, orchestrator.transporter_profile)

  edi_communication_logs ||= ecl_in_queue
  [edi_communication_logs].flatten.each do |ecl|
    logger.info "Sending feed data to #{orchestrator.partner}"
    query_char = (query_param.present? ? '&' : '')
    query_str = "?#{query_param}#{query_char}feedType=#{feed_type}"
    res = feed_transport.send_data(ecl.data, "#{orchestrator.feed_message_remote_path}#{query_str}", 'POST')
    # Read the response body once (HTTP::Response::Body can only be consumed once)
    response_body = res[:http_result]&.body.to_s
    ecl.notes = "HTTP CODE: #{res[:http_result]&.code}, HTTP BODY: #{response_body}, HTTP METHOD: 'POST', Timestamp: #{Time.current.to_datetime.to_fs(:crm_default)}"
    logger.info "Result: HTTP CODE: #{res[:http_result]&.code}, HTTP BODY: #{response_body}"
    if res[:success] && response_body.present?
      json_hash = JSON.parse(response_body).with_indifferent_access
      res_feed_id = json_hash[:feedId]
      feed_status = json_hash[:feedStatus]
      
      # Check for ingestion errors even if HTTP status is 200
      if feed_status == 'ERROR' || json_hash[:ingestionErrors].present?
        ingestion_errors = json_hash.dig(:ingestionErrors, :ingestionError) || []
        error_messages = ingestion_errors.map { |e| "#{e[:code]}: #{e[:description]}" }.join('; ')
        logger.error "[Walmart FeedSender] Feed submission returned ERROR status: #{error_messages}"
        ecl.notes = "#{ecl.notes}\nFEED STATUS: #{feed_status}, ERRORS: #{error_messages}"
        ecl.error
      else
        ecl.transaction_id = res_feed_id
        ecl.transmit_datetime = Time.current
        ecl.start_process!
        logger.info "[Walmart FeedSender] Feed submitted successfully, feedId: #{res_feed_id}, status: #{feed_status}"
      end
    else
      ecl.error
    end
  end
  edi_communication_logs
end