Skip to content

Commit

Permalink
Merge pull request #981 from sul-dlss/statistical-codes
Browse files Browse the repository at this point in the history
Use FOLIO statistical codes to determine if this should be a database…
  • Loading branch information
cbeer authored Jul 18, 2023
2 parents 6858e55 + 960e4d6 commit ac8a89e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/folio_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def items_and_holdings(instance_id:)
get_json('/inventory-hierarchy/items-and-holdings', method: :post, body: body.to_json)
end

def instance(instance_id:)
get_json("/inventory/instances/#{instance_id}")
end

def statistical_codes
@statistical_codes ||= get_json('/statistical-codes?limit=2000&query=cql.allRecords=1 sortby name').fetch('statisticalCodes')
end

private

# @param [HTTP::Response] response
Expand Down
7 changes: 7 additions & 0 deletions lib/folio_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ def pieces
@pieces ||= record.fetch('pieces') { client.pieces(instance_id:) }.compact
end

def statistical_codes
@statistical_codes ||= instance.fetch('statisticalCodes') do
my_ids = client.instance(instance_id:).fetch('statisticalCodeIds')
client.statistical_codes.select { |code| my_ids.include?(code['id']) }
end
end

def instance
record['instance'] || {}
end
Expand Down
5 changes: 5 additions & 0 deletions lib/traject/config/folio_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ def holdings(record, context)
accumulator << JSON.generate(record.courses)
end

# Statistical code is 'Database'
to_field 'format_main_ssim' do |record, accumulator, _context|
accumulator << 'Database' if record.statistical_codes.any? { |stat_code| stat_code['name'] == 'Database' }
end

##
# Skip records for missing `item_display` field
each_record do |_record, context|
Expand Down
1 change: 1 addition & 0 deletions lib/traject/config/sirsi_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ def reserves_lookup
end
end

# Legacy Symphony Item Type is DATABASE
to_field 'format_main_ssim' do |record, accumulator, context|
accumulator << 'Database' if holdings(record, context).any? { |holding| holding.type == 'DATABASE' }
end
Expand Down
19 changes: 18 additions & 1 deletion lib/traject/readers/folio_postgres_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def queries
end
end

# This gets the UUID of the "Database" statistical code. This is the only statistical code we care about.
def statical_code_database
@statical_code_database ||= begin
response = @connection.exec("SELECT id FROM sul_mod_inventory_storage.statistical_code WHERE jsonb->>'name' = 'Database';")
response.map { |row| row['id'] }.first
end
end

def each
return to_enum(:each) unless block_given?

Expand Down Expand Up @@ -113,7 +121,16 @@ def sql_query(conditions, addl_from: nil)
vi.jsonb || jsonb_build_object(
'suppressFromDiscovery', COALESCE((vi.jsonb ->> 'discoverySuppress')::bool, false),
'electronicAccess', COALESCE(sul_mod_inventory_storage.getElectronicAccessName(COALESCE(vi.jsonb #> '{electronicAccess}', '[]'::jsonb)), '[]'::jsonb),
'notes', jsonb_path_query_array(vi.jsonb->'notes', '$ ? (@.staffOnly == false)')
'statisticalCodes', CASE WHEN '#{statical_code_database}' = ANY(ARRAY(SELECT jsonb_array_elements_text(vi.jsonb->'statisticalCodeIds'))) THEN
jsonb_build_array(
jsonb_build_object(
'id', '#{statical_code_database}',
'name', 'Database'
)
)
ELSE
'[]'::jsonb
END
),
'source_record', COALESCE(jsonb_agg(DISTINCT mr."content"), '[]'::jsonb),
'items',
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/files/whodunit.json
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@
"modeOfIssuanceId": "9d18a02f-5897-4c31-9106-c9abb5c7ae8b",
"publicationRange": [

],
"statisticalCodes": [

],
"alternativeTitles": [

Expand Down
38 changes: 37 additions & 1 deletion spec/integration/folio_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@
JSON.parse(File.read(file_fixture('a14185492.json')))
end

let(:client) { instance_double(FolioClient) }
let(:client_instance_response) do
{
'id' => '4e481734-9304-5035-99b3-afe6153d9835',
'hrid' => 'a2656738',
'statisticalCodeIds' => %w[d3f618e2-9fa9-4623-94ae-1d95d1d66f79]
}
end

let(:statistical_codes_response) do
[
{ 'id' => 'd3f618e2-9fa9-4623-94ae-1d95d1d66f79', 'name' => 'MARCIVE loaded record' },
{ 'id' => '12313', 'name' => 'Not used' },
{ 'id' => '4bc78766-8f34-4b1a-9e39-2a689a4ae998', 'name' => 'Database' }
]
end

let(:client) { instance_double(FolioClient, instance: client_instance_response, statistical_codes: statistical_codes_response) }
let(:items_and_holdings) { {} }
let(:holding_summaries) { [] }

Expand Down Expand Up @@ -720,4 +736,24 @@
it { is_expected.to eq ['EARTH-SCI -|- STACKS -|- -|- v.1, 11 -|- ', 'EARTH-SCI -|- STACKS -|- -|- Supplement: Library keeps latest only -|- '] }
end
end

describe 'the Resource Type facet (format_main_ssim)' do
subject(:field) { result['format_main_ssim'] }

context 'with the database statistical code' do
let(:client_instance_response) do
{
'id' => '4e481734-9304-5035-99b3-afe6153d9835',
'hrid' => 'a2656738',
'statisticalCodeIds' => %w[d3f618e2-9fa9-4623-94ae-1d95d1d66f79 4bc78766-8f34-4b1a-9e39-2a689a4ae998]
}
end

it { is_expected.to eq ['Music score', 'Database'] }
end

context 'without the database statistical code' do
it { is_expected.to eq ['Music score'] }
end
end
end

0 comments on commit ac8a89e

Please sign in to comment.