From 8f1ab04b49b9d22236b4ae31f9f3f1f6ac9e8d93 Mon Sep 17 00:00:00 2001 From: Huda Khan Date: Mon, 23 Oct 2023 15:08:18 -0600 Subject: [PATCH 1/2] updating code --- app/models/illiad_requests.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/illiad_requests.rb b/app/models/illiad_requests.rb index 85f99ea1..4ea5b090 100644 --- a/app/models/illiad_requests.rb +++ b/app/models/illiad_requests.rb @@ -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 From 7cf8c93d453deb2b3a5a9694b9cbc430be0dbce2 Mon Sep 17 00:00:00 2001 From: Huda Khan Date: Mon, 23 Oct 2023 16:15:47 -0600 Subject: [PATCH 2/2] adding tests handling expiration date formats --- spec/models/illiad_requests_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/models/illiad_requests_spec.rb b/spec/models/illiad_requests_spec.rb index a657fe71..65ff99c0 100644 --- a/spec/models/illiad_requests_spec.rb +++ b/spec/models/illiad_requests_spec.rb @@ -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", @@ -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 @@ -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 @@ -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