Class: Edi::AmazonVc::TransactionMessageProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/amazon_vc/transaction_message_processor.rb

Constant Summary

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

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'app/services/edi/amazon_vc/transaction_message_processor.rb', line 34

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

#process(edi_communication_logs = nil) ⇒ Object



5
6
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
# File 'app/services/edi/amazon_vc/transaction_message_processor.rb', line 5

def process(edi_communication_logs = nil)
  edi_communication_logs ||= EdiCommunicationLog
                             .where(state: 'processing')
                             .where(partner: orchestrator.partner)
                             .where.not(transaction_id: nil)
                             .order(:created_at)
  [edi_communication_logs].flatten.each do |ecl|
    logger.info "EdiCommunicationLog:#{ecl.id} - Retrieving API transaction result from #{orchestrator.partner} for transaction id: #{ecl.transaction_id}"
    begin
      transport = instantiate_transporter(orchestrator.transporter, orchestrator.transporter_profile)
      res = transport.send_data('', "#{orchestrator.transaction_message_remote_path}/#{ecl.transaction_id}", 'GET')
      ecl.update(notes: [ecl.notes, "HTTP CODE: #{res[:http_result]&.code}, HTTP BODY: #{res[:http_result]&.body}, Timestamp: #{(Time.current.to_datetime).to_fs(:crm_default)}"].compact.join(" | "))
      logger.info "Result: HTTP CODE: #{res[:http_result]&.code}, HTTP BODY: #{res[:http_result]&.body}"
      if res[:success] && (data = res[:http_result]&.body.to_s).present?
        json_hash = JSON.parse(data).with_indifferent_access
        status = json_hash[:payload].dig('transactionStatus', 'status')
        timeout_time = ecl.transmit_datetime + orchestrator.failure_timeout_in_minutes.minutes
        if status == 'Success'
          ecl.complete!
        elsif (status == 'Failure' || (Time.current > timeout_time))
          ecl.update(notes: [ecl.notes, "Timed out after #{orchestrator.failure_timeout_in_minutes} minutes"].compact.join(" | ")) if (Time.current > timeout_time)
          ecl.error!
        end
      else
        ecl.error
      end
    end
  end
end