Class: Edi::MiraklSeller::ProductDataProcessor

Inherits:
BaseEdiService show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/services/edi/mirakl_seller/product_data_processor.rb

Overview

Service object: product data 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, #process, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#call(_catalog_items: nil, _states: 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
# File 'app/services/edi/mirakl_seller/product_data_processor.rb', line 8

def call(_catalog_items: nil, _states: nil)
  ecl = nil
  data_result = nil
  EdiCommunicationLog.transaction do
    logger.info "Sending product data for partner #{orchestrator.partner}"
    load_result = load_results
    items = load_result[:items]
    load_result[:catalog_items]

    data_result = orchestrator.product_data_configurator.build_xml(items: items)
    data_xml = data_result.xml

    # Always emit the ECL for the items that mapped so the sender has
    # something to upload; unmappable items are dropped, not the whole feed.
    ecl = EdiCommunicationLog.create_outbound_file_from_data(
      data: data_xml,
      file_extension: 'xml',
      file_name: "wy-api-product-export-#{Time.zone.now.strftime('%Y%m%d%H%M%S')}.xml",
      partner: orchestrator.partner,
      category: 'product_data',
      data_type: 'xml',
      file_info: {}
    )

    # Intentional category skips (accessories, unmapped lines) are expected
    # on every run — log them, don't alarm anyone.
    logger.info("[#{orchestrator.partner}] product_data excluded #{data_result.skipped.size} unmapped item(s): #{data_result.skipped.join('; ')}") if data_result.skipped.present?
  end

  # Only a mapped product missing a *required* field is a genuine problem
  # (Mirakl will reject that product) — that's what warrants the alert.
  # Deliver after commit so a mail failure cannot roll back the feed ECL.
  notify_errors(data_result)
  ecl
end

#load_results(_states: nil) ⇒ Object



44
45
46
47
48
49
# File 'app/services/edi/mirakl_seller/product_data_processor.rb', line 44

def load_results(_states: nil)
  catalog_items = CatalogItem.for_orchestrator_keys(orchestrator.config[:orchestrator_keys])
  items = Item.where.not(upc: [nil, '']).joins(:catalog_items).merge(catalog_items).distinct
  { items: items, catalog_items: catalog_items }
  # CatalogItem.where(id: 46152) #This is a test catalog item
end