Class: GmailSignaturePusher

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

Overview

Service object: gmail signature pusher.

Defined Under Namespace

Classes: BatchResult, Result

Constant Summary collapse

GMAIL_SCOPE =

Gmail scope.

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

Domain.

'@warmlyyours.com'

Instance Method Summary collapse

Constructor Details

#initializeGmailSignaturePusher

Returns a new instance of GmailSignaturePusher.



31
32
33
34
# File 'app/services/gmail_signature_pusher.rb', line 31

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



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

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)


72
73
74
75
# File 'app/services/gmail_signature_pusher.rb', line 72

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

#push_all(employees = nil) ⇒ Object



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

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/gmail_signature_pusher.rb', line 36

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

  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