Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update view to warn user if they have an existing request or loan #2495

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/models/folio/patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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', [])
Expand Down
33 changes: 33 additions & 0 deletions app/models/patron_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Folio::Item>] 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) }
Expand Down
4 changes: 4 additions & 0 deletions app/services/folio_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions app/views/patron_requests/_item_request_hold.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% if type.present? %>
<div class="alert alert-warning alert-dismissible shadow-sm d-flex gap-3 align-items-center">
<i class="bi bi-exclamation-triangle-fill fs-3 text-warning"></i>
<div>
Please be aware that there is already a <%= type %> for this title in our system under your name.
</div>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>
17 changes: 16 additions & 1 deletion app/views/patron_requests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div class="mt-3 col-lg-8">
<%= render 'item_request_hold', type: title_level_on_account %>
</div>
<% end %>

<h1 class="fw-semibold mt-4"><%= @patron_request.item_title %></h1>
<% if @patron_request.selectable_items.one? %>
<% single_item = @patron_request.selectable_items.first %>
Expand Down Expand Up @@ -154,7 +161,8 @@
<tbody>
<% sort_holdings(f.object.selectable_items).each.with_index(1) do |item, index| %>
<% not_requestable = cannot?(:request, item) %>
<tr data-sortby-status="<%= item.status_text.downcase.gsub(' ', '_') %>" data-sortby-callnumber="<%= index %>" >
<% request_holds = @patron_request.on_account?(item) %>
<tr data-sortby-status="<%= item.status_text.downcase.gsub(' ', '_') %>" data-sortby-callnumber="<%= index %>" class="<%= request_holds.present? ? 'border-white' : '' %>">
<td>
<label>
<%= f.check_box 'barcodes', { multiple: true, class: 'form-check-input', disabled: cannot?(:request, item), data: { 'itemselector-target': 'items', 'itemselector-id-param': item.id, 'itemselector-available-param': item.available?, 'itemselector-duequeueinfo-param': queue_length_display(item), 'itemselector-label-param': callnumber_label(item), action: 'itemselector#change' } }, item.barcode || item.id, "" %>
Expand Down Expand Up @@ -198,6 +206,13 @@
<%= requests_patron_item_selector_label(item, not_requestable:) %>
</td>
</tr>
<% if request_holds.present? %>
<tr>
<td colspan="2" class="pt-0 px-3">
<%= render 'item_request_hold', type: request_holds %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Expand Down
6 changes: 4 additions & 2 deletions spec/factories/patrons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
{
patron_blocks: [],
proxies: [],
sponsors: []
sponsors: [],
patron_summary: { holds: [], loans: [] }
}
end

Expand Down Expand Up @@ -61,7 +62,8 @@
}
],
proxies: [],
sponsors: []
sponsors: [],
patron_summary: { holds: [], loans: [] }
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/features/create_aeon_patron_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
let(:bib_data) { :special_collections_single_holding }
let(:patron) do
instance_double(Folio::Patron, id: user.patron_key, username: 'auser', display_name: 'A User', exists?: true, email: nil,
patron_description: 'faculty',
patron_group_name: 'faculty',
patron_description: 'faculty', loans: [],
patron_group_name: 'faculty', holds: [],
blocked?: false, proxies: [], sponsors: [], sponsor?: false, proxy?: false,
allowed_request_types: ['Hold', 'Recall', 'Page'])
end
Expand Down