From 03bfe3b59873b7350ea207caed0b561c08aac206 Mon Sep 17 00:00:00 2001 From: Justin Littman Date: Fri, 17 May 2024 15:43:43 -0400 Subject: [PATCH] Change EmbargoReleaseService to use RepositoryObject. closes #4997 --- app/models/repository_object_version.rb | 3 ++ app/services/embargo_release_service.rb | 2 +- spec/factories/dros.rb | 28 ----------- spec/factories/repository_object_versions.rb | 49 +++++++++++++++++++ spec/services/embargo_release_service_spec.rb | 6 +-- 5 files changed, 56 insertions(+), 32 deletions(-) diff --git a/app/models/repository_object_version.rb b/app/models/repository_object_version.rb index 4d9857b69..19d09277c 100644 --- a/app/models/repository_object_version.rb +++ b/app/models/repository_object_version.rb @@ -6,8 +6,11 @@ class RepositoryObjectVersion < ApplicationRecord has_many :user_versions, dependent: :destroy has_one :head_version_of, class_name: 'RepositoryObject', inverse_of: :head_version, dependent: nil + scope :in_virtual_objects, ->(member_druid) { where("structural #> '{hasMemberOrders,0}' -> 'members' ? :druid", druid: member_druid) } scope :members_of_collection, ->(collection_druid) { where("structural -> 'isMemberOf' ? :druid", druid: collection_druid) } + # Note that this query is slow. Creating a timestamp index on the releaseDate field is not supported by PG. + scope :embargoed_and_releaseable, -> { where("(access -> 'embargo' ->> 'releaseDate')::timestamp <= ?", Time.zone.now) } validates :version, :version_description, presence: true after_save :update_object_source_id diff --git a/app/services/embargo_release_service.rb b/app/services/embargo_release_service.rb index d3251bf1e..18a36b42b 100644 --- a/app/services/embargo_release_service.rb +++ b/app/services/embargo_release_service.rb @@ -7,7 +7,7 @@ class EmbargoReleaseService def self.release_all # Find objects to process - embargoed_items_to_release = Dro.embargoed_and_releaseable + embargoed_items_to_release = RepositoryObject.currently_embargoed_and_releaseable return Rails.logger.info('No objects to process') if embargoed_items_to_release.none? diff --git a/spec/factories/dros.rb b/spec/factories/dros.rb index 0c8639e69..8aacbc88c 100644 --- a/spec/factories/dros.rb +++ b/spec/factories/dros.rb @@ -69,32 +69,4 @@ } end end - - trait :with_embargo do - access do - { - view: 'dark', - download: 'dark', - embargo: { - releaseDate: 1.year.from_now.iso8601, - view: 'world', - download: 'world' - } - } - end - end - - trait :with_releasable_embargo do - access do - { - view: 'dark', - download: 'dark', - embargo: { - releaseDate: 1.month.ago.iso8601, - view: 'world', - download: 'world' - } - } - end - end end diff --git a/spec/factories/repository_object_versions.rb b/spec/factories/repository_object_versions.rb index ebb491857..24c4e947e 100644 --- a/spec/factories/repository_object_versions.rb +++ b/spec/factories/repository_object_versions.rb @@ -66,6 +66,27 @@ closed_at { nil } end + trait :with_repository_object do + repository_object + + after(:build) do |repository_object_version, _evaluator| + repository_object = repository_object_version.repository_object + # Repository object will already have a version 1, so delete it. + if repository_object_version.version == 1 && repository_object.versions.present? + repository_object.update(head_version: nil, opened_version: nil, last_closed_version: nil) + repository_object.versions.first.destroy! + end + repository_object.versions << repository_object_version + repository_object.head_version = repository_object_version + if repository_object_version.closed_at.present? + repository_object.last_closed_version = repository_object_version + else + repository_object.opened_version = repository_object_version + end + repository_object.save! + end + end + trait :dro_repository_object_version do # rubocop:disable Lint/EmptyBlock end @@ -98,4 +119,32 @@ end structural { nil } end + + trait :with_embargo do + access do + { + view: 'dark', + download: 'dark', + embargo: { + releaseDate: 1.year.from_now.iso8601, + view: 'world', + download: 'world' + } + } + end + end + + trait :with_releasable_embargo do + access do + { + view: 'dark', + download: 'dark', + embargo: { + releaseDate: 1.month.ago.iso8601, + view: 'world', + download: 'world' + } + } + end + end end diff --git a/spec/services/embargo_release_service_spec.rb b/spec/services/embargo_release_service_spec.rb index 20daeefd2..c8e8e62cb 100644 --- a/spec/services/embargo_release_service_spec.rb +++ b/spec/services/embargo_release_service_spec.rb @@ -232,11 +232,11 @@ end describe '#release_all' do - let!(:item_with_releasable_embargo) { create(:ar_dro, :with_releasable_embargo) } + let!(:item_with_releasable_embargo) { create(:repository_object_version, :with_repository_object, :with_releasable_embargo).repository_object } before do - create(:ar_dro) - create(:ar_dro, :with_embargo) + create(:repository_object_version, :with_repository_object) + create(:repository_object_version, :with_repository_object, :with_embargo) allow(described_class).to receive(:release) end