Skip to content

Commit

Permalink
Extract logic about querying service points to a new class
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 11, 2023
1 parent 8e06183 commit 6ca0faf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/jobs/submit_folio_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions app/models/folio/request_abilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions app/services/folio/service_point_store.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions app/services/folio/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ca0faf

Please sign in to comment.