Skip to content

Commit

Permalink
Merge pull request #790 from datacite/doi-estimate-field
Browse files Browse the repository at this point in the history
Doi estimate field
  • Loading branch information
svogt0511 authored Mar 9, 2022
2 parents cb5e267 + c675f6c commit 4c2dfd3
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ def safe_params
{ "secondaryServiceContact": [:uid, :email, "givenName", "familyName", :name] },
"votingContact",
{ "votingContact": [:uid, :email, "givenName", "familyName", :name] },
"doiEstimate",
],
keys: {
"displayName" => :display_name,
Expand All @@ -486,6 +487,7 @@ def safe_params
"salesforceId" => :salesforce_id,
"fromSalesforce" => :from_salesforce,
"globusUuid" => :globus_uuid,
"doiEstimate" => :doi_estimate,
},
)
end
Expand Down
15 changes: 0 additions & 15 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,22 +648,10 @@ def to_jsonapi
"deleted_at" => deleted_at ? deleted_at.iso8601 : nil,
}

Rails.logger.info "SKV - TO_JSONAPI BEGIN: uid, doi_counts, dois_total, dois_current_year, dois_last_year"
Rails.logger.info uid
Rails.logger.info doi_counts
Rails.logger.info dois_total
Rails.logger.info dois_current_year
Rails.logger.info dois_last_year
Rails.logger.info "SKV - TO_JSONAPI MIDDLE: attributes"
Rails.logger.info attributes
Rails.logger.info "SKV - TO_JSONAPI END"

{ "id" => symbol.downcase, "type" => "clients", "attributes" => attributes }
end

def self.export(query: nil)
Rails.logger.info "SKV - EXPORTING CLIENTS - BEGIN"

# Loop through all clients
i = 0
page = { size: 1_000, number: 1 }
Expand All @@ -688,9 +676,6 @@ def self.export(query: nil)
page_num += 1
end

Rails.logger.info query
Rails.logger.info "SKV - EXPORTING CLIENTS - END"

"#{i} clients exported."
end

Expand Down
42 changes: 41 additions & 1 deletion app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Provider < ApplicationRecord
version
doi_quota_allowed
doi_quota_used
doi_estimate
]

# include helper module for caching infrequently changing resources
Expand Down Expand Up @@ -149,6 +150,9 @@ class Provider < ApplicationRecord
# validates :voting_contact, contact: true
# validates :billing_information, billing_information: true

# validates :doi_estimate, numericality: { only_integer: true, greater_than_or_equal_to: 0 } if :member_type === "consortium_organization"
validate :doi_estimate_field

strip_attributes

has_many :clients, foreign_key: :allocator
Expand Down Expand Up @@ -363,6 +367,7 @@ class Provider < ApplicationRecord
updated_at: { type: :date },
deleted_at: { type: :date }
}
indexes :doi_estimate, type: :integer
end
end

Expand Down Expand Up @@ -428,7 +433,8 @@ def as_indexed_json(options = {})
nil
else
contacts.map { |m| m.try(:as_indexed_json, exclude_associations: true) }
end
end,
"doi_estimate" => doi_estimate
}
end

Expand Down Expand Up @@ -537,6 +543,7 @@ def csv
created: created,
updated: updated,
deleted_at: deleted_at,
doi_estimate: doi_estimate,
}.values

CSV.generate { |csv| csv << provider }
Expand Down Expand Up @@ -826,6 +833,33 @@ def user_url
ENV["VOLPINO_URL"] + "/users?provider-id=" + symbol.downcase
end

def activity_id_not_changed
if activity_id_changed? && self.persisted?
errors.add(:activity_id, "Change of activity_id not allowed!")
end
end

def doi_estimate_field
if member_type == "consortium_organization"
begin
num = Integer(doi_estimate)
if num < 0
errors.add(
:doi_estimate,
:doi_estimate_invalid,
value: "The doi_estimate must be a nonnegative integer.",
)
end
rescue
errors.add(
:doi_estimate,
:doi_estimate_invalid,
value: "The doi_estimate must be a nonnegative integer.",
)
end
end
end

# attributes to be sent to elasticsearch index
def to_jsonapi
attributes = {
Expand Down Expand Up @@ -859,6 +893,7 @@ def to_jsonapi
"created" => created.iso8601,
"updated" => updated.iso8601,
"deleted_at" => deleted_at ? deleted_at.iso8601 : nil,
"doi_estimate" => doi_estimate,
}

{
Expand Down Expand Up @@ -912,6 +947,11 @@ def set_defaults
self.billing_information = {} if billing_information.blank?
self.consortium_id = nil unless member_type == "consortium_organization"
self.non_profit_status = "non-profit" if non_profit_status.blank?
if member_type == "consortium_organization"
self.doi_estimate = doi_estimate.to_i
else
self.doi_estimate = 0
end

# custom filename for attachment as data URLs don't support filenames
if logo_content_type.present?
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/provider_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class ProviderSerializer
:voting_contact,
:has_required_contacts,
:created,
:updated
:updated,
:doi_estimate

has_many :clients, record_type: :clients
has_many :prefixes, record_type: :prefixes
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20220217020855_add_doi_estimate_to_allocator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AddDoiEstimateToAllocator < ActiveRecord::Migration[5.2]
def change
add_column :allocator, :doi_estimate, :integer, default: 0, null: false
end


def self.down
remove_column :doi_estimate
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

#
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -12,7 +13,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_11_12_201512) do
ActiveRecord::Schema.define(version: 2022_02_17_020855) do
create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t|
t.string "name", limit: 191, null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -77,6 +78,7 @@
t.string "logo_content_type"
t.bigint "logo_file_size"
t.datetime "logo_updated_at"
t.integer "doi_estimate", default: 0, null: false
t.index ["globus_uuid"], name: "index_allocator_on_globus_uuid"
t.index ["organization_type"], name: "index_allocator_organization_type"
t.index ["symbol"], name: "symbol", unique: true
Expand Down
25 changes: 25 additions & 0 deletions spec/models/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@
end
end

describe "doi estimate" do
subject { build(:provider) }

it "valid" do
[0, 98765, "0", "98765"].each do |value|
subject.member_type = "consortium_organization"
subject.doi_estimate = value
expect(subject.save).to be true
expect(subject.doi_estimate).to be_a_kind_of(Integer)
expect(subject.doi_estimate).to eq(value.to_i)
end
end

it "invalid" do
["-123", -123].each do |value|
subject.member_type = "consortium_organization"
subject.doi_estimate = value
expect(subject.save).to be false
expect(subject.errors.details).to eq(
doi_estimate: [{ error: :doi_estimate_invalid, value: "The doi_estimate must be a nonnegative integer." }]
)
end
end
end

describe "from_salesforce" do
subject { build(:provider) }

Expand Down

0 comments on commit 4c2dfd3

Please sign in to comment.