Module: Models::EmployeeManagement

Extended by:
ActiveSupport::Concern
Included in:
Employee
Defined in:
app/concerns/models/employee_management.rb

Overview

Management hierarchy, customer assignment, and schema.org structure.

Mixed into Employee; combines subordinate-resolution (via the ltree
hierarchy plus role inheritance), customer-drop activity cleanup, and
schema.org structured-data output for public author pages.

See Also:

Instance Method Summary collapse

Instance Method Details

#current_reviewEmployeeReview?

The most recent active current-goals review for this employee.

Returns:



94
95
96
# File 'app/concerns/models/employee_management.rb', line 94

def current_review
  employee_record.employee_reviews.current_goals.active.order(:created_at).reverse_order.first
end

#drop_customer_event(event) ⇒ void

This method returns an undefined value.

Cancels open activities when a customer is dropped from this employee.

No-ops while the customer still has any assigned sales rep; otherwise
marks every open activity for the customer as closed by the event's
creator with the "dropped" activity result type.

Parameters:

  • event (Event)

    drop event carrying customer_id and creator_id



66
67
68
69
70
71
72
73
74
# File 'app/concerns/models/employee_management.rb', line 66

def drop_customer_event(event)
  return unless event.customer.sales_reps.empty?

  to_cancel = activities.open_activities.where(party_id: event.customer_id)
  logger.debug("Employee#drop_customer_event: #{event.inspect}, cancelling: #{to_cancel.count}")
  to_cancel.each do |activity|
    activity.update!(activity_result_type_id: 1, closed_by_id: event.creator_id)
  end
end

#managesActiveRecord::Relation<Employee>

Subordinates of this employee.

Returns:



27
28
29
# File 'app/concerns/models/employee_management.rb', line 27

def manages
  Employee.where(id: manages_ids)
end

#manages?(employee) ⇒ Boolean

Whether the given employee reports to this one.

Parameters:

  • employee (Employee, nil)

    employee to test; nil is always false

Returns:

  • (Boolean)

    true if the employee appears in #manages_ids



18
19
20
21
22
# File 'app/concerns/models/employee_management.rb', line 18

def manages?(employee)
  return false unless employee

  manages_ids.include?(employee.id)
end

#manages_and_selfActiveRecord::Relation<Employee>

Subordinates plus this employee.

Returns:



34
35
36
# File 'app/concerns/models/employee_management.rb', line 34

def manages_and_self
  Employee.where(id: manages_ids + [id])
end

#manages_idsArray<Integer>

Ids of every employee this one manages, directly or transitively.

Logic Details

Combines three sources and deduplicates them: descendants from the
ltree hierarchy, employees whose account roles are managed by this
employee's roles, and explicitly linked managed_employee_ids.
The result is memoized in Rails.cache keyed on the record's
cache_key.

Returns:

  • (Array<Integer>)

    party ids of managed employees



48
49
50
51
52
53
54
55
56
# File 'app/concerns/models/employee_management.rb', line 48

def manages_ids
  Rails.cache.fetch([cache_key, :employee_subordinates]) do
    managed_role_ids = Role.managed_by_roles(role_ids).ids
    list = descendants.pluck('parties.id')
    list |= Employee.joins(account: :roles).where('roles.id' => managed_role_ids).pluck('parties.id') if managed_role_ids.present?
    list += managed_employee_ids
    list.uniq
  end
end

#role_cleanupvoid

This method returns an undefined value.

Reconciles this employee's account roles with their active state.

Inactive employees lose all roles; active employees only shed roles
already covered by an inherited ancestor.



82
83
84
85
86
87
88
89
# File 'app/concerns/models/employee_management.rb', line 82

def role_cleanup
  if inactive
    logger.info "Employee #{id} Inactive, clearing all roles"
    .roles.clear
  else
    prune_inherited_roles
  end
end

#schema_dot_org_structureSchemaDotOrg::Person

schema.org Person representation of this employee.

Returns:



101
102
103
104
105
106
107
108
109
110
# File 'app/concerns/models/employee_management.rb', line 101

def schema_dot_org_structure
  SchemaDotOrg::Person.new.tap do |person|
    person.givenName = first_name
    person.familyName = last_name
    person.name = full_name
    assign_author_url(person)
    assign_profile_image(person)
    assign_same_as_links(person)
  end
end

#to_json_ldString?

Note:

Reports and swallows any error, returning nil, so structured
data can never break page rendering.

JSON-LD serialization of #schema_dot_org_structure.

Returns:

  • (String, nil)

    JSON-LD markup, or nil on failure



117
118
119
120
121
122
# File 'app/concerns/models/employee_management.rb', line 117

def to_json_ld
  schema_dot_org_structure.to_s
rescue StandardError => e
  ErrorReporting.error e
  logger.error e
end