Skip to content

Commit

Permalink
refactors loading of checkout history report
Browse files Browse the repository at this point in the history
updates to v2 of alma_rest_client
  • Loading branch information
niquerio committed Jan 6, 2025
1 parent 5b13a9b commit 1124f2d
Show file tree
Hide file tree
Showing 12 changed files with 1,688 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gem "yabeda-prometheus"

gem "alma_rest_client",
git: "https://github.com/mlibrary/alma_rest_client",
tag: "1.3.1"
tag: "v2.0.0"

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
31 changes: 20 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
GIT
remote: https://github.com/mlibrary/alma_rest_client
revision: 2014809367ece3f21935bd24b45a12285f61e253
tag: 1.3.1
revision: 9606225d82480b6d1568902813ae9018dd8c1acc
tag: v2.0.0
specs:
alma_rest_client (1.3.1)
alma_rest_client (2.0.0)
activesupport (~> 7.0, >= 4.2)
httparty
faraday
faraday-retry
httpx
rexml

GEM
Expand Down Expand Up @@ -101,7 +103,6 @@ GEM
bigdecimal
rexml
crass (1.0.6)
csv (3.3.2)
database_cleaner (2.1.0)
database_cleaner-active_record (>= 2, < 3)
database_cleaner-active_record (2.2.0)
Expand All @@ -121,14 +122,21 @@ GEM
railties (>= 5.0.0)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
faraday (2.12.2)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.4.0)
net-http (>= 0.5.0)
faraday-retry (2.2.1)
faraday (~> 2.0)
ffi (1.17.0-x86_64-linux-gnu)
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.1.2)
httparty (0.22.0)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
http-2 (1.0.2)
httpx (1.4.0)
http-2 (>= 1.0.0)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.8.0)
Expand Down Expand Up @@ -157,9 +165,9 @@ GEM
mini_mime (1.1.5)
minitest (5.25.4)
msgpack (1.7.5)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
mysql2 (0.5.6)
net-http (0.6.0)
uri
net-imap (0.5.4)
date
net-protocol
Expand Down Expand Up @@ -301,6 +309,7 @@ GEM
unicode-display_width (3.1.2)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uri (1.0.2)
useragent (0.16.11)
webmock (3.24.0)
addressable (>= 2.8.0)
Expand Down
5 changes: 5 additions & 0 deletions app/utils/checkout_history_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CheckoutHistoryLoader
class LoanLoadError < StandardError; end

class FetchReportError < StandardError; end
end
48 changes: 48 additions & 0 deletions app/utils/checkout_history_loader/loan_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module CheckoutHistoryLoader
class LoanLoader
def initialize(report_item:, user:, loan:)
@report_item = report_item
@user = user
@loan = loan
end

def has_identical_checkout_date?
@loan.checkout_date&.to_date&.to_fs(:db) == @report_item.checkout_date
end

def load
return if @loan.return_date.present?
return if @report_item.return_date.blank?

@loan.tap do |l|
l.user = @user
l.id = @report_item.id
l.title = @report_item.title
l.author = @report_item.author
l.mms_id = @report_item.mms_id
l.return_date = @report_item.return_date
l.checkout_date = @report_item.checkout_date
l.barcode = @report_item.barcode
l.call_number = @report_item.call_number
l.description = @report_item.description
end

if @loan.save
Rails.logger.info("item_loan '#{@loan.id}' saved")
else
Rails.logger.error("item_loan '#{@loan.id}' not saved: #{@loan.errors.full_messages}")
raise LoanLoadError, @loan.errors.full_messages
end
end

def self.load(report_item)
user = User.find_or_create_by_uniqname(report_item.uniqname)
unless user.retain_history
Rails.logger.warn("item_loan '#{report_item.id}' not saved: patron opt out")
return
end
loan = Loan.find_or_create_by(id: report_item.id)
new(report_item: report_item, user: user, loan: loan).load
end
end
end
28 changes: 28 additions & 0 deletions app/utils/checkout_history_loader/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module CheckoutHistoryLoader
class Report
include Enumerable

def initialize(items)
@items = items
end

def each(&block)
@items.each(&block)
end

def self.for_rows(rows)
new(rows.map { |x| ReportItem.new(x) })
end

def self.fetch
rows = []
response = AlmaRestClient.client.get_report(path: ENV.fetch("CIRC_REPORT_PATH")) do |row|
rows.push ReportItem.new(row)
end
if response.status != 200
raise FetchReportError, response.body
end
new(rows)
end
end
end
47 changes: 47 additions & 0 deletions app/utils/checkout_history_loader/report_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module CheckoutHistoryLoader
class ReportItem
def initialize(row)
@row = row
end

def uniqname
@row["User Primary Identifier"]
end

def id
@row["Item Loan Id"]
end

def title
@row["Title"]&.slice(0, 255)
end

def author
@row["Author"]&.slice(0, 255)
end

def mms_id
@row["MMS Id"]
end

def checkout_date
@row["Loan Date"]
end

def return_date
@row["Return Date"]
end

def barcode
@row["Barcode"]
end

def call_number
@row["Call Number"]
end

def description
@row["Description"]
end
end
end
23 changes: 23 additions & 0 deletions app/utils/fix_uniqname_problem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module FixUniqnameProblem
def self.opted_in?(uniqname)
User.find(uniqname)&.retain_history
rescue
false
end

def self.list_of_opted_in
path = "#{Rails.root}/app/utils/uniqnames.txt"
list = []
File.open(path, "r") do |f|
f.each_line do |line|
if opted_in?(line.downcase.strip)
list.push(line)
end
end
end
output = "#{Rails.root}/app/utils/opted_in.txt"
File.open(output, "w") do |f|
f.puts list
end
end
end
Loading

0 comments on commit 1124f2d

Please sign in to comment.