Class: GmailSignaturePusher

Inherits:
Object
  • Object
show all
Defined in:
app/services/gmail_signature_pusher.rb

Defined Under Namespace

Classes: BatchResult, Result

Constant Summary collapse

GMAIL_SCOPE =
'https://www.googleapis.com/auth/gmail.settings.basic'
DOMAIN =
'@warmlyyours.com'

Instance Method Summary collapse

Constructor Details

#initializeGmailSignaturePusher

Returns a new instance of GmailSignaturePusher.



26
27
28
29
# File 'app/services/gmail_signature_pusher.rb', line 26

def initialize
  @credentials_email = Heatwave::Configuration.fetch(:google, :gmail_signatures, :client_email)
  @credentials_key   = Heatwave::Configuration.fetch(:google, :gmail_signatures, :private_key)
end

Instance Method Details

#gmail_address(employee) ⇒ Object



59
60
61
62
63
# File 'app/services/gmail_signature_pusher.rb', line 59

def gmail_address(employee)
  emails = employee.contact_points.select { |cp| cp.category == ContactPoint::EMAIL }
  wy_email = emails.find { |cp| cp.detail&.downcase&.end_with?(DOMAIN) }
  wy_email&.detail&.downcase
end

#has_signature?(employee) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'app/services/gmail_signature_pusher.rb', line 65

def has_signature?(employee)
  employee.employee_record&.signature.present? ||
    employee.effective_scheduler_link.present?
end

#push_all(employees = nil) ⇒ Object



53
54
55
56
57
# File 'app/services/gmail_signature_pusher.rb', line 53

def push_all(employees = nil)
  employees ||= Employee.active_employees.sorted.includes(:employee_record, :contact_points)
  results = employees.map { |emp| push_one(emp) }
  BatchResult.new(results: results)
end

#push_one(employee) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/gmail_signature_pusher.rb', line 31

def push_one(employee)
  email = gmail_address(employee)
  return Result.new(
    employee_id: employee.id,
    employee_name: employee.full_name,
    email: email || 'N/A',
    error: 'No @warmlyyours.com email found'
  ) unless email

  html = render_signature(employee)

  service = build_gmail_service(email)
  send_as = Google::Apis::GmailV1::SendAs.new(signature: html)
  service.patch_user_setting_send_as('me', email, send_as)

  Result.new(employee_id: employee.id, employee_name: employee.full_name, email: email, success: true)
rescue Google::Apis::Error => e
  Result.new(employee_id: employee.id, employee_name: employee.full_name, email: email, error: "Gmail API: #{e.message}")
rescue StandardError => e
  Result.new(employee_id: employee.id, employee_name: employee.full_name, email: email.to_s, error: e.message)
end