lib/eco/api/microcases/set_supervisor.rb in eco-helpers-1.5.1 vs lib/eco/api/microcases/set_supervisor.rb in eco-helpers-1.5.2
- old
+ new
@@ -1,26 +1,30 @@
module Eco
module API
class MicroCases
- #self.define("set_supervisor") do |entry, person, session, options, people, supers_job|
-
- def set_supervisor(entry, person, options, people, supers_job)
- # set supervisor
+ # 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`.
+ # @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 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)
unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
- if !(sup_id = entry.supervisor_id)
- person.supervisor_id = nil
- else
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
+ micro.with_supervisor(sup_id, people) do |supervisor|
+ if !sup_id
+ person.supervisor_id = nil
+ elsif supervisor
person.supervisor_id = supervisor.id
+ elsif !block_given?
+ person.supervisor_id = sup_id
else
- # delay setting supervisor if does not exit
- supers_job.add(person) do |person|
- person.supervisor_id = sup_id
- end
+ yield(sup_id) if block_given?
end
end
end
end
-
+
end
end
end