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

Index holdings data to mhld_struct #876

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
69 changes: 69 additions & 0 deletions lib/traject/config/sirsi_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,75 @@ def library_has(field)
[field['a'], field['z']].compact.join(' ')
end

to_field 'mhld_struct' do |record, accumulator, _context|
location_groups = Traject::MarcExtractor.new('852:853:863:866:867:868').collect_matching_lines(record) { |field, _spec, _extractor| field }.slice_before { |field| field.tag == '852' }

result = location_groups.each_with_object({}) do |fields, hash|
next unless fields.first.tag == '852'

fields_by_tag = fields.group_by(&:tag)

marc852 = fields.first

library = marc852.subfields.select { |sf| sf.code == 'b' }.map(&:value).compact.join(' ')
library = nil if library.empty?
location = marc852.subfields.select { |sf| sf.code == 'c' }.map(&:value).compact.join(' ')
location = nil if location.empty?

comments = marc852.subfields.select { |sf| sf.code == '3' || sf.code == 'z' }.map(&:value).compact
next if comments.any? { |c| c =~ /all holdings transferred/i }

# Reset to process new 852
mhld_field = MhldField.new

next if skipped_locations[location] || missing_locations[location]

mhld_result = {}

comment = comments.reject(&:empty?).join(' ')

marc853s = fields_by_tag.fetch('853', []).each_with_object({}) do |field, h|
link_seq_num = field.select { |sf| sf.code == '8' }.map(&:value).first.to_i
h[link_seq_num] = field
end

most_recent_marc863 = fields_by_tag.fetch('863', []).select { |field| field['8'].present? }.max_by do |field|
link_num, seq_num = field['8'].split('.', 2).map(&:to_i)
[link_num, seq_num || -1]
end

mhld_result[:holdings] = (fields_by_tag['866'] || []).map do |field|
{ statement: field['a'], note: field['z'] }.reject { |_k, v| v.blank? }
end

mhld_result[:supplements] = (fields_by_tag['867'] || []).map do |field|
{ statement: field['a'], note: field['z'] }.reject { |_k, v| v.blank? }
end

mhld_result[:index] = (fields_by_tag['868'] || []).map do |field|
{ statement: field['a'], note: field['z'] }.reject { |_k, v| v.blank? }
end

mhld_result[:note] = comment if comment.present?

if most_recent_marc863 && (marc852['='] || mhld_result[:holdings].none? || mhld_result[:holdings].any? { |statement| statement[:statement].end_with? '-' } || mhld_result[:supplements] || mhld_result[:index])
most_recent_marc863_link_num = most_recent_marc863['8']&.split('.')&.first.to_i

if most_recent_marc863_link_num != 0
pattern = marc853s[most_recent_marc863_link_num]
mhld_result[:latest_received] = mhld_field.get863display_value(pattern, most_recent_marc863)
end
end

next if mhld_result.all? { |_k, v| v.blank? }

hash[library] ||= {}
hash[library][location] ||= mhld_result
end

accumulator << result unless result.blank?
end

to_field 'bookplates_display' do |record, accumulator|
Traject::MarcExtractor.new('979').collect_matching_lines(record) do |field, _spec, _extractor|
file = field['c']
Expand Down
Loading