Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/lupo
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 22, 2019
2 parents 50745e4 + 854c9d5 commit 61b3fc3
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 81 deletions.
2 changes: 1 addition & 1 deletion app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def show

# Use callbacks to share common setup or constraints between actions.
def set_provider
@provider = Provider.unscoped.where("allocator.role_name IN ('ROLE_ALLOCATOR', 'ROLE_MEMBER')").where(deleted_at: nil).where(symbol: params[:id]).first
@provider = Provider.unscoped.where("allocator.role_name IN ('ROLE_FOR_PROFIT_PROVIDER', 'ROLE_CONTRACTUAL_PROVIDER', 'ROLE_CONSORTIUM_LEAD' , 'ROLE_ALLOCATOR', 'ROLE_MEMBER')").where(deleted_at: nil).where(symbol: params[:id]).first
fail ActiveRecord::RecordNotFound unless @provider.present?
end
end
12 changes: 7 additions & 5 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def destroy

# Use callbacks to share common setup or constraints between actions.
def set_provider
@provider = Provider.unscoped.where("allocator.role_name IN ('ROLE_ALLOCATOR', 'ROLE_ADMIN')").where(deleted_at: nil).where(symbol: params[:id]).first
@provider = Provider.unscoped.where("allocator.role_name IN ('ROLE_FOR_PROFIT_PROVIDER', 'ROLE_CONTRACTUAL_PROVIDER', 'ROLE_CONSORTIUM_LEAD' , 'ROLE_ALLOCATOR', 'ROLE_ADMIN')").where(deleted_at: nil).where(symbol: params[:id]).first
fail ActiveRecord::RecordNotFound unless @provider.present?
end

Expand All @@ -194,16 +194,18 @@ def safe_params
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",
"generalContact",{ "generalContact": [:email, "givenName", "familyName"]},
:name, :symbol, :description, :website, :joined, "organizationType", "focusArea", :phone, "contactName", "contactEmail", "isActive", "passwordInput", :country, "billingInformation",{ "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle","roleName",
"technicalContact",{ "technicalContact": [:email, "givenName", "familyName"]},
"secondaryBillingContact",{ "secondaryBillingContact": [:email, "givenName", "familyName"]},
"billingContact",{ "billingContact": [: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,
"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, "roleName" =>:role_name,
"technicalContact" => :technical_contact,
"secondaryBillingContact" => :secondary_billing_contact,
"billingContact" => :billing_contact,
"serviceContact" => :service_contact,
"votingContact" => :voting_contact
}
Expand Down
26 changes: 16 additions & 10 deletions app/models/concerns/authenticable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ def decode_auth_param(username: nil, password: nil)
end

def get_payload(uid: nil, user: nil, password: nil)
roles = {
"ROLE_ADMIN" => "staff_admin",
"ROLE_ALLOCATOR" => "provider_admin",
"ROLE_DATACENTRE" => "client_admin"
}
roles = {
"ROLE_ADMIN" => "staff_admin",
"ROLE_DATACENTRE" => "client_admin",
"ROLE_ALLOCATOR" => "provider_admin",
"ROLE_CONSORTIUM_LEAD" => "provider_admin",
"ROLE_CONTRACTUAL_PROVIDER" => "provider_admin",
"ROLE_FOR_PROFIT_PROVIDER" => "provider_admin"
}
payload = {
"uid" => uid,
"role_id" => roles.fetch(user.role_name, "user"),
Expand Down Expand Up @@ -144,11 +147,14 @@ def generate_token(attributes={})
end

def get_payload(uid: nil, user: nil, password: nil)
roles = {
"ROLE_ADMIN" => "staff_admin",
"ROLE_ALLOCATOR" => "provider_admin",
"ROLE_DATACENTRE" => "client_admin"
}
roles = {
"ROLE_ADMIN" => "staff_admin",
"ROLE_DATACENTRE" => "client_admin",
"ROLE_ALLOCATOR" => "provider_admin",
"ROLE_CONSORTIUM_LEAD" => "provider_admin",
"ROLE_CONTRACTUAL_PROVIDER" => "provider_admin",
"ROLE_FOR_PROFIT_PROVIDER" => "provider_admin"
}
payload = {
"uid" => uid,
"role_id" => roles.fetch(user.role_name, "user"),
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def query(query, options={})
must << { term: { focus_area: options[:focus_area] }} if options[:focus_area].present?

if options[:all_members]
must << { terms: { role_name: %w(ROLE_ALLOCATOR ROLE_MEMBER) }}
must << { terms: { role_name: %w( ROLE_FOR_PROFIT_PROVIDER ROLE_CONTRACTUAL_PROVIDER ROLE_CONSORTIUM_LEAD ROLE_ALLOCATOR ROLE_MEMBER) }}
else
must << { term: { role_name: "ROLE_ALLOCATOR" }}
must << { terms: { role_name: %w( ROLE_FOR_PROFIT_PROVIDER ROLE_CONTRACTUAL_PROVIDER ROLE_CONSORTIUM_LEAD ROLE_ALLOCATOR) }}
end

must_not << { exists: { field: "deleted_at" }} unless options[:include_deleted]
Expand Down
68 changes: 53 additions & 15 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ class Provider < ActiveRecord::Base
validates_uniqueness_of :symbol, message: "This name has already been taken"
validates_format_of :contact_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, message: "contact_email should be an email"
validates_format_of :website, :with => /https?:\/\/[\S]+/ , if: :website?, message: "Website should be an url"
validates_inclusion_of :role_name, :in => %w( ROLE_ALLOCATOR ROLE_MEMBER ROLE_ADMIN ROLE_DEV ), :message => "Role %s is not included in the list"
validates_inclusion_of :role_name, :in => %w( ROLE_FOR_PROFIT_PROVIDER ROLE_CONTRACTUAL_PROVIDER ROLE_CONSORTIUM_LEAD ROLE_ALLOCATOR ROLE_MEMBER ROLE_ADMIN ROLE_DEV ), :message => "Role %s is not included in the list"
validates_inclusion_of :organization_type, :in => %w(nationalInstitution nationalLibrary academicInstitution academicLibrary researchInstitution governmentAgency publisher professionalSociety serviceProvider vendor), :message => "organization type %s is not included in the list", if: :organization_type?
validates_inclusion_of :focus_area, :in => %w(biomedicalAndHealthSciences earthSciences humanities mathematicsAndComputerScience physicalSciencesAndEngineering socialSciences general), :message => "focus area %s is not included in the list", if: :focus_area?
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_format_of :twitter_handle, :with => /\A@[a-zA-Z0-9_]{1,16}\z/, if: :twitter_handle?
validates :technical_contact, contact: true
validates :billing_contact, contact: true
validates :secondary_billing_contact, contact: true
validates :service_contact, contact: true
validates :voting_contact, contact: true
#validates :billing_information, billing_information: true ##commented while we collect data

before_validation :set_region

Expand Down Expand Up @@ -112,12 +114,17 @@ class Provider < ActiveRecord::Base
city: { type: :text },
country: { type: :text },
address: { type: :text }}
indexes :general_contact, type: :object, properties: {
indexes :technical_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
}
indexes :technical_contact, type: :object, properties: {
indexes :billing_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
}
indexes :secondary_billing_contact, type: :object, properties: {
email: { type: :text },
given_name: { type: :text},
family_name: { type: :text }
Expand Down Expand Up @@ -176,8 +183,9 @@ def as_indexed_json(options={})
"country" => billing_country,
"city" => billing_city
},
"general_contact" => general_contact,
"technical_contact" => technical_contact,
"billing_contact" => billing_contact,
"secondary_billing_contact" => secondary_billing_contact,
"service_contact" => service_contact,
"voting_contact" => voting_contact,
"created" => created,
Expand Down Expand Up @@ -212,11 +220,11 @@ def csv
description: description,
website: website,
region: region_human_name,
country: country_name,
country: country_code,
logo_url: logo_url,
focus_area: focus_area,
organization_type: organization_type,
member_type: member_type,
member_type: member_type_label,
billing_address: billing_address,
billing_post_code: billing_post_code,
billing_city: billing_city,
Expand Down Expand Up @@ -276,6 +284,36 @@ def billing_country
billing_information.fetch("country",nil) if billing_information.present?
end

def member_type_label
member_type_labels[role_name]
end

def member_type_labels
{
"ROLE_MEMBER" => "Member Only",
"ROLE_ALLOCATOR" => "Member Provider",
"ROLE_CONSORTIUM_LEAD" => "Consortium Lead",
"ROLE_CONTRACTUAL_PROVIDER" => "Contractual Provider",
"ROLE_ADMIN" => "DataCite admin",
"ROLE_DEV" => "DataCite admin",
"ROLE_FOR_PROFIT_PROVIDER" => "For-profit Provider"
}
end

def member_type
member_types[role_name]
end

def member_types
{
"ROLE_MEMBER" => "member_only",
"ROLE_ALLOCATOR" => "provider",
"ROLE_CONSORTIUM_LEAD" => "consortium_lead",
"ROLE_CONTRACTUAL_PROVIDER" => "contractual_provider",
"ROLE_FOR_PROFIT_PROVIDER" => "for_profit_provider"
}
end

# count years account has been active. Ignore if deleted the same year as created
def cumulative_years
if deleted_at && deleted_at.year > created_at.year
Expand Down Expand Up @@ -322,13 +360,13 @@ def password_input=(value)
write_attribute(:password, encrypt_password_sha256(value)) if value.present?
end

def member_type
if role_name == "ROLE_ALLOCATOR"
"provider"
elsif role_name == "ROLE_MEMBER"
"member_only"
end
end
# def member_type
# if role_name == "ROLE_ALLOCATOR"
# "provider"
# elsif role_name == "ROLE_MEMBER"
# "member_only"
# end
# end

def client_ids
clients.where(deleted_at: nil).pluck(:symbol).map(&:downcase)
Expand Down
14 changes: 9 additions & 5 deletions app/serializers/provider_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ProviderSerializer
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, :general_contact, :technical_contact, :service_contact, :voting_contact, :created, :updated
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, :technical_contact,:billing_contact, :secondary_billing_contact, :service_contact, :voting_contact, :created, :updated

has_many :clients, record_type: :clients
has_many :prefixes, record_type: :prefixes
Expand All @@ -31,14 +31,18 @@ class ProviderSerializer
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 :billing_contact do |object|
object.billing_contact.present? ? object.billing_contact.transform_keys!{ |key| key.to_s.camelcase(:lower) } : {}
end

attribute :secondary_billing_contact do |object|
object.secondary_billing_contact.present? ? object.secondary_billing_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
Expand Down
31 changes: 31 additions & 0 deletions app/validators/billing_information_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class BillingInformationValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# Don't try to validate if we have nothing
return unless value.present?

unless value["city"].present?
record.errors[attribute] << "has no city specified"
end

unless value["state"].present?
record.errors[attribute] << "has no state/province specified"
end
unless value["country"].present?
record.errors[attribute] << "has no country specified"
end
unless value["department"].present?
record.errors[attribute] << "has no department specified"
end

unless value["address"].present?
record.errors[attribute] << "has no street address specified"
end

unless value["postCode"].present? || value["post_code"].present?
record.errors[attribute] << "has no post/zip code specified"
end
unless value["organization"].present?
record.errors[attribute] << "has no organization specified"
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20190521152430_add_billing_contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddBillingContact < ActiveRecord::Migration[5.2]
def up
add_column :allocator, :billing_contact, :json
add_column :allocator, :secondary_billing_contact, :json
end

def down
remove_column :allocator, :billing_contact
remove_column :allocator, :secondary_billing_contact
end
end
9 changes: 9 additions & 0 deletions db/migrate/20190522162430_remove_general_contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RemoveGeneralContact < ActiveRecord::Migration[5.2]
def up
remove_column :allocator, :general_contact
end

def down
add_column :allocator, :general_contact, :json
end
end
Loading

0 comments on commit 61b3fc3

Please sign in to comment.