Class: Shipping::PostShippingInstruction

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

Overview

Service object: post shipping instruction.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Class Method Summary collapse

Instance Method Summary collapse

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 BaseService

Class Method Details

.call(order) ⇒ Object



10
# File 'app/services/shipping/post_shipping_instruction.rb', line 10

def self.call(order) = new.call(order)

Instance Method Details

#call(order) ⇒ Object



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

def call(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

#find_assortment_instructions(order) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/shipping/post_shipping_instruction.rb', line 45

def find_assortment_instructions(order)
  assortment_instructions = []
  # extract our line items once
  line_items = order.line_items.active_parent_lines
  AssortmentInstruction.active.with_active_email_template.customer_shipping_notice.for_customer(order.customer).each do |assortment_instruction|
    next unless assortment_instruction.line_items_qualify?(line_items)

    assortment_instructions << assortment_instruction
    break if assortment_instruction.final_rule
  end
  assortment_instructions
end