Class: Edi::MiraklSeller::ProductImportMessageProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/mirakl_seller/product_import_message_processor.rb

Overview

Service object: product import message processor.

Constant Summary

Constants included from RequestIdentifiable

RequestIdentifiable::REQUEST_ID_HEADERS

Constants included from AddressAbbreviator

AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseEdiService

#amazon_feed_product_type, #duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #onboard_ordered_catalog_items, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from RequestIdentifiable

#partner_request_id

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, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#instantiate_transporter(_transporter, transporter_profile = nil) ⇒ Object



47
48
49
50
# File 'app/services/edi/mirakl_seller/product_import_message_processor.rb', line 47

def instantiate_transporter(_transporter, transporter_profile = nil)
  # It doesn't matter what we get in the params, mirakl only has 1 transporter
  Transport::MiraklTransporter.new({ profile: transporter_profile })
end

#process(edi_communication_logs = nil) ⇒ Object



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
# File 'app/services/edi/mirakl_seller/product_import_message_processor.rb', line 8

def process(edi_communication_logs = nil)
  edi_communication_logs ||= EdiCommunicationLog
                             .where(state: 'processing')
                             .where(category: 'product_data')
                             .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 Import result from #{orchestrator.partner} for import id: #{ecl.transaction_id}"
    begin
      transport = instantiate_transporter(orchestrator.transporter, orchestrator.transporter_profile)
      res = transport.send_data('', "#{orchestrator.product_import_message_remote_path}/#{ecl.transaction_id}", 'GET')
      http = res[:http_result]
      # Scrubbed at extraction: `present?` below raises on a UTF-8-tagged body
      # holding invalid bytes, and this ECL would then never leave `processing`.
      body = scrub_text(http&.body)
      # Persisted before the parse below, deliberately: if the body turns out not
      # to be JSON, this note is the only record of what Mirakl actually returned.
      append_note(ecl, "HTTP CODE: #{http&.status}, HTTP BODY: #{body}, Timestamp: #{Time.current.to_datetime.to_fs(:crm_default)}", separator: " | ")
      logger.info "Result: HTTP CODE: #{http&.status}, HTTP BODY: #{body}"

      if res[:success] && body.present?
        json_hash = JSON.parse(body).with_indifferent_access
        append_note(ecl, JSON.pretty_generate(json_hash), separator: " \n\n ")
        apply_import_result(ecl, json_hash, transport)
      else
        ecl.error!
      end
    rescue StandardError => e
      # This loop polls every Mirakl partner in one pass. Without a rescue an
      # unexpected payload raises out of `each` and leaves every partner after
      # this one stuck in `processing` until someone notices.
      logger.error "EdiCommunicationLog:#{ecl.id} - #{orchestrator.partner} import poll failed: #{e.message}"
      ErrorReporting.error(e, message: 'Mirakl product import poll failed', edi_communication_log_id: ecl.id, partner: orchestrator.partner)
      append_note(ecl, "Processing error: #{e.message}")
    end
  end
end