Class: Edi::MiraklSeller::ProductImportMessageProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/mirakl_seller/product_import_message_processor.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

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



61
62
63
64
# File 'app/services/edi/mirakl_seller/product_import_message_processor.rb', line 61

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



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

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')
      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
        error_lines = json_hash.dig('transform_lines_in_error')
        has_error_report = json_hash.dig('has_error_report')
        warning_lines = json_hash.dig('transform_lines_with_warning')
        import_status = json_hash.dig('import_status')
        ecl.update(notes: [ecl.notes, JSON.pretty_generate(json_hash)].compact.join(" \n\n "))

        if import_status == 'SENT' && error_lines == 0 && warning_lines == 0
          # Success
          ecl.complete!
        elsif error_lines > 0 || has_error_report
          # Failure. Get the error file and attach it to the ecl 
          error_res = transport.send_data('', "#{orchestrator.product_import_message_remote_path}/#{ecl.transaction_id}/transformation_error_report", 'GET')
          error_file = StringIO.new(error_res[:http_result])
          error_xml = Nokogiri::XML(error_file)
          error_xml.xpath('//product').each do |product_xml|
            product_sku = product_xml.search("code[text()=#{orchestrator.error_product_identifier}]").first.next_element.text
            error_message = product_xml.xpath('errors')&.first&.content
            ecl.update(notes: [ecl.notes, "PRODUCT: #{product_sku}, ERRORS: #{error_message}"].compact.join("\n"))
          end
          ecl.error!
        elsif warning_lines > 0
          ecl.update(notes: [ecl.notes, "Product upload has #{warning_lines} warnings"].compact.join(" \n "))
          ecl.error!
        elsif (Time.current > timeout_time=(ecl.transmit_datetime + orchestrator.failure_timeout_in_minutes.minutes))
          ecl.update(notes: [ecl.notes, "Timed out after #{orchestrator.failure_timeout_in_minutes} minutes"].compact.join(" \n "))
          ecl.error!
        elsif ecl.process_attempts > orchestrator.max_process_attempts
          ecl.update(notes: [ecl.notes, "Reached max number of processing attemps. #{orchestrator.max_process_attempts} attempts"].compact.join(" \n "))
          ecl.error!
        else 
          ecl.process_attempts ||= 0
          ecl.process_attempts += 1
          ecl.save!
        end
      else
        ecl.error
      end
    end
  end
end