Class: Order::SendAbandonedCartEmails

Inherits:
BaseService show all
Defined in:
app/services/order/send_abandoned_cart_emails.rb

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

#initializeSendAbandonedCartEmails

Returns a new instance of SendAbandonedCartEmails.



3
4
5
6
7
# File 'app/services/order/send_abandoned_cart_emails.rb', line 3

def initialize
  @first_reminder_template = EmailTemplate.find_by(system_code: "ABANDONED_CART")
  @second_reminder_template = EmailTemplate.find_by(system_code: "ABANDONED_CART_REM")
  @third_reminder_template = EmailTemplate.find_by(system_code: "ABANDONED_CART_REM_2")
end

Instance Method Details

#processObject



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/order/send_abandoned_cart_emails.rb', line 9

def process
  problems = []
  all_abandoned_orders = Query::OrderQuery.new.abandoned_carts + Query::OrderQuery.new.abandoned_carts(reminded=true)
  all_abandoned_orders.each do |order|
    need_to_remind = false #Flag to know when to send the reminder

    # Check which reminder needs to be sent
    case order.abandoned_cart_reminder
    when 'reminder_not_sent'
      #This order hasn't been reminded yet, so we send the first reminder
      if order.communications.where(email_template_id: @first_reminder_template.id).any?
        problems << {order: order, error: 'Reminder already sent'}
        next
      end
      need_to_remind = true
      template_code = "ABANDONED_CART"
      column_to_update = "first_reminder"
    when 'first_reminder'
      #This order had the first reminder sent at some point. Now we check if the conditions are met to send the second reminder
      if order.communications.where(email_template_id: @second_reminder_template.id).any?
        problems << {order: order, error: 'Second reminder already sent'}
        next
      end
      cc = order.communications.where(email_template_id: @first_reminder_template.id)&.first #Grab the first reminder sent

      #We send a reminder after 24 hours of the first one, and we disregard old orders that never got the second reminder sent
      need_to_remind = true if cc.present? && cc.created_at > 24.hours.ago && cc.created_at < 7.days.ago
      template_code = "ABANDONED_CART_REM"
      column_to_update = "second_reminder"
    when 'second_reminder'
      #This order had the second reminder sent at some point. Now we check if the conditions are met to send the third reminder
      if order.communications.where(email_template_id: @third_reminder_template.id).any?
        problems << {order: order, error: 'Third reminder already sent'}
        next
      end
      cc = order.communications.where(email_template_id: @second_reminder_template.id)&.first #Grab the first reminder sent

      #We send a reminder after 24 hours of the first one, and we disregard old orders that never got the second reminder sent
      need_to_remind = true if cc.present? && cc.created_at > 48.hours.ago && cc.created_at < 7.days.ago
      template_code = "ABANDONED_CART_REM_2"
      column_to_update = "third_reminder"
    end

    next unless need_to_remind

    acc = order.customer.
    if acc.nil?
      res = order.customer.
      if res[:success] == true
        acc = res[:account]
      end
    end
    next if acc.nil?

    co = CommunicationBuilder.new(
      resource: order,
      sender: INFO_EMAIL,
      recipient_party: order.customer,
      emails: acc.,
      recipient_name: order.customer.guest? ? nil : order.customer.name,
      template_system_code: template_code,
      merge_options: {order: order.to_liquid}).create
    order.update_column('abandoned_cart_reminder',  column_to_update)
  end
  InternalMailer.abandoned_cart_problems_alert(problems).deliver_later if problems.any?
  return true
end