Class: Shipping::PostShippingInstruction

Inherits:
BaseService show all
Defined in:
app/services/shipping/post_shipping_instruction.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

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 BaseService

Instance Method Details

#find_assortment_instructions(order) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/shipping/post_shipping_instruction.rb', line 40

def find_assortment_instructions(order)
  assortment_instructions = []
  # extract our line items once
  line_items = order.line_items.active_parent_lines
  AssortmentInstruction.active.customer_shipping_notice.for_customer(order.customer).each do |assortment_instruction|
    if assortment_instruction.line_items_qualify?(line_items)
      assortment_instructions << assortment_instruction
    end
    break if assortment_instruction.final_rule
  end
  assortment_instructions
end

#process(order) ⇒ Object



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
# File 'app/services/shipping/post_shipping_instruction.rb', line 7

def process(order)
  unless order.is_regular_order?
    logger.info("Shipping Notification does not handle non regular order, Order #{order.id} skipped")
    return Result.new(status: :skipped)
  end
  # Who receives?
  recipient_party = order.primary_party || order.customer
  recipient_emails = [recipient_party.email].compact
  recipient_emails += order.tracking_email
  return Result.new(status: :skipped, message: 'No emails found for order/party') if recipient_emails.empty?
  communications = []
  find_assortment_instructions(order).each do |ai|
    # Check if we already sent this template for this order to prevent duplicates
    existing = Communication.where(
      resource_type: 'Order',
      resource_id: order.id,
      email_template_id: ai.email_template_id
    ).exists?

    next if existing # Skip if already sent

    cb = CommunicationBuilder.new(resource: order,
                             sender_party: order.primary_sales_rep,
                             emails: recipient_emails.uniq.join(','),
                             recipient_party: recipient_party,
                             email_template_id: ai.email_template_id,
                             upload_ids: ai.items.map(&:literature_id))
    communications << cb.create
  end
  Result.new(communications: communications, status: :ok)
end