Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLAC-675: show item availability status #454

Merged
merged 7 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ BOOTSNAP_CACHE_DIR=./tmp

# turn of functionality during FOLIO update
FOLIO_UPDATE_IN_PROGRESS=

CATALOGUE_SERVICES_API_BASE_URL=http://catservices.test
1 change: 1 addition & 0 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ class CatalogController < ApplicationController
config.add_summary_field "extent", field: "extent_ssm"
config.add_summary_field "language", field: "language_ssim"
config.add_summary_field "prefercite", field: "prefercite_html_tesm", helper_method: :render_html_tags
config.add_summary_field "availability", field: "availability_status", accessor: :availability_status
config.add_summary_field "cult_sens_adv_notice",
label: I18n.t("ead_notes.cultural_sens_adv_notice"),
helper_method: :render_html_tags,
Expand Down
8 changes: 8 additions & 0 deletions app/models/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,12 @@ def wrap_in_paragraph(value)
# rubocop:enable Rails/OutputSafety
end
end

def availability_status
holdings, item = CatalogueServicesClient.new.get_item_ids(instance_id: self["folio_instance_id_ssi"])
Rails.logger.debug "solrdoc"
Rails.logger.debug [holdings, item]

CatalogueServicesClient.new.get_requestable(instance_id: self["folio_instance_id_ssi"], holdings_id: holdings, item_id: item)
end
end
75 changes: 75 additions & 0 deletions app/services/catalogue_services_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

class ServiceTokenError < StandardError; end

class HoldingsRequestError < StandardError; end

class ItemRequestError < StandardError; end

class RequestDetailsError < StandardError; end

class UserDetailsError < StandardError; end

class CatalogueServicesClient
MAX_TOKEN_RETRIES = 3

def get_holdings(instance_id:)
conn = setup_connection

res = conn.get("/catalogue-services/folio/instance/#{instance_id}")
if res.status == 200
if res.body.present?
res.body["holdingsRecords"]
end
else
Rails.logger.error "Failed to retrieve holdings for #{instance_id}"
raise HoldingsRequestError.new("Failed to retrieve holdings for #{instance_id}")
end
rescue => e
Rails.logger.error "get_holdings - Failed to connect catalogue-service: #{e.message}"
raise HoldingsRequestError.new("Failed to retrieve holdings for #{instance_id}")
end

def get_holding(instance_id:, holdings_id:, item_id:)
all_holdings = get_holdings(instance_id: instance_id)

# find holdings record
holding = all_holdings.find { |h| h["id"] == holdings_id }

# find item record
item = holding["itemRecords"].find { |i| i["id"] == item_id }

[holding, item]
end

def get_requestable(instance_id:, holdings_id:, item_id:)
all_holdings = get_holdings(instance_id: instance_id)

# find holdings record
holding = all_holdings.find { |h| h["id"] == holdings_id }

# find item record
item = holding["itemRecords"].find { |i| i["id"] == item_id }

item["requestable"] ? item["displayStatus"] : "Not for loan"
end

def get_item_ids(instance_id:)
all_holdings = get_holdings(instance_id: instance_id)

item_id = all_holdings.first["itemRecords"].first["holdingsRecordId"] if all_holdings.first["itemRecords"].any?

holding_id = all_holdings.first["itemRecords"].first["id"] if all_holdings.first["itemRecords"].any?
Rails.logger.debug "catservices"
Rails.logger.debug [item_id, holding_id]
[item_id, holding_id]
end

private

def setup_connection
Faraday.new(url: ENV["CATALOGUE_SERVICES_API_BASE_URL"]) do |f|
f.response :json
end
end
end
7 changes: 7 additions & 0 deletions config/initializers/faraday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "faraday"

# Make sure Faraday requests include a non-default User-Agent, since it will break the specs
# every time Faraday is upgraded.
Faraday.default_connection_options = {headers: {user_agent: "nla-arclight/#{Rails.configuration.version}"}}
5 changes: 5 additions & 0 deletions spec/features/document_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
}
)
.to_return(status: 200, body: solr_response, headers: {})

cat_response = IO.read("spec/files/catalogue_services/08aed703-3648-54d0-80ef-fddb3c635731.json")

WebMock.stub_request(:get, /catalogue-services\/folio\/instance\/(.*)/)
.to_return(status: 200, body: cat_response, headers: {"Content-Type" => "application/json"})
end

it "has correct tab title, including the collection prefix" do
Expand Down
Loading
Loading