Skip to content

Commit

Permalink
Use FOLIO statistical codes to determine if this should be a database…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
jcoyne committed Jul 17, 2023
1 parent e646c02 commit 6659efb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 14 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 @@ -176,6 +176,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:)
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 @@ -226,6 +226,11 @@ def holdings(record, context)
accumulator << record.bound_with_parents.to_json
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
35 changes: 22 additions & 13 deletions lib/traject/readers/folio_postgres_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ def sql_query(conditions, addl_from: nil)
'instance',
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)
'electronicAccess', COALESCE(sul_mod_inventory_storage.getElectronicAccessName(COALESCE(vi.jsonb #> '{electronicAccess}', '[]'::jsonb)), '[]'::jsonb),
'statisticalCodes', COALESCE(json_agg(DISTINCT
jsonb_build_object(
'id', sc.id,
'name', sc.jsonb ->> 'name'
)
) FILTER (WHERE sc.id IS NOT NULL), '[]'::json)
),
'source_record', COALESCE(jsonb_agg(DISTINCT mr."content"), '[]'::jsonb),
'items',
Expand Down Expand Up @@ -255,37 +261,40 @@ def sql_query(conditions, addl_from: nil)
)
FROM sul_mod_inventory_storage.instance vi
LEFT JOIN sul_mod_inventory_storage.holdings_record hr
ON hr.instanceid = vi.id
ON hr.instanceid = vi.id
LEFT JOIN sul_mod_inventory_storage.item item
ON item.holdingsrecordid = hr.id
ON item.holdingsrecordid = hr.id
-- Instance's statistical code
LEFT JOIN LATERAL jsonb_array_elements_text(vi.jsonb -> 'statisticalCodeIds') uuid_stats_code(id) ON TRUE
LEFT JOIN statistical_code sc ON uuid_stats_code.id::uuid = sc.id
-- Course information related to items on reserve
LEFT JOIN sul_mod_courses.coursereserves_reserves cr
ON (cr.jsonb ->> 'itemId')::uuid = item.id
ON (cr.jsonb ->> 'itemId')::uuid = item.id
LEFT JOIN sul_mod_courses.coursereserves_courselistings cl
ON cl.id = cr.courselistingid
ON cl.id = cr.courselistingid
LEFT JOIN sul_mod_courses.coursereserves_courses cc
ON cc.courselistingid = cl.id
ON cc.courselistingid = cl.id
-- Item's Effective location relation
LEFT JOIN viewLocations itemEffLoc
ON item.effectivelocationid = itemEffLoc.locId
ON item.effectivelocationid = itemEffLoc.locId
-- Item's Permanent location relation
LEFT JOIN viewLocations itemPermLoc
ON item.permanentlocationid = itemPermLoc.locId
ON item.permanentlocationid = itemPermLoc.locId
-- Item's Temporary location relation
LEFT JOIN viewLocations itemTempLoc
ON item.temporarylocationid = itemTempLoc.locId
ON item.temporarylocationid = itemTempLoc.locId
-- Item's Material type relation
LEFT JOIN sul_mod_inventory_storage.material_type mt
ON item.materialtypeid = mt.id
ON item.materialtypeid = mt.id
-- Item's Call number type relation
LEFT JOIN sul_mod_inventory_storage.call_number_type cnt
ON (item.jsonb #>> '{effectiveCallNumberComponents, typeId}')::uuid = cnt.id
ON (item.jsonb #>> '{effectiveCallNumberComponents, typeId}')::uuid = cnt.id
-- Item's Damaged status relation
LEFT JOIN sul_mod_inventory_storage.item_damaged_status itemDmgStat
ON (item.jsonb ->> 'itemDamagedStatusId')::uuid = itemDmgStat.id
ON (item.jsonb ->> 'itemDamagedStatusId')::uuid = itemDmgStat.id
-- Item's Permanent loan type relation
LEFT JOIN sul_mod_inventory_storage.loan_type plt
ON item.permanentloantypeid = plt.id
ON item.permanentloantypeid = plt.id
-- Item's Temporary loan type relation
LEFT JOIN sul_mod_inventory_storage.loan_type tlt
ON item.temporaryloantypeid = tlt.id
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
18 changes: 17 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 4bc78766-8f34-4b1a-9e39-2a689a4ae998]
}
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

0 comments on commit 6659efb

Please sign in to comment.