Skip to content

Commit

Permalink
Merge pull request #855 from datacite/prefix-bug
Browse files Browse the repository at this point in the history
Prefix bug - moving prefix assignment to lupo create repository funct…
  • Loading branch information
svogt0511 authored Jan 30, 2023
2 parents c4ce69e + 9c23505 commit 5a768c4
Show file tree
Hide file tree
Showing 27 changed files with 670 additions and 170 deletions.
43 changes: 43 additions & 0 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Client < ApplicationRecord
message: "Client type %s is not included in the list"
validates_associated :provider
validate :check_id, on: :create
validate :check_prefix, on: :create
validate :freeze_symbol, on: :update
validate :check_issn, if: :issn?
validate :check_certificate, if: :certificate?
Expand All @@ -97,6 +98,7 @@ class Client < ApplicationRecord
before_validation :set_defaults
before_create { self.created = Time.zone.now.utc.iso8601 }
before_save { self.updated = Time.zone.now.utc.iso8601 }
after_create :assign_prefix
after_create_commit :create_reference_repository
after_update_commit :update_reference_repository
after_destroy_commit :destroy_reference_repository
Expand Down Expand Up @@ -915,6 +917,47 @@ def check_id
end
end

def get_prefix
provider_prefix = (provider.present? && provider.provider_prefixes.present?) ? provider.provider_prefixes.select { |_provider_prefix| (_provider_prefix.state == "without-repository") }.first : nil
prefix = Prefix.all.count > 0 ? Prefix.all.select { |_prefix| (_prefix.state == "unassigned") }.first : nil

provider_prefix || prefix || nil
end

def check_prefix
if !get_prefix
errors.add(
:base,
"No prefixes available. Unable to create repository.",
)
end
end

def assign_prefix
available_prefix = get_prefix
if !available_prefix
errors.add(
:base,
"No prefixes available. Created repository, but a prefix was not assigned. Contact support to get a prefix.",
)
else
prefix, provider_prefix = nil
available_prefix.class.name == "Prefix" ? prefix = available_prefix : provider_prefix = available_prefix

if !provider_prefix.present?
provider_prefix = ProviderPrefix.create(
provider_id: provider.symbol, prefix_id: prefix.uid
)
end

ClientPrefix.create(
client_id: symbol,
provider_prefix_id: provider_prefix.uid,
prefix_id: provider_prefix.prefix.uid,
)
end
end

def user_url
ENV["VOLPINO_URL"] + "/users?client-id=" + symbol.downcase
end
Expand Down
14 changes: 6 additions & 8 deletions db/seeds/development/base.seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
if Provider.where(symbol: "ADMIN").blank?
FactoryBot.create(:provider, symbol: "ADMIN")
end
# Need available prefixes for repository creation.
if Prefix.where(uid: "10.14454").blank?
FactoryBot.create(:prefix, uid: "10.14454")
end
provider =
Provider.where(symbol: "DATACITE").first ||
FactoryBot.create(:provider, symbol: "DATACITE")
# No need anymore to call FactoryBot.create to establish provider/client prefix relationships.
# That is done in client creation.
client =
Client.where(symbol: "DATACITE.TEST").first ||
FactoryBot.create(
Expand All @@ -23,14 +29,6 @@
symbol: ENV["MDS_USERNAME"],
password_input: ENV["MDS_PASSWORD"],
)
if Prefix.where(uid: "10.14454").blank?
prefix = FactoryBot.create(:prefix, uid: "10.14454")
### This creates both the client_prefix and the pprovider association
FactoryBot.create(
:client_prefix,
client_id: client.symbol, prefix_id: prefix.uid,
)
end
dois = FactoryBot.create_list(:doi, 10, client: client, state: "findable")
FactoryBot.create_list(:event_for_datacite_related, 3, obj_id: dois.first.doi)
FactoryBot.create_list(:event_for_datacite_usage, 2, obj_id: dois.first.doi)
22 changes: 18 additions & 4 deletions spec/concerns/authenticable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,35 @@
end

context "draft doi" do
let(:consortium) do
let!(:consortium) do
create(:provider, symbol: "DC", role_name: "ROLE_CONSORTIUM")
end
let(:provider) do

let!(:provider) do
create(
:provider,
symbol: "DATACITE",
consortium: consortium,
role_name: "ROLE_CONSORTIUM_ORGANIZATION",
)
end
let(:client) do
let!(:client) do
create(:client, provider: provider, symbol: "DATACITE.RPH")
end
let(:doi) { create(:doi, client: client) }
let!(:prefix) { create(:prefix, uid: "10.14454") }
let!(:provider_prefix) do
create(:provider_prefix, provider: provider, prefix: prefix)
end
let!(:client_prefix) do
create(
:client_prefix,
client: client,
prefix: prefix,
provider_prefix: provider_prefix
)
end

let!(:doi) { create(:doi, client: client) }

it "staff_admin" do
token = User.generate_token(role_id: "staff_admin")
Expand Down
5 changes: 2 additions & 3 deletions spec/concerns/helpable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

describe Doi, vcr: true do
describe Doi, vcr: true, elasticsearch: true do
subject { create(:doi) }

context "generate_random_provider_symbol" do
Expand Down Expand Up @@ -286,8 +286,7 @@
create(
:client,
provider: provider,
symbol: ENV["MDS_USERNAME"],
password: ENV["MDS_PASSWORD"],
password_input: ENV["MDS_PASSWORD"]
)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/concerns/indexable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

describe "Indexable", vcr: true do
describe "Indexable", vcr: true, elasticsearch: true do
subject { create(:doi) }

xit "send_message" do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5a768c4

Please sign in to comment.