Skip to content

Commit

Permalink
Merge pull request #995 from sul-dlss/handleIlliadDates
Browse files Browse the repository at this point in the history
Handling 'NotWantedAfter' ILLIAD transaction values.
  • Loading branch information
corylown authored Oct 24, 2023
2 parents 99f0bb5 + 7cf8c93 commit 952c43b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/models/illiad_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,28 @@ def pickup_library
end

def expiration_date
scan_type? ? placed_date + 2.months : Time.zone.parse(@illiad_result['NotWantedAfter'])
based_on_placed = placed_date + 2.months
# In some cases, even if the request is a hold/recall and not a scan,
# the 'NotWantedAfter' field may be blank. In that case, we will just
# use the date that is 2 months after the transaction creation date
scan_type? ? based_on_placed : (user_supplied_expiration_date || based_on_placed)
end

def user_supplied_expiration_date
return nil if @illiad_result['NotWantedAfter'].blank?

parse_expiration_date(@illiad_result['NotWantedAfter'])
end

def parse_expiration_date(illiad_date)
# Some dates will be in mm/dd/yyyy or m/d/yyyy format, and Time.zone.parse will throw an error
date_regex = %r{\d{1,2}/\d{1,2}/\d{4}}
date_to_parse = illiad_date.match?(date_regex) ? Date.strptime(illiad_date, '%m/%d/%Y').to_s : illiad_date
# We still want to return a date that provides a time portion/is consistent with the other results
Time.zone.parse(date_to_parse)
rescue ArgumentError => e
Honeybadger.notify(e, error_message: "Parsing #{illiad_date} for ILLIAD request expiration date returns #{e}")
nil
end

def fill_by_date; end
Expand Down
27 changes: 27 additions & 0 deletions spec/models/illiad_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
"NotWantedAfter":"2024-10-11",
"CallNumber":"ABC"}'
end
let(:expiration_result) do
'{"CreationDate":"2022-05-11T10:49:41.783",
"NotWantedAfter":"07/15/2022s"}'
end
let(:expiration_empty_result) do
'{"CreationDate":"2022-05-11T10:49:41.783",
"NotWantedAfter":null}'
end
let(:scan_result) do
'{"TransactionNumber":12345,
"Username":"sunet",
Expand Down Expand Up @@ -44,6 +52,9 @@
context 'when the request is ILLIAD hold/recall' do
subject(:hold) { IlliadRequests::Request.new(JSON.parse(hold_recall_result)) }

let(:expiration_hold) { IlliadRequests::Request.new(JSON.parse(expiration_result)) }
let(:expiration_empty_hold) { IlliadRequests::Request.new(JSON.parse(expiration_empty_result)) }

it 'correctly identified hold recall result as not being type scan' do
expect(hold.scan_type?).to be(false)
end
Expand All @@ -55,6 +66,18 @@
it 'correctly retrieves author for non-scan transaction' do
expect(hold.author).to eq('Gilbert Roy')
end

it 'correctly calculates the expiration date' do
expect(hold.expiration_date.to_s).to eq('2024-10-11 00:00:00 -0700')
end

it 'handles ILLIAD expiration date of format mm/dd/yyyy' do
expect(expiration_hold.expiration_date.to_s).to eq('2022-07-15 00:00:00 -0700')
end

it 'handles an empty ILLIAD expiration date and returns a date two months after placed date' do
expect(expiration_empty_hold.expiration_date.to_s).to eq('2022-07-11 10:49:41 -0700')
end
end

context 'when the request is an ILLIAD scan request' do
Expand All @@ -71,6 +94,10 @@
it 'correctly retrieves author for scan transaction' do
expect(scan.author).to eq('Frederick Wright')
end

it 'returns a date two months after creation date for expiration' do
expect(scan.expiration_date).to eq(scan.placed_date + 2.months)
end
end
end
end

0 comments on commit 952c43b

Please sign in to comment.