Class: Edi::AmazonVc::Sender

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

Direct Known Subclasses

ListingMessageSender

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



29
30
31
32
33
34
35
36
# File 'app/services/edi/amazon_vc/sender.rb', line 29

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(partner:, category:, transporter: :http_seller_api, transporter_profile: nil, data_type: 'xml', remote_path: nil, http_method: 'POST', edi_communication_logs: nil) ⇒ Object

What type of transporter?



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/edi/amazon_vc/sender.rb', line 3

def process(partner:, category:, transporter: :http_seller_api, # What type of transporter?
            transporter_profile: nil,
            data_type: 'xml',
            remote_path: nil,
            http_method: 'POST', # we are patching this to allow a PATCH with mode=VALIDATION_PREVIEW
            edi_communication_logs: nil) # Which category to send, e.g order_confirm
  transport = instantiate_transporter(transporter, transporter_profile)
  edi_communication_logs ||= EdiCommunicationLog.requiring_processing.where(partner:).where(category:).order(:created_at)
  [edi_communication_logs].flatten.each do |ecl|
    logger.info "Sending #{category} data #{ecl.data} to Amazon using transporter: #{transporter} using profile #{transporter_profile}"
    res = transport.send_data(ecl.data, remote_path, http_method)
    ecl.transmit_datetime = Time.current
    data = res[:http_result]&.body.to_s
    ecl.notes = "Timestamp: #{Time.current.to_datetime.to_fs(:crm_default)}\nREMOTE_PATH: #{remote_path}\nHTTP CODE: #{res[:http_result]&.code}\nHTTP METHOD: #{http_method}\nATTEMPT NUMBER: #{res[:attempt_number_reached]}\nHTTP BODY: #{data}"
    json_hash = JSON.parse(data)&.with_indifferent_access
    ecl.transaction_id = json_hash&.dig('submissionId')
    logger.info "Result: HTTP CODE: #{res[:http_result]&.code}, HTTP BODY: #{res[:http_result]&.body}"
    if res[:success] && %w[ACCEPTED VALID].include?(json_hash&.dig('status').to_s.upcase)
      ecl.complete!
    else
      ecl.error
    end
  end
  edi_communication_logs
end