Skip to content

Commit

Permalink
created organizations_controller. datacite/bracco#210
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 26, 2019
1 parent cf93e01 commit 4a91517
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 6 deletions.
165 changes: 165 additions & 0 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
class OrganizationsController < ApplicationController
include ActionController::MimeResponds
include Countable

before_action :set_provider, only: [:show]

def index
sort = case params[:sort]
when "relevance" then { "_score" => { order: 'desc' }}
when "name" then { "name.raw" => { order: 'asc' }}
when "-name" then { "name.raw" => { order: 'desc' }}
when "created" then { created: { order: 'asc' }}
when "-created" then { created: { order: 'desc' }}
else { "name.raw" => { order: 'asc' }}
end

page = page_from_params(params)

if params[:id].present?
response = Provider.find_by_id(params[:id])
elsif params[:ids].present?
response = Provider.find_by_id(params[:ids], page: page, sort: sort)
else
response = Provider.query(params[:query],
year: params[:year],
region: params[:region],
consortium_lead_id: params[:provider_id],
organization_type: params[:organization_type],
focus_area: params[:focus_area],
page: page,
sort: sort)
end

begin
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
regions = total > 0 ? facet_by_region(response.response.aggregations.regions.buckets) : nil
member_types = total > 0 ? facet_by_key(response.response.aggregations.member_types.buckets) : nil
organization_types = total > 0 ? facet_by_key(response.response.aggregations.organization_types.buckets) : nil
focus_areas = total > 0 ? facet_by_key(response.response.aggregations.focus_areas.buckets) : nil

@providers = response.results
respond_to do |format|
format.json do
options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
years: years,
regions: regions,
"memberTypes" => member_types,
"organizationTypes" => organization_types,
"focusAreas" => focus_areas
}.compact

options[:links] = {
self: request.original_url,
next: @providers.blank? ? nil : request.base_url + "/providers?" + {
query: params[:query],
year: params[:year],
region: params[:region],
"member_type" => params[:member_type],
"organization_type" => params[:organization_type],
"focus-area" => params[:focus_area],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: sort }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:params] = {
:current_ability => current_ability,
}

fields = fields_from_params(params)
if fields
render json: ProviderSerializer.new(@providers, options.merge(fields: fields)).serialized_json, status: :ok
else
render json: ProviderSerializer.new(@providers, options).serialized_json, status: :ok
end
end
header = %w(
accountName
fabricaAccountId
year
is_active
accountDescription
accountWebsite
region
country
logo_url
focusArea
organisation_type
accountType
generalContactEmail
groupEmail
technicalContactEmail
technicalContactGivenName
technicalContactFamilyName
secondaryTechnicalContactEmail
secondaryTechnicalContactGivenName
secondaryTechnicalContactFamilyName
serviceContactEmail
serviceContactGivenName
serviceContactFamilyName
secondaryServiceContactEmail
secondaryServiceContactGivenName
secondaryServiceContactFamilyName
votingContactEmail
votingContactGivenName
votingContactFamilyName
billingStreet
billingPostalCode
billingCity
department
billingOrganization
billingState
billingCountry
billingContactEmail
billingContactGivenName
billingontactFamilyName
secondaryBillingContactEmail
secondaryBillingContactGivenName
secondaryBillingContactFamilyName
twitter
ror_id
member_type
joined
created
updated
deleted_at)
format.csv { render request.format.to_sym => response.records.to_a, header: header }
end
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
end
end

def show
options = {}
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
options[:include] = @include
options[:is_collection] = false
options[:params] = {
:current_ability => current_ability,
}
render json: ProviderSerializer.new(@provider, options).serialized_json, status: :ok
end

protected

def set_provider
@provider = Provider.unscoped.where("allocator.role_name IN ('ROLE_FOR_PROFIT_PROVIDER', 'ROLE_CONTRACTUAL_PROVIDER', 'ROLE_CONSORTIUM_LEAD' , 'ROLE_CONSORTIUM_ORGANIZATION', 'ROLE_ALLOCATOR', 'ROLE_ADMIN', 'ROLE_MEMBER', 'ROLE_REGISTRATION_AGENCY')").where(deleted_at: nil).where(symbol: params[:id]).first
fail ActiveRecord::RecordNotFound unless @provider.present?
end
end
2 changes: 1 addition & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def index
exclude_registration_agencies: params[:exclude_registration_agencies],
year: params[:year],
region: params[:region],
consortium_lead: params[:consortium_lead],
consortium_lead_id: params[:consortium_lead_id],
member_type: params[:member_type],
organization_type: params[:organization_type],
focus_area: params[:focus_area],
Expand Down
1 change: 0 additions & 1 deletion app/graphql/types/prefix_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def states
collection = collection.state(args[:state].underscore.dasherize) if args[:state].present?
collection = collection.query(args[:query]) if args[:query].present?

puts collection.inspect
if args[:state].present?
[{ id: args[:state],
title: args[:state].underscore.humanize,
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def query(query, options={})
if self.name == "Provider"
must << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { term: { region: options[:region].upcase }} if options[:region].present?
must << { term: { consortium_lead_id: options[:consortium_lead].upcase }} if options[:consortium_lead].present?
must << { term: { consortium_lead_id: options[:consortium_lead_id].upcase }} if options[:consortium_lead_id].present?
must << { term: { member_type: options[:member_type] }} if options[:member_type].present?
must << { term: { organization_type: options[:organization_type] }} if options[:organization_type].present?
must << { term: { focus_area: options[:focus_area] }} if options[:focus_area].present?
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

resources :providers do
resources :clients, constraints: { :id => /.+/ }, shallow: true
resources :organizations, constraints: { :id => /.+/ }, shallow: true
resources :dois, constraints: { :id => /.+/ }
resources :prefixes, constraints: { :id => /.+/ }
end
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 @@ -50,6 +50,9 @@
expect(subject.member_type).to eq("consortium_lead")
expect(subject.member_type_label).to eq("Consortium Lead")
expect(subject.consortium_organizations.length).to eq(3)
consortium_organization = subject.consortium_organizations.last
expect(consortium_organization.consortium_lead_id).to eq("VIVA")
expect(consortium_organization.member_type).to eq("consortium_organization")
end
end

Expand Down
1 change: 0 additions & 1 deletion spec/models/usage_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
id = "https://api.test.datacite.org/reports/0498876e-dd55-42b0-b2a6-850df004a0e4"
usage_reports = UsageReport.find_by_id(id)
expect(usage_reports[:data].size).to eq(1)
puts usage_reports[:data].first
expect(usage_reports[:data].first).to eq(:id=>"https://api.test.datacite.org/reports/0498876e-dd55-42b0-b2a6-850df004a0e4", :reporting_period=>{:begin_date=>"2018-10-01", :end_date=>"2018-10-31"})
end

Expand Down
1 change: 0 additions & 1 deletion spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,6 @@

it 'updates the record' do
patch "/dois/10.14454/q6g15xs4", valid_attributes, headers
puts last_response.body
expect(last_response.status).to eq(201)
expect(json.dig('data', 'attributes', 'url')).to eq("https://datashare.ucsf.edu/stash/dataset/doi:10.7272/Q6G15XS4")
expect(json.dig('data', 'attributes', 'doi')).to eq("10.14454/q6g15xs4")
Expand Down
1 change: 0 additions & 1 deletion spec/requests/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
post uri, params, headers
expect(last_response.status).to eq(401)

puts last_response.body
expect(json["errors"]).to eq(errors)
expect(json["data"]).to be_blank
end
Expand Down
20 changes: 20 additions & 0 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,26 @@
expect(json.dig('data', 'attributes', 'name')).to eq("Figshare")
expect(json.dig('data', 'attributes', 'memberType')).to eq("consortium_organization")
expect(json.dig('data', 'relationships', 'consortiumLead', 'data', 'id')).to eq(consortium_lead.symbol.downcase)

sleep 1

get "/providers/#{consortium_lead.symbol.downcase}?include=consortium-organizations", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('included', 0, 'attributes', 'systemEmail')).to eq("[email protected]")
expect(json.dig('included', 0, 'attributes', 'name')).to eq("Figshare")
expect(json.dig('included', 0, 'attributes', 'memberType')).to eq("consortium_organization")
expect(json.dig('included', 0, 'relationships', 'consortiumLead', 'data', 'id')).to eq(consortium_lead.symbol)

get "/providers?consortium-lead-id=#{consortium_lead.symbol.downcase}", nil, headers

expect(last_response.status).to eq(200)
puts last_response.body

get "/providers/#{consortium_lead.symbol.downcase}/organizations", nil, headers

expect(last_response.status).to eq(200)
puts last_response.body
end
end

Expand Down

0 comments on commit 4a91517

Please sign in to comment.