From ca7a6d842a86a67875d378fd06f9c9e6e593cace Mon Sep 17 00:00:00 2001 From: Bryceson Laing Date: Wed, 2 Sep 2026 08:37:29 -0500 Subject: [PATCH] fix registered date bug --- app/jobs/index_job_doi_registration.rb | 1 + app/models/concerns/indexable.rb | 2 ++ app/models/doi.rb | 2 ++ spec/concerns/indexable_spec.rb | 22 +++++++++++++ spec/jobs/index_job_doi_registration_spec.rb | 33 ++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 spec/jobs/index_job_doi_registration_spec.rb diff --git a/app/jobs/index_job_doi_registration.rb b/app/jobs/index_job_doi_registration.rb index be84fe96a..02930032b 100644 --- a/app/jobs/index_job_doi_registration.rb +++ b/app/jobs/index_job_doi_registration.rb @@ -13,6 +13,7 @@ class IndexJobDoiRegistration < ApplicationJob def perform(obj) EnrichedDoiIndexJob.new.perform(obj.doi) + obj.reload response = obj.__elasticsearch__.index_document Rails.logger.error "[Elasticsearch] Error #{response.inspect}" unless %w(created updated).include?(response["result"]) end diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 5a4fe803f..f0e424bb6 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -22,6 +22,8 @@ module Indexable elsif not %w[Prefix ProviderPrefix ClientPrefix DataciteDoi].include?(self.class.name) IndexJob.perform_later(self) elsif instance_of?(DataciteDoi) + # update url before enqueueing so the index job does not race with after_commit :update_url. + update_url IndexJobDoiRegistration.perform_later(self) if index_sync_enabled? diff --git a/app/models/doi.rb b/app/models/doi.rb index 343ca88eb..7b370ef6c 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -2129,7 +2129,9 @@ def validatable? # providers europ, and DOI registration agencies do their own handle registration, so fetch url from handle system instead def update_url return nil if current_user.nil? || !is_registered_or_findable? + return if @updating_url + @updating_url = true if %w(europ).include?(provider_id) || type == "OtherDoi" UrlJob.perform_later(doi) # TODO better define conditions for updating handle system diff --git a/spec/concerns/indexable_spec.rb b/spec/concerns/indexable_spec.rb index 380867b5a..216f11597 100644 --- a/spec/concerns/indexable_spec.rb +++ b/spec/concerns/indexable_spec.rb @@ -289,6 +289,28 @@ end event.doi_for_source.touch end + + it "sets minted before enqueueing IndexJobDoiRegistration" do + token = User.generate_token(role_id: "client_admin") + unsaved = build( + :doi, + agency: "datacite", + aasm_state: "findable", + minted: nil, + current_user: User.new(token), + ) + expect(unsaved.minted).to be_nil + + allow(unsaved).to receive(:register_url) do + unsaved.update!(minted: Time.zone.now) + end + + expect(IndexJobDoiRegistration).to receive(:perform_later).at_least(:once) do |arg| + expect(DataciteDoi.find(arg.id).minted).to be_present + end + + unsaved.save! + end end end diff --git a/spec/jobs/index_job_doi_registration_spec.rb b/spec/jobs/index_job_doi_registration_spec.rb new file mode 100644 index 000000000..e8bc915f1 --- /dev/null +++ b/spec/jobs/index_job_doi_registration_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe IndexJobDoiRegistration, type: :job do + let(:doi) { create(:doi, minted: nil) } + + after do + clear_enqueued_jobs + clear_performed_jobs + end + + it "queues the job" do + doi + expect { IndexJobDoiRegistration.perform_later(doi) }. + to have_enqueued_job(IndexJobDoiRegistration).on_queue("test_lupo_doi_registration") + end + + it "reloads the DOI before indexing so registered is present" do + minted_at = Time.zone.parse("2026-03-29T21:48:59Z") + doi.update_columns(minted: minted_at) + doi.minted = nil + + elasticsearch = instance_double("ElasticsearchIndexer") + allow(doi).to receive(:__elasticsearch__).and_return(elasticsearch) + expect(elasticsearch).to receive(:index_document) do + expect(doi.minted).to be_present + { "result" => "updated" } + end + + IndexJobDoiRegistration.perform_now(doi) + end +end