Sha256: 19aa9e7ca0b16559277bfa3a2fc04d198456965c2d291b1d00703e167da1b1df

Contents?: true

Size: 1.44 KB

Versions: 23

Compression:

Stored size: 1.44 KB

Contents

module Eco
  module API
    class MicroCases
      # Finds the supervisor among `people` by using the `supervisor_id` of `value`.
      # @param value [String, Ecoportal::API::V1::Person, Hash] the subordinate person,
      #   or the unique identifier of the supervisor to be found (preferably the `external_id`).
      # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
      # @param strict [Boolean] specifies if the search should be `strict` (see {Eco::API::Organization::People#person}).
      # @yield [supervisor] block that does some stuff with supevisor.
      # @yieldparam supervisor [Ecoportal::API::V1::Person] the person object of the supervisor, or `nil` if not found.
      # @return [nil, Ecoportal::API::V1::Person] found `supervisor` of `value`.
      def with_supervisor(value, people, strict: false)
        if sup_id = with_supervisor_supervisor_id(value)
          people.person(id: sup_id, external_id: sup_id, email: sup_id, strict: strict)
        end.tap do |supervisor|
          yield(supervisor) if block_given?
        end
      end

      private

      def with_supervisor_supervisor_id(value)
        return nil if value.to_s.strip.empty?
        case
        when value.respond_to?(:supervisor_id)
          value.supervisor_id
        when value.is_a?(Hash)
          value["supervisor_id"]
        else
          value
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.20 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.19 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.18 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.17 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.16 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.15 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.14 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.13 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.12 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.11 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.10 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.9 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.8 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.7 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.6 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.5 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.4 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.3 lib/eco/api/microcases/with_supervisor.rb
eco-helpers-3.0.2 lib/eco/api/microcases/with_supervisor.rb