Skip to content

Commit

Permalink
Add a BorrowDirectRequests model to wrap BorrowDirect and add it to t…
Browse files Browse the repository at this point in the history
…he UI.
  • Loading branch information
jkeck committed Jul 27, 2019
1 parent f0977f6 commit 7dfb0a6
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 3 deletions.
54 changes: 54 additions & 0 deletions app/models/borrow_direct_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

##
# Wrap the BorrowDirect::RequestQuery in a class that we
# can inject our patron barcode from (and do error handling)
class BorrowDirectRequests
attr_reader :patron
def initialize(patron)
@patron = patron
end

def requests
request_client.requests('open', true).map { |request| BorrowDirectRequests::Request.new(request) }.select(&:active?)
rescue BorrowDirect::Error
[]
end

private

def request_client
@request_client ||= BorrowDirect::RequestQuery.new(patron.barcode)
end

##
# Wrap the BorrowDirect::RequestQuery::Item in a class
# so we can give it a similar iterface to Symphony Requests
class Request < SimpleDelegator
# Request becomes ON_LOAN once we receive it (and should show as a request ready for pickup/checkout)
# Request becomes COMPLETED once the uesr returns it
ACTIVE_REQUEST_STATUSES = %w[
ENTERED IN_PROCESS SHIPPED
].freeze

def active?
ACTIVE_REQUEST_STATUSES.include? request_status
end

def key
request_number
end

def expiration_date; end

def fill_by_date; end

def ready_for_pickup?
false
end

def to_partial_path
'requests/borrow_direct_request'
end
end
end
12 changes: 11 additions & 1 deletion app/models/patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def fines
end

def requests
@requests ||= fields['holdRecordList'].map { |request| Request.new(request) }
@requests ||= symphony_requests + borrow_direct_requests
end

def group
Expand All @@ -151,6 +151,16 @@ def to_partial_path

private

def borrow_direct_requests
return [] if proxy_borrower? # Proxies can't submit borrow direct requests, so don't check.

BorrowDirectRequests.new(self).requests
end

def symphony_requests
fields['holdRecordList'].map { |request| Request.new(request) }
end

def fields
record['fields']
end
Expand Down
31 changes: 31 additions & 0 deletions app/views/requests/_borrow_direct_request.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<li class="row d-flex flex-wrap list-group-item">
<div class="d-flex flex-row flex-grow-1 justify-content-between w-100 row col-md-4">
<div class="w-50 col-md-6 library">
<span class="d-md-none">Deliver to </span> Stanford
</div>
<div class="w-50 col-md-6 text-right text-md-left date">
In transit
</div>
</div>
<div class="d-flex flex-grow-1 flex-column flex-md-row w-75 row col-md-8">
<h3 class="col-md-7 clamp-2 record-title title text-reset"><%= borrow_direct_request.title %></h3>
<div class="d-flex flex-row row col-md-5">
<div class="w-50 col-md-6"></div>
<div class="w-50 col-md-6"></div>
</div>
</div>
<div>
<button class="btn collapsed" type="button" data-toggle="collapse" data-target="#collapseRequest-<%= borrow_direct_request.key %>" aria-expanded="false" aria-controls="collapseRequest-<%= borrow_direct_request.key %>">
<span class="expand-icon"><%= sul_icon :'sharp-add-24px' %><span class="sr-only">Expand</span></span>
<span class="collapse-icon"><%= sul_icon :'sharp-remove-24px' %><span class="sr-only">Collapse</span></span>
</button>
</div>
<div class="collapse w-100" id="collapseRequest-<%= borrow_direct_request.key %>">
<dl class="row mb-0">
<dt class="col-3 offset-1 col-md-2 offset-md-2">Requested:</dt>
<dd class="col-8"><%= l(borrow_direct_request.date_submitted, format: :short) %></dd>
<dt class="col-3 offset-1 col-md-2 offset-md-2">Source:</dt>
<dd class="col-8">BorrowDirect</dd>
</dl>
</div>
</li>
71 changes: 71 additions & 0 deletions spec/models/borrow_direct_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe BorrowDirectRequests do
subject(:bd_requests) { described_class.new(patron) }

let(:patron) { instance_double(Patron, barcode: '123456') }
let(:in_process_request) do
instance_double(
BorrowDirect::RequestQuery::Item,
request_number: 'STA-123454321',
request_status: 'IN_PROCESS',
date_submitted: Time.zone.today - 3.days
)
end
let(:completed_request) { instance_double(BorrowDirect::RequestQuery::Item, request_status: 'COMPLETED') }
let(:mock_requests) do
instance_double(BorrowDirect::RequestQuery, requests: [in_process_request, completed_request])
end

context 'when successful' do
before do
allow(BorrowDirect::RequestQuery).to receive(:new).with(patron.barcode).and_return(mock_requests)
end

it 'only return requests with active statuses' do
expect(bd_requests.requests.length).to be(1)
end
end

context 'when borrow direct returns an error' do
before do
allow(BorrowDirect::RequestQuery).to receive(:new).with(patron.barcode).and_raise(
BorrowDirect::Error, 'Item not Found'
)
end

it 'returns an empty array' do
expect(bd_requests.requests).to eq([])
end
end

describe 'BorrowDirectRequests::Request' do
let(:request) do
BorrowDirectRequests::Request.new(in_process_request)
end

it 'delegates methods to the given request oboject' do
expect(request.date_submitted).to eq in_process_request.date_submitted
end

it 'returns the request_number as the key' do
expect(request.key).to eq in_process_request.request_number
end

it { expect(request).not_to be_ready_for_pickup }

context 'when in an active state' do
it { expect(request).to be_active }
end

context 'when not in an active state' do
let(:request) do
BorrowDirectRequests::Request.new(completed_request)
end

it { expect(request).not_to be_active }
end
end
end
14 changes: 12 additions & 2 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,18 @@
}]
end

it 'has fines' do
expect(group.requests).to all(be_a(Request))
before do
allow(BorrowDirectRequests).to receive(:new).and_return(
instance_double(BorrowDirectRequests, requests: [{ key: 2 }])
)
end

it 'has checkouts from symphony' do
expect(group.requests.first).to be_a(Request)
end

it 'has checkouts from BorrowDirect' do
expect(group.requests.last[:key]).to be(2)
end
end
end
7 changes: 7 additions & 0 deletions spec/models/patron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,19 @@
context 'with requests' do
before do
fields[:holdRecordList] = [{ key: 1, fields: {} }]
allow(BorrowDirectRequests).to receive(:new).and_return(
instance_double(BorrowDirectRequests, requests: [{ key: 2 }])
)
end

describe '#requests' do
it 'returns a list of requests for the patron' do
expect(patron.requests).to include a_kind_of(Request).and(have_attributes(key: 1))
end

it 'includes requests that come from the BorrowDirectRequests class' do
expect(patron.requests.last[:key]).to be 2
end
end
end
end

0 comments on commit 7dfb0a6

Please sign in to comment.