From 4a91517c5d1a272ce788880e0f9feba723cbe5db Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 26 Jul 2019 10:30:02 +0200 Subject: [PATCH] created organizations_controller. datacite/bracco#210 --- app/controllers/organizations_controller.rb | 165 ++++++++++++++++++ app/controllers/providers_controller.rb | 2 +- .../types/prefix_connection_with_meta_type.rb | 1 - app/models/concerns/indexable.rb | 2 +- config/routes.rb | 1 + spec/models/provider_spec.rb | 3 + spec/models/usage_report_spec.rb | 1 - spec/requests/dois_spec.rb | 1 - spec/requests/events_spec.rb | 1 - spec/requests/providers_spec.rb | 20 +++ 10 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 app/controllers/organizations_controller.rb diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb new file mode 100644 index 000000000..a4115b385 --- /dev/null +++ b/app/controllers/organizations_controller.rb @@ -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 diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 88d1bf3d7..12dc9388d 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -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], diff --git a/app/graphql/types/prefix_connection_with_meta_type.rb b/app/graphql/types/prefix_connection_with_meta_type.rb index e0eed75d4..3303ba670 100644 --- a/app/graphql/types/prefix_connection_with_meta_type.rb +++ b/app/graphql/types/prefix_connection_with_meta_type.rb @@ -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, diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index fe362d395..abf95e4ec 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -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? diff --git a/config/routes.rb b/config/routes.rb index b80c845d4..c9285b4b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/models/provider_spec.rb b/spec/models/provider_spec.rb index 50c40f99f..e83352d18 100644 --- a/spec/models/provider_spec.rb +++ b/spec/models/provider_spec.rb @@ -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 diff --git a/spec/models/usage_report_spec.rb b/spec/models/usage_report_spec.rb index f3142bb82..bf1dba902 100644 --- a/spec/models/usage_report_spec.rb +++ b/spec/models/usage_report_spec.rb @@ -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 diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index 9305a971b..80e8e550d 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -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") diff --git a/spec/requests/events_spec.rb b/spec/requests/events_spec.rb index def59c2d6..66be97813 100644 --- a/spec/requests/events_spec.rb +++ b/spec/requests/events_spec.rb @@ -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 diff --git a/spec/requests/providers_spec.rb b/spec/requests/providers_spec.rb index 0944842fd..91cb8fa8f 100644 --- a/spec/requests/providers_spec.rb +++ b/spec/requests/providers_spec.rb @@ -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("doe@joe.joe") + 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