-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactors loading of checkout history report
updates to v2 of alma_rest_client
- Loading branch information
Showing
12 changed files
with
1,688 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.