Module: Eco::API::MicroCases::People::ApplyChanges::SetSupervisor

Included in:
Eco::API::MicroCases::People::ApplyChanges
Defined in:
lib/eco/api/microcases/people/apply_changes/set_supervisor.rb

Instance Method Summary collapse

Instance Method Details

#set_supervisor(person, sup_id, people, options) {|supervisor_id| ... } ⇒ Object

Note:
  • It prevents the basic cyclic supervisor case (supervisor to be supervisor of themselves)

Unique access point to set the supervisor_id value on a person.

Parameters:

  • person (Ecoportal::API::V1::Person)

    the person we want to update, carrying the changes to be done.

  • sup_id (nil, String)

    the supervisor id we should set on the person.

  • people (Eco::API::Organization::People)

    People involved in the current update.

  • options (Hash)

    the options.

Yields:

  • (supervisor_id)

    callback when the supervisor_id is unknown (not nil nor any one's in people).

Yield Parameters:

  • supervisor_id (String)

    the unknown supervisor_id.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eco/api/microcases/people/apply_changes/set_supervisor.rb', line 16

def set_supervisor(person, sup_id, people, options)
  return false if options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
  return false if sup_id && ((person.id == sup_id) || (person.external_id == sup_id))

  cur_id    = person.supervisor_id
  cur_super = cur_id && with_supervisor(cur_id, people)

  micro.with_supervisor(sup_id, people) do |new_super|
    if !sup_id
      person.supervisor_id = nil
      descrease_subordinates(cur_super)
    elsif new_super && (id = new_super.id)
      set_supervisor(new_super, nil, people, options) if new_super_direct_cyclic?(person, new_super)
      person.supervisor_id = id
      descrease_subordinates(cur_super)
      increase_subordinates(new_super)
    elsif !block_given?
      descrease_subordinates(cur_super)
      person.supervisor_id = sup_id
    elsif block_given?
      yield(sup_id)
    end
  end
end