Skip to content

Commit

Permalink
show item availability status
Browse files Browse the repository at this point in the history
  • Loading branch information
CEdwardsBlasikiewicz committed Jun 17, 2024
1 parent 3b0f09d commit 151f116
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 0 deletions.
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 @@ -95,4 +95,12 @@ def wrap_in_paragraph(value)
value
end
end

def availability_status
holdings, item = CatalogueServicesClient.new.get_item_ids(instance_id: self["folio_instance_id_ssi"])
p "solrdoc"
pp [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?
p "catservices"
pp [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
Loading

0 comments on commit 151f116

Please sign in to comment.