Skip to content

Commit

Permalink
Merge pull request #5162 from sul-dlss/t5161-move_user_version
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne authored Aug 9, 2024
2 parents 66e235f + 0504686 commit 1ffe773
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
19 changes: 13 additions & 6 deletions app/jobs/publish_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,35 @@ class PublishJob < ApplicationJob
queue_as :publish_default

# @param [String] druid the identifier of the item to be published
# @param [Integer,nil] user_version the version of the item to be published. If nil, the latest version will be published.
# @param [BackgroundJobResult] background_job_result identifier of a background job result to store status info
# @param [String] workflow Which workflow should this be reported to?
def perform(druid:, background_job_result:, workflow:, log_success: true)
# @param [String,nil] workflow workflow to report to. If nil, no workflow will be reported to.
# @param [Boolean] log_success whether success should be logged
def perform(druid:, background_job_result:, workflow: nil, user_version: nil, log_success: true)
background_job_result.processing!
workflow_process = workflow == 'releaseWF' ? 'release-publish' : 'publish'
cocina_object = CocinaObjectStore.find(druid)

# Note that LogFailureJob / LogSuccessJob will update the BackgroundJobResult.
# If workflow is nil, no workflow will be reported to.
if cocina_object.admin_policy?
return LogFailureJob.perform_later(druid:,
background_job_result:,
workflow:,
workflow_process:,
workflow_process: workflow_process_for(workflow),
output: { errors: [{ title: 'Publishing error', detail: 'Cannot publish an admin policy' }] })
end

Publish::MetadataTransferService.publish(druid:, workflow:)
Publish::MetadataTransferService.publish(druid:, user_version:, workflow:)
EventFactory.create(druid:, event_type: 'publishing_complete', data: { background_job_result_id: background_job_result.id })
return unless log_success

LogSuccessJob.perform_later(druid:,
background_job_result:,
workflow:,
workflow_process:)
workflow_process: workflow_process_for(workflow))
end

def workflow_process_for(workflow)
workflow == 'releaseWF' ? 'release-publish' : 'publish'
end
end
2 changes: 1 addition & 1 deletion app/services/publish/metadata_transfer_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Publish
# If the object does not have user versions, the latest closed version will be published as version 1.
class MetadataTransferService
# @param [String] druid for the object to be published
# @param [String] user_version if a specific version is to be published
# @param [Integer] user_version if a specific version is to be published
# @param [String] workflow (optional) the workflow used for reporting back status to (defaults to 'accessionWF')
def self.publish(druid:, user_version: nil, workflow: 'accessionWF')
new(druid:, workflow:, user_version:).publish
Expand Down
9 changes: 5 additions & 4 deletions app/services/user_version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def self.withdraw(druid:, user_version:, withdraw: true)

# @param [String] druid of the item
# @param [Integer] RepositoryObjectVersion version of the item to move to
# @param [integer] user_version version to move
# @param [Integer] user_version version to move
# @return [UserVersion] The user version
# @raise [UserVersionService::UserVersioningError] RepositoryObject not found for the druid
# @raise [UserVersionService::UserVersioningError] RepositoryObjectVersion not found for the version
Expand All @@ -48,10 +48,11 @@ def self.move(druid:, version:, user_version:)
repository_object_version = repository_object_version(druid:, version:)
raise(UserVersioningError, 'RepositoryObjectVersion not closed') unless repository_object_version.closed?

user_version = user_version_for(druid:, user_version:)
user_version.update(repository_object_version:)
user_version_obj = user_version_for(druid:, user_version:)
user_version_obj.update(repository_object_version:)
PublishJob.perform_later(druid:, user_version:, background_job_result: BackgroundJobResult.create)
EventFactory.create(druid:, event_type: 'user_version_moved', data: { version: user_version.to_s })
user_version
user_version_obj
end

# @param [String] druid of the item
Expand Down
5 changes: 3 additions & 2 deletions spec/jobs/publish_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

it 'invokes the Publish::MetadataTransferService' do
expect(Publish::MetadataTransferService).to have_received(:publish).with(druid:, workflow:).once
expect(Publish::MetadataTransferService).to have_received(:publish).with(druid:, workflow:, user_version: nil).once
end

it 'marks the job as complete' do
Expand All @@ -44,10 +44,11 @@

context 'when log_success is set to false' do
subject(:perform) do
described_class.perform_now(druid:, background_job_result: result, workflow:, log_success: false)
described_class.perform_now(druid:, user_version: 4, background_job_result: result, workflow:, log_success: false)
end

it 'does not mark the job as complete' do
expect(Publish::MetadataTransferService).to have_received(:publish).with(druid:, workflow:, user_version: 4).once
expect(EventFactory).to have_received(:create)

expect(LogSuccessJob).not_to have_received(:perform_later)
Expand Down
2 changes: 2 additions & 0 deletions spec/services/user_version_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

before do
allow(EventFactory).to receive(:create)
allow(PublishJob).to receive(:perform_later)
end

describe '.create' do
Expand Down Expand Up @@ -84,6 +85,7 @@
expect(user_version.repository_object_version).to eq repository_object_version1
user_version_service_move
expect(user_version.reload.repository_object_version).to eq repository_object_version2
expect(PublishJob).to have_received(:perform_later).with(druid:, user_version: 1, background_job_result: BackgroundJobResult)
end
end

Expand Down

0 comments on commit 1ffe773

Please sign in to comment.