Skip to content

Commit

Permalink
Merge pull request #16 from mlibrary/return-dates-bug
Browse files Browse the repository at this point in the history
Return dates bug
  • Loading branch information
niquerio authored Jul 21, 2022
2 parents 0a3b4a8 + c58579d commit d33bb73
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 3 additions & 1 deletion config/alma_analytics_reports/circ-history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
<sawx:expr xsi:type="sawx:sqlExpression">"Loan Details"."Call Number"</sawx:expr></saw:columnFormula></saw:column></saw:columns>
<saw:filter>
<sawx:expr xsi:type="sawx:logical" op="and">
<sawx:expr xsi:type="sawx:sql">"Loan Date"."Loan Date" &gt;= TIMESTAMPADD(SQL_TSI_DAY, -7, CURRENT_DATE)</sawx:expr>
<sawx:expr xsi:type="sawx:logical" op="or">
<sawx:expr xsi:type="sawx:sql">"Loan Date"."Loan Date" &gt;= TIMESTAMPADD(SQL_TSI_DAY, -7, CURRENT_DATE)</sawx:expr>
<sawx:expr xsi:type="sawx:sql">"Return Date"."Return Date" &gt;= TIMESTAMPADD(SQL_TSI_DAY, -7, CURRENT_DATE)</sawx:expr></sawx:expr>
<sawx:expr xsi:type="sawx:comparison" op="notEqual">
<sawx:expr xsi:type="sawx:sqlExpression">"Borrower Details"."Primary Identifier"</sawx:expr>
<sawx:expr xsi:type="xsd:string">None</sawx:expr></sawx:expr>
Expand Down
6 changes: 5 additions & 1 deletion lib/tasks/alma_circ_history.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ namespace :alma_circ_history do
Rails.logger.warn("item_loan '#{row["Item Loan Id"]}' not saved: patron opt out")
next
end
loan = Loan.new do |l|
loan = Loan.find_or_create_by(id: row["Item Loan Id"])
next if loan.return_date.present? || loan.checkout_date&.to_date&.to_fs(:db) == row["Loan Date"]

# mrio: using `tap` so I can use block syntax
loan.tap do |l|
l.user = u
l.id = row["Item Loan Id"]
l.title = row["Title"]&.slice(0, 255)
Expand Down
2 changes: 0 additions & 2 deletions spec/support/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
loan.return_date = Faker::Date.backward(days: 365)
loan.checkout_date = Faker::Date.between(from: loan.return_date - 180.days,
to: loan.return_date)
elsif loan.return_date.nil?
loan.return_date = Faker::Date.between(from: loan.checkout_date, to: DateTime.now)
end
end
end
Expand Down
28 changes: 26 additions & 2 deletions spec/tasks/alma_circ_history_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,40 @@
load_circ_history
expect(Loan.all.count).to eq(2)
end
it "skips over already loaded history" do
it "skips over already loaded history with a return date" do
user_ajones
user_emcard
loan = create(:loan, id: "3159980960006381", title: "my_title")
loan = create(:loan, id: "3159980960006381", title: "my_title", checkout_date: "2020-01-07", return_date: "2022-07-20")
expect(Loan.all.count).to eq(1)
load_circ_history
expect(Loan.all.count).to eq(2)
expect(Loan.find(loan.id).title).to eq("my_title")
end

it "skips over already loaded history where there isn't a new return date in the report" do
user_ajones
user_emcard
loan = create(:loan, id: "3159980960006381", title: "my_title", checkout_date: "2020-01-07")
loans = File.read("./spec/fixtures/circ_history.json")
loans.gsub!("2021-01-14", "")
@stub.to_return(body: loans, headers: {content_type: "application/json"})
@stub.response # clear out original response
expect(Loan.all.count).to eq(1)
load_circ_history
expect(Loan.all.count).to eq(2)
expect(Loan.find(loan.id).title).to eq("my_title")
end
it "processes already loaded history that doesn't include a reutrn date where theres a new return date in the report" do
user_ajones
user_emcard
loan = create(:loan, id: "3159980960006381", title: "my_title", checkout_date: "2022-01-07")
expect(Loan.all.count).to eq(1)
load_circ_history
expect(Loan.all.count).to eq(2)
expect(Loan.find(loan.id).title).to eq("Between the world and me /")
expect(Loan.find(loan.id).return_date.to_date.to_fs(:db)).to eq("2021-01-14")
end

it "it adds user if they don't exist" do
user_ajones
load_circ_history
Expand Down

0 comments on commit d33bb73

Please sign in to comment.