Class: Bob::Employees

Inherits:
API
  • Object
show all
Defined in:
lib/bob/api/employees.rb

Constant Summary collapse

DEFAULT_FIELDS =
['root.id', 'root.email', 'work.startDate', 'root.secondName', 'root.fullName', 'root.displayName',
'work.title', 'home.privateEmail', 'work.reportsToIdInCompany', 'address.city', 'work.reportsTo.email',
'work.manager', 'work.secondLevelManager', 'work.employeeIdInCompany', 'payroll.employment.type',
'address.country', 'address.usaState', 'internal.lifecycleStatus', 'work.department',
'about.socialData.linkedin'].freeze

Constants inherited from API

API::BASE_URL, API::SANDBOX_URL

Class Method Summary collapse

Methods inherited from API

build_url, create_csv, delete, get, headers, post, post_file, post_media, put

Class Method Details

.all_leavers(start_date:, end_date:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bob/api/employees.rb', line 47

def self.all_leavers(start_date:, end_date:)
  fields = ['internal.terminationDate', 'internal.status', 'root.id', 'root.displayName', 'work.title',
            'work.reportsTo.email', 'root.email']
  search({ humanReadable: 'replace', showInactive: true, fields: }).select do |employee|
    next unless employee.internal.status == 'Inactive' && employee.internal.termination_date.present?

    # Don't process employees that left before the period we are looking into
    internal_term_date = Date.parse(employee.internal.termination_date)
    next if internal_term_date.before?(start_date)

    # Need to fetch lifecycle statuses, as garden leavers have left before the actual internal term date
    lifecycle_statuses = Bob::Employee::LifecycleHistory.all(employee.id)
    garden_leave_status = lifecycle_statuses.find { |status| status.status == 'garden leave' }

    if garden_leave_status
      lifecycle_statuses = Bob::Employee::LifecycleHistory.all(employee.id)
      garden_leave_status = lifecycle_statuses.find { |status| status.status == 'garden leave' }
      termination_date = Date.parse(garden_leave_status.effective_date)
    end

    termination_date ||= Date.parse(employee.internal.termination_date)
    (start_date..end_date).include?(termination_date)
  end
end

.find(employee_id_or_email, params = { humanReadable: 'replace' }) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bob/api/employees.rb', line 31

def self.find(employee_id_or_email, params = { humanReadable: 'replace' })
  if params[:fields]
    params[:fields] = (DEFAULT_FIELDS + params[:fields] + Bob.default_custom_fields.map do |dcf|
      Bob.custom_fields[dcf][:api_field]
    end).uniq
  end
  unless params[:fields]
    params[:fields] = (DEFAULT_FIELDS + Bob.default_custom_fields.map do |dcf|
      Bob.custom_fields[dcf][:api_field]
    end)
  end

  response = post("people/#{employee_id_or_email}", params)
  EmployeeParser.new(JSON.parse(response)).employee
end

.find_by(field:, value:, params: { humanReadable: 'replace' }) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bob/api/employees.rb', line 88

def self.find_by(field:, value:, params: { humanReadable: 'replace' })
  if params[:fields]
    params[:fields] = (DEFAULT_FIELDS + params[:fields] + Bob.default_custom_fields.map do |dcf|
      Bob.custom_fields[dcf][:api_field]
    end).uniq
  end
  unless params[:fields]
    params[:fields] = (DEFAULT_FIELDS + Bob.default_custom_fields.map do |dcf|
      Bob.custom_fields[dcf][:api_field]
    end)
  end

  search(params).find do |employee|
    employee.send(field) == value
  end
end

.search(params = { humanReadable: 'replace' }) ⇒ Object



24
25
26
27
28
29
# File 'lib/bob/api/employees.rb', line 24

def self.search(params = { humanReadable: 'replace' })
  params[:fields] = DEFAULT_FIELDS unless params[:fields]

  response = post('people/search', params)
  EmployeeParser.new(JSON.parse(response)).employees
end

.starts_on(date = Date.current, params = { humanReadable: 'replace' }) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bob/api/employees.rb', line 72

def self.starts_on(date = Date.current, params = { humanReadable: 'replace' })
  if params[:fields]
    params[:fields] = (DEFAULT_FIELDS + params[:fields] + Bob.default_custom_fields.map do |dcf|
      Bob.custom_fields[dcf][:api_field]
    end).uniq
  end
  unless params[:fields]
    params[:fields] = (DEFAULT_FIELDS + Bob.default_custom_fields.map do |dcf|
      Bob.custom_fields[dcf][:api_field]
    end)
  end

  response = post('people/search', params)
  EmployeeParser.new(JSON.parse(response)).starters_on(date)
end

.update(employee_id:, params:) ⇒ Object



105
106
107
# File 'lib/bob/api/employees.rb', line 105

def self.update(employee_id:, params:)
  put("people/#{employee_id}", params)
end

.update_custom_field(employee_id:, field:, value:) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/bob/api/employees.rb', line 109

def self.update_custom_field(employee_id:, field:, value:)
  temp_params = { Bob.custom_fields[field][:dig_path].reverse[0] => value }
  Bob.custom_fields[field][:dig_path].reverse.drop(1).each do |field|
    temp_params = { field => temp_params }
  end

  update(employee_id:, params: temp_params)
end

.update_email(employee_id, email) ⇒ Object



123
124
125
# File 'lib/bob/api/employees.rb', line 123

def self.update_email(employee_id, email)
  put("people/#{employee_id}/email", { email: })
end

.update_start_date(employee_id, start_date) ⇒ Object

start date needs to be in ISO format



119
120
121
# File 'lib/bob/api/employees.rb', line 119

def self.update_start_date(employee_id, start_date)
  post("employees/#{employee_id}", { startDate: start_date })
end