Class: Employee::WorkAnniversaryMessage

Inherits:
BaseService show all
Defined in:
app/services/employee/work_anniversary_message.rb

Overview

Service object: work anniversary message.

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#create_praise(employee_record) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/employee/work_anniversary_message.rb', line 17

def create_praise(employee_record)
  return if employee_record.years_at_work == 0

  logger.info "Creating work anniversary praise for #{employee_record.employee.full_name}"
  p = nil
  Praise.transaction do
    p = Praise.create!(
      employee_records: [employee_record],
      praise_type: "work_anniversary",
      headline: "Happy #{employee_record.years_at_work.ordinalize} Anniversary #{employee_record.employee.first_name}!",
      praise: "Happy #{employee_record.years_at_work.ordinalize} Anniversary From Your WarmlyYours Family!",
      praise_date: Date.current, # employee_record.next_birthday
      image_url: get_badge_attachment,
      notify: true,
      notify_staff: true
    )
    p.publish
  end
  p
end

#get_badgeObject



47
48
49
50
# File 'app/services/employee/work_anniversary_message.rb', line 47

def get_badge
  # https://ik.warmlyyours.com/img/birthday-badge-9c349d.png
  Image.find_by(title: 'Anniversary Badge')&.attachment
end

#get_badge_attachmentObject



38
39
40
41
# File 'app/services/employee/work_anniversary_message.rb', line 38

def get_badge_attachment
  # https://crm.warmlyyours.com/en-US/images/6266
  Image.find_by(title: 'Anniversary Badge').image_url(width: 300, encode_format: :jpeg)
end

#load_overdue_employee_recordsObject



43
44
45
# File 'app/services/employee/work_anniversary_message.rb', line 43

def load_overdue_employee_records
  EmployeeRecord.active_employees.where(EmployeeRecord[:next_work_anniversary].lteq(Date.current)).where(disable_special_events_alerts: false)
end

#processObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/services/employee/work_anniversary_message.rb', line 4

def process
  # Find all past birthdays or present and schedule a message
  praises = []
  records = load_overdue_employee_records
  logger.info "No work anniversaries to process" if records.blank?
  records.each do |er|
    # Create a Praise
    praises << create_praise(er)
    schedule_next_work_anniversary(er)
  end
  praises
end

#schedule_next_work_anniversary(employee_record) ⇒ Object



52
53
54
55
56
57
# File 'app/services/employee/work_anniversary_message.rb', line 52

def schedule_next_work_anniversary(employee_record)
  next_year = (Date.current + 1.year).year
  employee_record.next_work_anniversary = employee_record.next_work_anniversary.change(year: next_year)
  logger.info "Advancing work anniversary to #{employee_record.next_work_anniversary} for #{employee_record.employee.full_name}"
  employee_record.save!
end