diff --git a/app/models/folio/patron.rb b/app/models/folio/patron.rb index 61f2e9c0b..bbca15996 100644 --- a/app/models/folio/patron.rb +++ b/app/models/folio/patron.rb @@ -166,6 +166,14 @@ def expired? user_info['active'] == false end + def holds + patron_summary['holds'] + end + + def loans + patron_summary['loans'] + end + private def valid_proxy_relation?(info) @@ -201,6 +209,11 @@ def policy_service @policy_service ||= Folio::CirculationRules::PolicyService.new(patron_groups: [patron_group['id']]) end + def patron_summary + @patron_summary ||= user_info.dig('stubs', 'patron_summary') # used for stubbing + @patron_summary ||= self.class.folio_client.patron_summary(id) + end + def patron_blocks @patron_blocks ||= user_info.dig('stubs', 'patron_blocks') # used for stubbing @patron_blocks ||= self.class.folio_client.patron_blocks(id).fetch('automatedPatronBlocks', []) diff --git a/app/models/patron_request.rb b/app/models/patron_request.rb index 7c99fa036..5c6324c2c 100644 --- a/app/models/patron_request.rb +++ b/app/models/patron_request.rb @@ -272,6 +272,39 @@ def selected_items items end + def on_account?(item = nil) + return instances_on_account if selectable_items.none? + + items_on_account(item) + end + + def instances_on_account + requests = 'request' if account_has_instance?(patron.holds) + loans = 'loan' if account_has_instance?(patron.loans) + [requests, loans].compact.join(' and ') + end + + def items_on_account(item) + requests = 'request' if account_has_item?(item, patron.holds) + loans = 'loan' if account_has_item?(item, patron.loans) + [requests, loans].compact.join(' and ') + end + + def account_has_instance?(instance_list) + return false unless selectable_items.none? && instance_list.present? + + instanceids = instance_list.map { |elem| elem['item']['instanceId'] } + instanceids.include?(instance_id) + end + + def account_has_item?(item, item_list) + return false unless (item || selectable_items.one?) && item_list.present? + + itemid = item ? item.id : selectable_items.first.id + itemids = item_list.map { |elem| elem['item']['itemId'] } + itemids.include?(itemid) + end + # @return [Array] the items that are holdable and recallable by the patron def holdable_recallable_items @holdable_recallable_items ||= selectable_items.filter { |item| item.recallable?(patron) && item.holdable?(patron) } diff --git a/app/services/folio_client.rb b/app/services/folio_client.rb index f2d9c35a6..428e03835 100644 --- a/app/services/folio_client.rb +++ b/app/services/folio_client.rb @@ -148,6 +148,10 @@ def patron_blocks(user_id) get_json("/automated-patron-blocks/#{user_id}") end + def patron_summary(user_id) + get_json("/patron/account/#{user_id}", params: { includeLoans: true, includeHolds: true }) + end + # Defines the hold request data for Folio # [String] pickup_location_id the UUID of the pickup location # [String] patron_comments diff --git a/app/views/patron_requests/_item_request_hold.html.erb b/app/views/patron_requests/_item_request_hold.html.erb new file mode 100644 index 000000000..7c3776e55 --- /dev/null +++ b/app/views/patron_requests/_item_request_hold.html.erb @@ -0,0 +1,9 @@ +<% if type.present? %> +
+ +
+ Please be aware that there is already a <%= type %> for this title in our system under your name. +
+ +
+<% end %> \ No newline at end of file diff --git a/app/views/patron_requests/new.html.erb b/app/views/patron_requests/new.html.erb index cfc9e1d48..e12d490c7 100644 --- a/app/views/patron_requests/new.html.erb +++ b/app/views/patron_requests/new.html.erb @@ -6,6 +6,13 @@ <%= render 'messages', messages: @patron_request.active_messages %> +<% title_level_on_account = @patron_request.on_account? %> +<% if title_level_on_account.present? %> +
+ <%= render 'item_request_hold', type: title_level_on_account %> +
+<% end %> +

<%= @patron_request.item_title %>

<% if @patron_request.selectable_items.one? %> <% single_item = @patron_request.selectable_items.first %> @@ -154,7 +161,8 @@ <% sort_holdings(f.object.selectable_items).each.with_index(1) do |item, index| %> <% not_requestable = cannot?(:request, item) %> - + <% request_holds = @patron_request.on_account?(item) %> +