Skip to content

Commit

Permalink
removed obsolete renew code; fixed loans
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 13, 2023
1 parent c2add4d commit d68977a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
40 changes: 2 additions & 38 deletions models/items/alma/loans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,6 @@ def initialize(parsed_response:, pagination:)
@pagination = pagination
end

def self.renew_all(uniqname:, client: AlmaRestClient.client, connections: [],
publisher: Publisher.new)
url = "/users/#{uniqname}/loans"
publisher.publish({step: 1, count: 0, renewed: 0, uniqname: uniqname})
response = client.get_all(url: url, record_key: "item_loan", query: {"expand" => "renewable"})

return response if response.code != 200
loans = response.parsed_response["item_loan"]&.map do |loan|
Loan.new(loan)
end
renew(uniqname: uniqname, loans: loans, publisher: publisher)
end

def self.renew(uniqname:, loans:, publisher: Publisher.new)
count = 0
renewed = 0
renew_statuses = []
loans.filter { |x| x.renewable? }.each do |loan|
response = Loan.renew(uniqname: uniqname, loan_id: loan.loan_id)
if response.code != 200
renew_statuses.push(:fail)
else
renewed += 1
renew_statuses.push(:success)
end
count += 1
publisher.publish({step: 2, count: count, renewed: renewed, uniqname: uniqname})
end
publisher.publish({step: 3, count: count, renewed: renewed, uniqname: uniqname})
RenewResponse.new(renew_statuses: renew_statuses)
end

def count
@parsed_response["total_record_count"]
end
Expand All @@ -55,8 +23,8 @@ def self.for(uniqname:, offset: nil, limit: 15,
query["limit"] = limit.nil? ? 15 : limit

response = client.get(url, query: query)
raise StandardError unless response.code == 200
pr = response.parsed_response
raise StandardError unless response.status == 200
pr = response.body
pagination_params = {url: "/current-checkouts/u-m-library", total: pr["total_record_count"]}
pagination_params[:limit] = limit unless limit.nil?
pagination_params[:current_offset] = offset unless offset.nil?
Expand All @@ -67,10 +35,6 @@ def self.for(uniqname:, offset: nil, limit: 15,
end

class Loan < AlmaItem
def self.renew(uniqname:, loan_id:, client: AlmaRestClient.client)
client.post("/users/#{uniqname}/loans/#{loan_id}", query: {op: "renew"})
end

def due_date
DateTime.patron_format(@parsed_response["due_date"]) unless claims_returned?
end
Expand Down
8 changes: 4 additions & 4 deletions spec/models/items/alma/loans_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe Loans do
context "two loans" do
before(:each) do
stub_alma_get_request(url: "users/jbister/loans", body: File.read("./spec/fixtures/loans.json"), query: {expand: "renewable", limit: 15, order_by: "due_date"})
stub_alma_get_request(url: "users/jbister/loans", output: File.read("./spec/fixtures/loans.json"), query: {expand: "renewable", limit: 15, order_by: "due_date"})
end
subject do
Loans.for(uniqname: "jbister")
Expand All @@ -31,7 +31,7 @@
end
context "no loans" do
before(:each) do
stub_alma_get_request(url: "users/jbister/loans", body: File.read("./spec/fixtures/no_loans.json"), query: {expand: "renewable", limit: 15, order_by: "due_date"})
stub_alma_get_request(url: "users/jbister/loans", output: File.read("./spec/fixtures/no_loans.json"), query: {expand: "renewable", limit: 15, order_by: "due_date"})
end
subject do
Loans.for(uniqname: "jbister")
Expand All @@ -53,7 +53,7 @@
@loan = one_loan["item_loan"].delete_at(0).to_json
end
it "requests loans sorted by title" do
stub_alma_get_request(url: "users/jbister/loans", body: @loan, query: {"expand" => "renewable", "offset" => 1, "limit" => 1, "direction" => "DESC", "order_by" => "title"})
stub_alma_get_request(url: "users/jbister/loans", output: @loan, query: {"expand" => "renewable", "offset" => 1, "limit" => 1, "direction" => "DESC", "order_by" => "title"})
loans = Loans.for(uniqname: "jbister", offset: 1, limit: 1, direction: "DESC", order_by: "title")
expect(loans.pagination.next.url).to include("direction=DESC")
expect(loans.pagination.next.url).to include("order_by=title")
Expand All @@ -64,7 +64,7 @@
before(:each) do
one_loan = JSON.parse(File.read("./spec/fixtures/loans.json"))
one_loan["item_loan"].delete_at(0)
stub_alma_get_request(url: "users/jbister/loans", body: one_loan.to_json, query: {"expand" => "renewable", "offset" => 1, "limit" => 1, "order_by" => "due_date"})
stub_alma_get_request(url: "users/jbister/loans", output: one_loan.to_json, query: {"expand" => "renewable", "offset" => 1, "limit" => 1, "order_by" => "due_date"})
end
subject do
Loans.for(uniqname: "jbister", offset: 1, limit: 1)
Expand Down

0 comments on commit d68977a

Please sign in to comment.