lib/eco/api/microcases/set_supervisor.rb in eco-helpers-2.0.25 vs lib/eco/api/microcases/set_supervisor.rb in eco-helpers-2.0.26
- old
+ new
@@ -1,28 +1,49 @@
module Eco
module API
class MicroCases
- # Special snippet to decide if the `supervisor_id` is set now or in a later batch job `supers_job`.
- # @note delaying the setting of a `supervisor_id` can save errors when the supervisor still does not exit.
- # @param sup_id [nil, String] the **supervisor id** we should set on the `person`.
+ # Unique access point to set the `supervisor_id` value on a person.
# @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
- # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
+ # @param sup_id [nil, String] the **supervisor id** we should set on the `person`.
+ # @param people [Eco::API::Organization::People] _People_ involved in the current update.
# @param options [Hash] the options.
# @yield [supervisor_id] callback when the supervisor_id is **unknown** (not `nil` nor any one's in `people`).
# @yieldparam supervisor_id [String] the **unknown** `supervisor_id`.
- def set_supervisor(sup_id, person, people, options)
+ def set_supervisor(person, sup_id, people, options)
unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
- micro.with_supervisor(sup_id, people) do |supervisor|
+ 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
- elsif supervisor
- person.supervisor_id = supervisor.id
+ descrease_subordinates(cur_super)
+ elsif new_super && id = new_super.id
+ person.supervisor_id = id
+ descrease_subordinates(cur_super)
+ increase_subordinates(new_super)
elsif !block_given?
+ descrease_subordinates(cur_super)
person.supervisor_id = sup_id
else
yield(sup_id) if block_given?
end
end
+ end
+ end
+
+ private
+
+ def descrease_subordinates(person, by = 1)
+ if person.is_a?(Ecoportal::API::V1::Person)
+ person.subordinates -= by
+ #person.subordinates = 0 if person.subordinates < 0
+ end
+ end
+
+ def increase_subordinates(person, by = 1)
+ if person.is_a?(Ecoportal::API::V1::Person)
+ #person.subordinates = 0 if person.subordinates < 0
+ person.subordinates += by
end
end
end
end