Class: Customer::CustomerAnniversaryMessage
- Inherits:
-
BaseService
- Object
- BaseService
- Customer::CustomerAnniversaryMessage
- Defined in:
- app/services/customer/customer_anniversary_message.rb
Overview
Service object: customer anniversary message.
Constant Summary collapse
- ACTIVITY_TYPE_ANNIVERSARY =
Activity type anniversary.
'ANNIVERSARY'.freeze
- EMAIL_TEMPLATE_SYSTEM_CODE =
Email template system code.
'ANNIVERSARY25OFF'.freeze
Instance Attribute Summary collapse
-
#customer_filter ⇒ Object
readonly
Returns the value of attribute customer_filter.
-
#email_category ⇒ Object
readonly
Returns the value of attribute email_category.
-
#email_template ⇒ Object
readonly
Returns the value of attribute email_template.
Attributes inherited from BaseService
Instance Method Summary collapse
- #create_anniversary_activity(customer_record) ⇒ Object
- #create_anniversary_communication(customer_record) ⇒ Object
-
#initialize(options = {}) ⇒ CustomerAnniversaryMessage
constructor
A new instance of CustomerAnniversaryMessage.
- #load_overdue_customer_records(due_on_or_before: Date.current, limit: nil) ⇒ Object
- #populate_first_anniverary ⇒ Object
- #process(due_on_or_before: Date.current, limit: nil) ⇒ Object
- #schedule_next_anniversary(customer_record) ⇒ Object
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ CustomerAnniversaryMessage
Returns a new instance of CustomerAnniversaryMessage.
13 14 15 16 17 18 |
# File 'app/services/customer/customer_anniversary_message.rb', line 13 def initialize( = {}) @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_filter ⇒ Object (readonly)
Returns the value of attribute customer_filter.
9 10 11 |
# File 'app/services/customer/customer_anniversary_message.rb', line 9 def customer_filter @customer_filter end |
#email_category ⇒ Object (readonly)
Returns the value of attribute email_category.
9 10 11 |
# File 'app/services/customer/customer_anniversary_message.rb', line 9 def email_category @email_category end |
#email_template ⇒ Object (readonly)
Returns the value of attribute email_template.
9 10 11 |
# File 'app/services/customer/customer_anniversary_message.rb', line 9 def email_template @email_template end |
Instance Method Details
#create_anniversary_activity(customer_record) ⇒ Object
55 56 57 58 59 |
# File 'app/services/customer/customer_anniversary_message.rb', line 55 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
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/services/customer/customer_anniversary_message.rb', line 61 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 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 end |
#load_overdue_customer_records(due_on_or_before: Date.current, limit: nil) ⇒ Object
74 75 76 77 78 |
# File 'app/services/customer/customer_anniversary_message.rb', line 74 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_anniverary ⇒ Object
86 87 88 89 90 |
# File 'app/services/customer/customer_anniversary_message.rb', line 86 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
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 |
# File 'app/services/customer/customer_anniversary_message.rb', line 20 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 break if limit && count > limit 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
80 81 82 83 84 |
# File 'app/services/customer/customer_anniversary_message.rb', line 80 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 |