Skip to content

Commit

Permalink
Move request to item
Browse files Browse the repository at this point in the history
  • Loading branch information
corylown committed Jul 27, 2023
1 parent db593a9 commit d18fb82
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
14 changes: 3 additions & 11 deletions lib/folio/status_current_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ module Folio
# and any requests associated with the item's instance record
# and creates an equivalent Symphony current location
class StatusCurrentLocation
attr_reader :item, :requests
attr_reader :item

def initialize(item, requests)
def initialize(item)
@item = item
@requests = requests
end

def current_location
Expand Down Expand Up @@ -52,14 +51,7 @@ def symphony_pickup_location_code
end

def service_point_code
request_awaiting_pickup&.dig('pickupServicePoint', 'code')
end

def request_awaiting_pickup
requests.find do |request|
request['itemId'] == item['id'] &&
request['status'] == 'Open - Awaiting pickup'
end
item&.dig('request', 'pickupServicePoint', 'code')
end
end
end
8 changes: 2 additions & 6 deletions lib/folio_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def sirsi_holdings

library_code, home_location_code = LocationsMap.for(item_location_code)
_current_library, current_location = LocationsMap.for(item.dig('location', 'temporaryLocation', 'code'))
current_location ||= Folio::StatusCurrentLocation.new(item, requests).current_location
current_location ||= Folio::StatusCurrentLocation.new(item).current_location

SirsiHolding.new(
call_number: [item.dig('callNumber', 'callNumber'), item['volume'], item['enumeration'], item['chronology']].compact.join(' '),
Expand All @@ -113,7 +113,7 @@ def bound_with_holdings

library_code, home_location_code = LocationsMap.for(item_location_code)
_current_library, current_location = LocationsMap.for(parent_item.dig('location', 'temporaryLocation', 'code'))
current_location ||= Folio::StatusCurrentLocation.new(parent_item, requests).current_location
current_location ||= Folio::StatusCurrentLocation.new(parent_item).current_location
SirsiHolding.new(
call_number: holding['callNumber'],
scheme: call_number_type_map(holding.dig('callNumberType', 'name')),
Expand Down Expand Up @@ -203,10 +203,6 @@ def pieces
@pieces ||= record.fetch('pieces') { client.pieces(instance_id:) }.compact
end

def requests
record['requests'] || []
end

def statistical_codes
@statistical_codes ||= instance.fetch('statisticalCodes') do
my_ids = client.instance(instance_id:).fetch('statisticalCodeIds')
Expand Down
28 changes: 11 additions & 17 deletions lib/traject/readers/folio_postgres_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def each
'permanentLocation' => locations[item['permanentLocationId']],
'temporaryLocation' => locations[item['temporaryLocationId']]
}.compact

item['request']['pickupServicePoint'] = service_points[item['request']['pickupServicePointId']] if item['request']
end

data['holdings'].each do |holding|
Expand All @@ -148,10 +150,6 @@ def each
} if holding.dig('boundWith', 'holding', 'effectiveLocationId')
end

data['requests'].each do |request|
request['pickupServicePoint'] = service_points[request['pickupServicePointId']]
end

yield FolioRecord.new(data)
end
end
Expand Down Expand Up @@ -206,7 +204,14 @@ def sql_query(conditions, addl_from: nil)
'callNumber', item.jsonb -> 'effectiveCallNumberComponents' ||
jsonb_build_object('typeName', cnt.jsonb ->> 'name'),
'electronicAccess', COALESCE(sul_mod_inventory_storage.getElectronicAccessName(COALESCE(item.jsonb #> '{electronicAccess}', '[]'::jsonb)), '[]'::jsonb),
'notes', COALESCE((SELECT json_agg(e || jsonb_build_object('itemNoteTypeName', ( SELECT jsonb ->> 'name' FROM sul_mod_inventory_storage.item_note_type WHERE id = nullif(e ->> 'itemNoteTypeId','')::uuid ))) FROM jsonb_array_elements(item.jsonb -> 'notes') AS e WHERE NOT COALESCE((e ->> 'staffOnly')::bool, false)), '[]'::json)
'notes', COALESCE((SELECT json_agg(e || jsonb_build_object('itemNoteTypeName', ( SELECT jsonb ->> 'name' FROM sul_mod_inventory_storage.item_note_type WHERE id = nullif(e ->> 'itemNoteTypeId','')::uuid ))) FROM jsonb_array_elements(item.jsonb -> 'notes') AS e WHERE NOT COALESCE((e ->> 'staffOnly')::bool, false)), '[]'::json),
'request', CASE WHEN request.id IS NOT NULL THEN
jsonb_build_object(
'id', request.id,
'status', request.jsonb ->> 'status',
'pickupServicePointId', request.jsonb ->> 'pickupServicePointId'
)
END
)
) FILTER (WHERE item.id IS NOT NULL),
'[]'::jsonb),
Expand Down Expand Up @@ -276,17 +281,6 @@ def sql_query(conditions, addl_from: nil)
'instructorObjects', cl.jsonb #> '{instructorObjects}'
)
) FILTER (WHERE cc.id IS NOT NULL),
'[]'::jsonb),
'requests',
COALESCE(
jsonb_agg(
DISTINCT jsonb_build_object(
'id', request.id,
'itemId', request.jsonb ->> 'itemId',
'status', request.jsonb ->> 'status',
'pickupServicePointId', request.jsonb ->> 'pickupServicePointId'
)
) FILTER (WHERE request.id IS NOT NULL),
'[]'::jsonb)
)
FROM sul_mod_inventory_storage.instance vi
Expand Down Expand Up @@ -347,7 +341,7 @@ def sql_query(conditions, addl_from: nil)
ON parentHolding.instanceid = parentInstance.id
-- Requests relation
LEFT JOIN sul_mod_circulation_storage.request request
ON (request.jsonb ->> 'instanceId')::uuid = vi.id
ON (request.jsonb ->> 'itemId')::uuid = item.id
AND request.jsonb ->> 'status' = 'Open - Awaiting pickup'
#{addl_from}
WHERE #{conditions.join(' AND ')}
Expand Down
20 changes: 8 additions & 12 deletions spec/integration/folio_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@
let(:client) { instance_double(FolioClient, instance: client_instance_response, statistical_codes: statistical_codes_response) }
let(:items_and_holdings) { {} }
let(:holding_summaries) { [] }
let(:requests) { [] }

before do
allow(folio_record).to receive(:items_and_holdings).and_return(items_and_holdings)
allow(folio_record).to receive(:courses).and_return([])
allow(folio_record).to receive(:holding_summaries).and_return(holding_summaries)
allow(folio_record).to receive(:requests).and_return(requests)
end

it 'maps the record with sirsi fields' do
Expand Down Expand Up @@ -481,6 +479,14 @@
'status' => 'Awaiting pickup',
'barcode' => '36105080746311',
'_version' => 3,
'request' => { 'id' => '7c8e3f57-6f1b-4d59-a8c6-9b51e32edd38',
'status' => 'Open - Awaiting pickup',
'pickupServicePoint' =>
{ 'pickupServicePointId' => 'b6987737-1e63-44cc-bfb1-2bcf044adcd7',
'code' => 'RUMSEY-MAP',
'name' => 'David Rumsey Map Center',
'pickupLocation' => true,
'discoveryDisplayName' => 'David Rumsey Map Center' } },
'location' =>
{ 'effectiveLocation' =>
{ 'id' => 'bb7bd5d2-5b97-4fc6-9dfd-b26a1c14e43f',
Expand Down Expand Up @@ -584,16 +590,6 @@
{ 'items' => items,
'holdings' => holdings }
end
let(:requests) do
[{ 'id' => '7c8e3f57-6f1b-4d59-a8c6-9b51e32edd38',
'itemId' => '7fdf7094-d30a-5f70-b23e-bc420a82a1d7',
'status' => 'Open - Awaiting pickup',
'pickupServicePoint' =>
{ 'code' => 'RUMSEY-MAP',
'name' => 'David Rumsey Map Center',
'discoveryDisplayName' => 'David Rumsey Map Center' },
'pickupServicePointId' => 'b6987737-1e63-44cc-bfb1-2bcf044adcd7' }]
end

before do
allow(client).to receive(:pieces).and_return([])
Expand Down

0 comments on commit d18fb82

Please sign in to comment.