Class: Customer::CustomerAnniversaryMessage

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/customer/customer_anniversary_message.rb

Constant Summary collapse

ACTIVITY_TYPE_ANNIVERSARY =
'ANNIVERSARY'
EMAIL_TEMPLATE_SYSTEM_CODE =
'ANNIVERSARY25OFF'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

#initialize(options = {}) ⇒ CustomerAnniversaryMessage

Returns a new instance of CustomerAnniversaryMessage.



8
9
10
11
12
13
# File 'app/services/customer/customer_anniversary_message.rb', line 8

def initialize(options={})
  @customer_filter = CustomerFilter.find(336) #All w/out DB/ETL/HD/COSTCO & Custom Pricing
  @email_template = EmailTemplate.find_by(system_code: EMAIL_TEMPLATE_SYSTEM_CODE)
  @email_category = @email_template.category
  super
end

Instance Attribute Details

#customer_filterObject (readonly)

Returns the value of attribute customer_filter.



6
7
8
# File 'app/services/customer/customer_anniversary_message.rb', line 6

def customer_filter
  @customer_filter
end

#email_categoryObject (readonly)

Returns the value of attribute email_category.



6
7
8
# File 'app/services/customer/customer_anniversary_message.rb', line 6

def email_category
  @email_category
end

#email_templateObject (readonly)

Returns the value of attribute email_template.



6
7
8
# File 'app/services/customer/customer_anniversary_message.rb', line 6

def email_template
  @email_template
end

Instance Method Details

#create_anniversary_activity(customer_record) ⇒ Object



52
53
54
55
56
# File 'app/services/customer/customer_anniversary_message.rb', line 52

def create_anniversary_activity(customer_record)
  logger.info "Creating anniversary activity for #{customer_record.party.full_name}"
  description = "WarmlyYours Customer Anniversary Date #{customer_record.next_anniversary_date}"
  customer_record.party.create_activity(ACTIVITY_TYPE_ANNIVERSARY, description: description)
end

#create_anniversary_communication(customer_record) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/customer/customer_anniversary_message.rb', line 58

def create_anniversary_communication(customer_record)
  logger.info "Creating anniversary communication for #{customer_record.party.full_name}"
  sender_party = Employee.find_by(full_name: "Julia Billen") || customer_record.party.primary_sales_rep
  comm = CommunicationBuilder.new(
    sender_party: sender_party,
    logger: logger,
    recipient_party: customer_record.party,
    template: email_template,
    transmit_at: 1.hour.from_now,
    use_best_email: true
  ).create
  comm
end

#load_overdue_customer_records(due_on_or_before: Date.current, limit: nil) ⇒ Object



72
73
74
75
76
# File 'app/services/customer/customer_anniversary_message.rb', line 72

def load_overdue_customer_records(due_on_or_before: Date.current, limit: nil)
  records = CustomerRecord.joins(customer: :primary_sales_rep).merge(Customer.organizations.where(Customer[:state].eq('customer'))).where(CustomerRecord[:next_anniversary_date].lteq(due_on_or_before))
  records = records.limit(limit) if limit.present?
  records
end

#populate_first_anniveraryObject



84
85
86
87
88
# File 'app/services/customer/customer_anniversary_message.rb', line 84

def populate_first_anniverary
  CustomerRecord.where.not(customer_since:nil).where(next_anniversary_date: nil).find_each do |cr|
    cr.update_column(:next_anniversary_date, cr.next_anniversary)
  end
end

#process(due_on_or_before: Date.current, limit: nil) ⇒ Object



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
# File 'app/services/customer/customer_anniversary_message.rb', line 15

def process(due_on_or_before: Date.current, limit: nil)
  # Find all anniversary and schedule a message
  communications = []
  activities = []
  records = load_overdue_customer_records(due_on_or_before: due_on_or_before)
  logger.info "Loaded #{records.size} records from db"
  count = 0
  filtered_records = []
  records.each do |cr|
    if customer_filter.applies_to_customer?(cr.party)
      filtered_records << cr
      count += 1
    else
      logger.info "Customer Record excluded due to customer filter exclusion"
    end
    if limit && count > limit
      break
    end
  end
  logger.info "#{filtered_records.size} remain after customer filter applied"
  Communication.transaction do
    filtered_records.each do |cr|
      # Create a Praise
      best_email = cr.customer.email
      # No need to create a communication if there's no email or if that email will be suppressed
      if best_email && EmailPreference.can_receive_email_of_category(best_email, email_category)
        communications << create_anniversary_communication(cr)
      else
        logger.info "#{best_email} is marked suppressed for email category #{email_category}"
      end
      activities << create_anniversary_activity(cr)
      schedule_next_anniversary(cr)
    end
  end
  { activities: activities, communications: communications }
end

#schedule_next_anniversary(customer_record) ⇒ Object



78
79
80
81
82
# File 'app/services/customer/customer_anniversary_message.rb', line 78

def schedule_next_anniversary(customer_record)
  customer_record.next_anniversary_date = customer_record.next_anniversary_date.next_year
  logger.info "Advancing customer anniversary to #{customer_record.next_anniversary_date} for #{customer_record.party.full_name}"
  customer_record.save!
end