diff --git a/app/jobs/submit_folio_request_job.rb b/app/jobs/submit_folio_request_job.rb index 48dc3c3ee..f87ba5e90 100644 --- a/app/jobs/submit_folio_request_job.rb +++ b/app/jobs/submit_folio_request_job.rb @@ -96,7 +96,7 @@ def pickup_location_id code = Settings.libraries[request.destination].folio_pickup_service_point_code code ||= Settings.libraries['GREEN'].folio_pickup_service_point_code - Folio::Types.instance.service_points.values.find { |v| v.code == code }&.id + Folio::Types.service_points.find_by(code:)&.id end end diff --git a/app/models/folio/request_abilities.rb b/app/models/folio/request_abilities.rb index 22398594f..4089cf62b 100644 --- a/app/models/folio/request_abilities.rb +++ b/app/models/folio/request_abilities.rb @@ -70,9 +70,12 @@ def pickup_libraries # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def default_pickup_library - service_points = Folio::Types.instance.service_points.select do |_k, v| - v.is_default_for_campus.present? && v.is_default_for_campus == request.holdings.first&.effective_location&.campus&.code - end.values.map(&:code) + campus_code = request.holdings.first&.effective_location&.campus&.code + service_points = if campus_code + Folio::Types.service_points.where(is_default_for_campus: campus_code).map(&:code) + else + [] + end library = Settings.libraries.keys.find do |key| service_points.include? Settings.libraries[key].folio_pickup_service_point_code @@ -84,7 +87,7 @@ def default_pickup_library private def default_pickup_libraries - service_points = Folio::Types.instance.service_points.select { |_k, v| v.is_default_pickup }.values.map(&:code) + service_points = Folio::Types.service_points.where(is_default_pickup: true).map(&:code) Settings.libraries.keys.select do |key| service_points.include? Settings.libraries[key].folio_pickup_service_point_code diff --git a/app/services/folio/service_point_store.rb b/app/services/folio/service_point_store.rb new file mode 100644 index 000000000..fae45619d --- /dev/null +++ b/app/services/folio/service_point_store.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Folio + # A cache of service point data + class ServicePointStore + def initialize(data_from_cache) + @data = data_from_cache.map { |p| Folio::ServicePoint.from_dynamic(p) } + end + + attr_reader :data + + def find_by(code:) + data.find { |candidate| candidate.code == code } + end + + def where(args) + if args.key?(:is_default_for_campus) + data.select { |candidate| candidate.is_default_for_campus == args[:is_default_for_campus] } + elsif args.key?(:is_default_pickup) + data.select { |candidate| candidate.is_default_pickup == args[:is_default_pickup] } + else + raise "unknown argument #{args.inspect}" + end + end + end +end diff --git a/app/services/folio/types.rb b/app/services/folio/types.rb index 0828ad6eb..d193d7441 100644 --- a/app/services/folio/types.rb +++ b/app/services/folio/types.rb @@ -6,7 +6,7 @@ module Folio # accessing the types. class Types class << self - delegate :policies, :circulation_rules, :criteria, :get_type, :locations, :libraries, to: :instance + delegate :policies, :circulation_rules, :criteria, :get_type, :locations, :libraries, :service_points, to: :instance end def self.instance @@ -44,7 +44,7 @@ def circulation_rules end def service_points - get_type('service_points').map { |p| Folio::ServicePoint.from_dynamic(p) }.index_by(&:id) + @service_points ||= ServicePointStore.new(get_type('service_points')) end def policies