Skip to content

Commit

Permalink
🚧 Bring over RelatedMembershipsJob
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkkwang committed Jun 4, 2024
1 parent 9efa8ad commit 560eeb6
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions app/jobs/bulkrax/related_memberships_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

# TODO(bkiahstroud): needs spec - see Bulkrax's child_relationships_job_spec for inspiration
module Bulkrax
class RelatedMembersError < RuntimeError; end

class RelatedMembershipsJob < ApplicationJob
queue_as :import

def perform(*args)
@record_with_relationship_id = args[0]
@related_item_entry_ids = args[1]
@relationship_type = args[2]

related_membership
rescue Bulkrax::RelatedMembersError # Not all of the Works exist yet; reschedule
reschedule(@record_with_relationship_id, @related_item_entry_ids, @relationship_type)
end

private

# add related works to work
def related_membership
related_members = []

related_works_hash.each { |k, _v| related_members << k }
relate_members_to_record(related_members) if related_members.present?
end

## Work-Work membership is added to the record as related_members
# @param [Array] related_members: id(s) of Work object(s) that are related to the record
def relate_members_to_record(related_members)
# build related_members_attributes
attrs = {
id: entry&.factory&.find&.id,
related_members_attributes: related_members.each.each_with_object({}) do |related_member, ids|
# NOTE(bkiahstroud): generate random string for index; using a traditional index causes issues
ids[SecureRandom.alphanumeric(15)] = { id: related_member, relationship: @relationship_type }
end
}

Bulkrax::RelatedMemberObjectFactory.new(attrs, entry.identifier, false, user, entry.factory_class).run
rescue StandardError => e
entry.status_info(e)
entry.save!
end

##
# @return [Hash] id of Work object with nested hash of the Work's source_identifier
def related_works_hash
@related_works_hash ||= related_member_entries.each_with_object({}) do |related_member_entry, hash|
work = related_member_entry.factory.find
# If we can't find the Work, raise a custom error
raise RelatedMembersError if work.blank?
hash[work.id] = { source_identifier: related_member_entry.identifier }
end
end

# @return [Array] of entries to relate to the record
def related_member_entries
@related_member_entries ||= @related_item_entry_ids.map { |e| Bulkrax::Entry.find(e) }
end

##
# @return [Bulkrax::Entry] of the record
def entry
@entry ||= Bulkrax::Entry.find(@record_with_relationship_id)
end

def user
@user ||= entry.importerexporter.user
end

def reschedule(record_with_relationship_id, related_member_entry_ids, relationship_type)
RelatedMembershipsJob
.set(wait: 5.minutes)
.perform_later(record_with_relationship_id, related_member_entry_ids, relationship_type)
end
end
end

0 comments on commit 560eeb6

Please sign in to comment.