Skip to content

Commit

Permalink
Merge pull request #277 from datacite/add_new_provenance_contacts
Browse files Browse the repository at this point in the history
Add new provenance contacts
  • Loading branch information
richardhallett authored May 20, 2019
2 parents a3bf4c3 + db08cf0 commit e15ee84
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 32 deletions.
25 changes: 19 additions & 6 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def index

def show
options = {}
options[:meta] = {
options[:meta] = {
providers: provider_count(provider_id: params[:id] == "admin" ? nil : params[:id]),
clients: client_count(provider_id: params[:id] == "admin" ? nil : params[:id]),
dois: doi_count(provider_id: params[:id] == "admin" ? nil : params[:id]) }.compact
Expand All @@ -107,7 +107,7 @@ def create
options[:is_collection] = false
options[:params] = {
:current_ability => current_ability,
}
}
render json: ProviderSerializer.new(@provider, options).serialized_json, status: :ok
else
logger.warn @provider.errors.inspect
Expand All @@ -120,7 +120,7 @@ def update
# logger.debug safe_params.inspect
if @provider.update_attributes(safe_params)
options = {}
options[:meta] = {
options[:meta] = {
providers: provider_count(provider_id: params[:id] == "admin" ? nil : params[:id]),
clients: client_count(provider_id: params[:id] == "admin" ? nil : params[:id]),
dois: doi_count(provider_id: params[:id] == "admin" ? nil : params[:id]) }.compact
Expand All @@ -129,7 +129,7 @@ def update
options[:params] = {
:current_ability => current_ability,
}

render json: ProviderSerializer.new(@provider, options).serialized_json, status: :ok
else
logger.warn @provider.errors.inspect
Expand Down Expand Up @@ -192,8 +192,21 @@ def safe_params
# ]
# params.require(:data).permit(:type, attributes: attributes)
ActiveModelSerializers::Deserialization.jsonapi_parse!(
params, only: [:name, :symbol, :description, :website, :joined, "organizationType", "focusArea", :phone, "contactName", "contactEmail", "isActive", "passwordInput", :country, "billingInformation",{ "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle" ],
keys: { "organizationType" => :organization_type, "focusArea" => :focus_area, "contactName" => :contact_name, "contactEmail" => :contact_email, :country => :country_code, "isActive" => :is_active, "passwordInput" => :password_input, "billingInformation" => :billing_information , "postCode" => :post_code, "rorId" => :ror_id, "twitterHandle" =>:twitter_handle }
params,
only: [
:name, :symbol, :description, :website, :joined, "organizationType", "focusArea", :phone, "contactName", "contactEmail", "isActive", "passwordInput", :country, "billingInformation",{ "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle",
"generalContact",{ "generalContact": [:email, "givenName", "familyName"]},
"technicalContact",{ "technicalContact": [:email, "givenName", "familyName"]},
"serviceContact",{ "serviceContact": [:email, "givenName", "familyName"]},
"votingContact",{ "votingContact": [:email, "givenName", "familyName"]}
],
keys: {
"organizationType" => :organization_type, "focusArea" => :focus_area, "contactName" => :contact_name, "contactEmail" => :contact_email, :country => :country_code, "isActive" => :is_active, "passwordInput" => :password_input, "billingInformation" => :billing_information , "postCode" => :post_code, "rorId" => :ror_id, "twitterHandle" =>:twitter_handle,
"generalContact" => :general_contact,
"technicalContact" => :technical_contact,
"serviceContact" => :service_contact,
"votingContact" => :voting_contact
}
)
end
end
32 changes: 30 additions & 2 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Provider < ActiveRecord::Base
validate :freeze_symbol, :on => :update
validates_format_of :ror_id, :with => /\A(?:(http|https):\/\/)?(?:ror\.org\/)?(0\w{6}\d{2})\z/, if: :ror_id?
validates_format_of :twitter_handle, :with => /\A[a-zA-Z0-9_]{1,15}\z/, if: :twitter_handle?
validates :general_contact, contact: true
validates :technical_contact, contact: true
validates :service_contact, contact: true
validates :voting_contact, contact: true

before_validation :set_region

Expand Down Expand Up @@ -108,6 +112,26 @@ class Provider < ActiveRecord::Base
city: { type: :text },
country: { type: :text },
address: { type: :text }}
indexes :general_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
}
indexes :technical_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
}
indexes :service_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
}
indexes :voting_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
}
indexes :created, type: :date
indexes :updated, type: :date
indexes :deleted_at, type: :date
Expand Down Expand Up @@ -152,6 +176,10 @@ def as_indexed_json(options={})
"country" => billing_country,
"city" => billing_city
},
"general_contact" => general_contact,
"technical_contact" => technical_contact,
"service_contact" => service_contact,
"voting_contact" => voting_contact,
"created" => created,
"updated" => updated,
"deleted_at" => deleted_at,
Expand All @@ -162,7 +190,7 @@ def as_indexed_json(options={})
# def self.query_fields
# ['symbol^10', 'name^10', 'contact_name^10', 'contact_email^10', '_all']
# end

def self.query_aggregations
{
years: { date_histogram: { field: 'created', interval: 'year', min_doc_count: 1 } },
Expand Down Expand Up @@ -211,7 +239,7 @@ def csv
def uid
symbol.downcase
end

def cache_key
"providers/#{uid}-#{updated.iso8601}"
end
Expand Down
23 changes: 20 additions & 3 deletions app/serializers/provider_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class ProviderSerializer
set_key_transform :camel_lower
set_type :providers
set_id :uid
# cache_options enabled: true, cache_length: 24.hours ### we cannot filter if we cache
attributes :name, :symbol, :website, :contact_name, :contact_email, :phone, :description, :region, :country, :logo_url, :organization_type, :focus_area, :is_active, :has_password, :joined, :twitter_handle, :billing_information, :ror_id, :created, :updated
# cache_options enabled: true, cache_length: 24.hours ### we cannot filter if we cache

attributes :name, :symbol, :website, :contact_name, :contact_email, :phone, :description, :region, :country, :logo_url, :organization_type, :focus_area, :is_active, :has_password, :joined, :twitter_handle, :billing_information, :ror_id, :general_contact, :technical_contact, :service_contact, :voting_contact, :created, :updated

has_many :clients, record_type: :clients
has_many :prefixes, record_type: :prefixes
Expand All @@ -29,4 +29,21 @@ class ProviderSerializer
attribute :twitter_handle, if: Proc.new { |object, params| params[:current_ability] && params[:current_ability].can?(:read_billing_information, object) == true } do |object|
object.twitter_handle
end

# Convert all contacts json models back to json style camelCase
attribute :general_contact do |object|
object.general_contact.present? ? object.general_contact.transform_keys!{ |key| key.to_s.camelcase(:lower) } : {}
end

attribute :technical_contact do |object|
object.technical_contact.present? ? object.technical_contact.transform_keys!{ |key| key.to_s.camelcase(:lower) } : {}
end

attribute :service_contact do |object|
object.service_contact.present? ? object.service_contact.transform_keys!{ |key| key.to_s.camelcase(:lower) } : {}
end

attribute :voting_contact do |object|
object.voting_contact.present? ? object.voting_contact.transform_keys!{ |key| key.to_s.camelcase(:lower) } : {}
end
end
20 changes: 20 additions & 0 deletions app/validators/contact_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ContactValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# Don't try to validate if we have nothing
return unless value.present?

# Email validation
unless value["email"].present? && value["email"] =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << "has an invalid email"
end

# Name validation
unless value["given_name"].present?
record.errors[attribute] << "has no givenName specified"
end

unless value["family_name"].present?
record.errors[attribute] << "has no familyName specified"
end
end
end
17 changes: 17 additions & 0 deletions db/migrate/20190517154500_add_extra_provider_contacts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class AddExtraProviderContacts < ActiveRecord::Migration[5.2]
def up
add_column :allocator, :general_contact, :json
add_column :allocator, :technical_contact, :json
add_column :allocator, :service_contact, :json
add_column :allocator, :voting_contact, :json
end

def down
remove_column :allocator, :general_contact
remove_column :allocator, :technical_contact
remove_column :allocator, :service_contact
remove_column :allocator, :voting_contact
end
end
42 changes: 28 additions & 14 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# 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,9 +10,9 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_04_26_090046) do
ActiveRecord::Schema.define(version: 2019_05_17_154500) do

create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t|
create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin", force: :cascade do |t|
t.string "name", limit: 191, null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
Expand All @@ -24,7 +22,7 @@
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t|
create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin", force: :cascade do |t|
t.string "key", limit: 191, null: false
t.string "filename", limit: 191, null: false
t.string "content_type", limit: 191
Expand All @@ -35,7 +33,7 @@
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "allocator", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "allocator", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.string "contact_email", null: false
t.string "contact_name", limit: 80, null: false
t.datetime "created"
Expand Down Expand Up @@ -63,11 +61,15 @@
t.json "billing_information"
t.string "twitter_handle", limit: 20
t.string "ror_id"
t.json "general_contact"
t.json "technical_contact"
t.json "service_contact"
t.json "voting_contact"
t.index ["organization_type"], name: "index_allocator_organization_type"
t.index ["symbol"], name: "symbol", unique: true
end

create_table "allocator_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "allocator_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.bigint "allocator", null: false
t.bigint "prefixes", null: false
t.datetime "created_at"
Expand All @@ -77,7 +79,7 @@
t.index ["prefixes"], name: "FKE7FBD674AF86A1C7"
end

create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t|
create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin", force: :cascade do |t|
t.integer "auditable_id"
t.string "auditable_type"
t.integer "associated_id"
Expand All @@ -99,7 +101,18 @@
t.index ["user_id", "user_type"], name: "user_index"
end

create_table "datacentre", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "contacts", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t|
t.bigint "allocator"
t.string "email"
t.string "given_name"
t.string "family_name"
t.string "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["allocator"], name: "fk_rails_5c598567a8"
end

create_table "datacentre", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.text "comments", limit: 4294967295
t.string "contact_email", null: false
t.string "contact_name", limit: 80, null: false
Expand Down Expand Up @@ -127,7 +140,7 @@
t.index ["url"], name: "index_datacentre_on_url", length: 100
end

create_table "datacentre_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "datacentre_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.bigint "datacentre", null: false
t.bigint "prefixes", null: false
t.datetime "created_at"
Expand All @@ -139,7 +152,7 @@
t.index ["prefixes"], name: "FK13A1B3BAAF86A1C7"
end

create_table "dataset", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "dataset", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.datetime "created"
t.string "doi", null: false
t.binary "is_active", limit: 1, null: false
Expand Down Expand Up @@ -194,7 +207,7 @@
t.index ["url"], name: "index_dataset_on_url", length: 100
end

create_table "media", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "media", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.datetime "created"
t.string "media_type", limit: 80
t.datetime "updated"
Expand All @@ -206,7 +219,7 @@
t.index ["url"], name: "index_media_on_url", length: 100
end

create_table "metadata", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "metadata", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.datetime "created"
t.integer "metadata_version"
t.integer "version"
Expand All @@ -218,7 +231,7 @@
t.index ["dataset"], name: "FKE52D7B2F4D3D6B1B"
end

create_table "prefix", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "prefix", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t|
t.datetime "created"
t.string "prefix", limit: 80, null: false
t.integer "version"
Expand All @@ -228,6 +241,7 @@

add_foreign_key "allocator_prefixes", "allocator", column: "allocator", name: "FKE7FBD67446EBD781"
add_foreign_key "allocator_prefixes", "prefix", column: "prefixes", name: "FKE7FBD674AF86A1C7"
add_foreign_key "contacts", "allocator", column: "allocator"
add_foreign_key "datacentre", "allocator", column: "allocator", name: "FK6695D60546EBD781"
add_foreign_key "datacentre_prefixes", "datacentre", column: "datacentre", name: "FK13A1B3BA47B5F5FF"
add_foreign_key "datacentre_prefixes", "prefix", column: "prefixes", name: "FK13A1B3BAAF86A1C7"
Expand Down
20 changes: 20 additions & 0 deletions spec/factories/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@
"address": Faker::Address.street_address,
"postCode": "10777"
}}
general_contact {{
"email": "[email protected]",
"given_name": "Richard",
"family_name": "Hallett"
}}
technical_contact {{
"email": "[email protected]",
"given_name": "Kristian",
"family_name": "Garza"
}}
service_contact {{
"email": "[email protected]",
"given_name": "Martin",
"family_name": "Fenner"
}}
voting_contact {{
"email": "[email protected]",
"given_name": "Robin",
"family_name": "Dasler"
}}
is_active { true }

initialize_with { Provider.where(symbol: symbol).first_or_initialize }
Expand Down
3 changes: 3 additions & 0 deletions spec/models/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
it { should validate_presence_of(:contact_email) }
it { should validate_presence_of(:contact_name) }
it { is_expected.to strip_attribute(:name) }
it {
expect(provider).to be_valid
}
end

describe "admin" do
Expand Down
Loading

0 comments on commit e15ee84

Please sign in to comment.