diff --git a/app/models/borrow_direct_requests.rb b/app/models/borrow_direct_requests.rb new file mode 100644 index 000000000..ba4f9c5eb --- /dev/null +++ b/app/models/borrow_direct_requests.rb @@ -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 diff --git a/app/models/patron.rb b/app/models/patron.rb index ec86deb57..6d1353598 100644 --- a/app/models/patron.rb +++ b/app/models/patron.rb @@ -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 @@ -151,6 +151,14 @@ def to_partial_path private + def borrow_direct_requests + BorrowDirectRequests.new(self).requests + end + + def symphony_requests + fields['holdRecordList'].map { |request| Request.new(request) } + end + def fields record['fields'] end diff --git a/app/views/requests/_borrow_direct_request.html.erb b/app/views/requests/_borrow_direct_request.html.erb new file mode 100644 index 000000000..f58d81447 --- /dev/null +++ b/app/views/requests/_borrow_direct_request.html.erb @@ -0,0 +1,31 @@ +
  • +
    +
    + Deliver to Stanford +
    +
    + In transit +
    +
    +
    +

    <%= borrow_direct_request.title %>

    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    Requested:
    +
    <%= l(borrow_direct_request.date_submitted, format: :short) %>
    +
    Source:
    +
    BorrowDirect
    +
    +
    +
  • diff --git a/spec/models/borrow_direct_requests_spec.rb b/spec/models/borrow_direct_requests_spec.rb new file mode 100644 index 000000000..2e7b7121a --- /dev/null +++ b/spec/models/borrow_direct_requests_spec.rb @@ -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 diff --git a/spec/models/patron_spec.rb b/spec/models/patron_spec.rb index 4d5c35081..877c61219 100644 --- a/spec/models/patron_spec.rb +++ b/spec/models/patron_spec.rb @@ -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