Class: Edi::Menard::DiscontinueFlowProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/menard/discontinue_flow_processor.rb

Overview

Picks up Menard catalog items in pending_discontinue state that have not
yet been notified (pending_discontinue_date IS NULL), sends advance email
to Menard contacts, and sets pending_discontinue_date for maintenance
(transition to discontinued after the grace period).

Unlike Amazon/Walmart processors, this does NOT transition to discontinued.
The nightly Maintenance::ItemMaintenance job handles that transition after
the orchestrator's pending_discontinue_lifetime (1 week) has elapsed.

Follows the established orchestrator pattern:
Worker -> BaseOrchestrator.execute_flow -> Orchestrator -> Processor

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

#load_pending_discontinue_itemsObject



32
33
34
35
36
37
38
# File 'app/services/edi/menard/discontinue_flow_processor.rb', line 32

def load_pending_discontinue_items
  orchestrator.customer.catalog
              .catalog_items
              .where(state: 'pending_discontinue')
              .where(pending_discontinue_date: nil)
              .includes(store_item: :item)
end

#processObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/edi/menard/discontinue_flow_processor.rb', line 17

def process
  catalog_items = load_pending_discontinue_items
  return :empty_set unless catalog_items.present?

  logger.info "Menard DiscontinueFlowProcessor: Found #{catalog_items.size} pending_discontinue items needing notification"

  catalog_items.find_each do |catalog_item|
    ErrorReporting.scoped(catalog_item_id: catalog_item.id) do
      process_catalog_item(catalog_item)
    end
  end

  :ok
end

#process_catalog_item(catalog_item) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/edi/menard/discontinue_flow_processor.rb', line 40

def process_catalog_item(catalog_item)
  discontinue_date = Date.current + orchestrator.pending_discontinue_lifetime
  model_number = catalog_item.third_party_sku.presence || catalog_item.store_item.item.sku

  logger.info "Menard DiscontinueFlowProcessor: Notifying for catalog item #{catalog_item.id} " \
              "(Model: #{model_number}), discontinue date: #{discontinue_date}"

  catalog_item.update!(pending_discontinue_date: discontinue_date)

  recipients = orchestrator.discontinue_notification_recipients
  InternalMailer.notify_menard_of_pending_discontinue(
    catalog_item,
    discontinue_date: discontinue_date,
    recipients: recipients
  ).deliver_later

  logger.info "Menard DiscontinueFlowProcessor: Notification queued for catalog item #{catalog_item.id}, " \
              "recipients: #{recipients.join(', ')}"
rescue StandardError => e
  logger.error "Menard DiscontinueFlowProcessor: Exception processing catalog item #{catalog_item.id}: #{e.class} - #{e.message}"
  ErrorReporting.error(e, catalog_item_id: catalog_item.id, partner: orchestrator.partner)
end