Skip to content

Commit

Permalink
fetch member id for organizations. #635
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Sep 18, 2020
1 parent 2b4a83c commit 4dac152
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/graphql/resolvers/total_count_from_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def resolve

response.body.dig("data", "message", "total-results")
end
end
end
9 changes: 7 additions & 2 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6175,9 +6175,14 @@ type Organization implements ActorItem {
memberId: ID

"""
Membership type if a DataCite member
Membership role id if a DataCite member
"""
memberRole: MemberRole
memberRoleId: String

"""
Membership role name if a DataCite member
"""
memberRoleName: String

"""
The name of the actor.
Expand Down
33 changes: 31 additions & 2 deletions app/graphql/types/organization_type.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# frozen_string_literal: true

class OrganizationType < BaseObject
MEMBER_ROLES = {
"ROLE_CONSORTIUM" => "Consortium",
"ROLE_CONSORTIUM_ORGANIZATION" => "consortium_organization",
"ROLE_ALLOCATOR" => "direct_member",
"ROLE_FOR_PROFIT_PROVIDER" => "for-profit_provider",
"ROLE_MEMBER" => "member_only" }

implements ActorItem

description "Information about organizations"

field :identifiers, [IdentifierType], null: true, description: "The identifier(s) for the organization."
field :member_id, ID, null: true, description: "Unique member identifier if a DataCite member"
field :member_role, MemberRoleType, null: true, description: "Membership type if a DataCite member"
field :member_role_id, String, null: true, description: "Membership role id if a DataCite member"
field :member_role_name, String, null: true, description: "Membership role name if a DataCite member"
field :url, [Url], null: true, hash_key: "links", description: "URL of the organization."
field :wikipedia_url, Url, null: true, hash_key: "wikipedia_url", description: "Wikipedia URL of the organization."
field :twitter, String, null: true, description: "Twitter username of the organization."
Expand Down Expand Up @@ -140,6 +148,27 @@ def alternate_name
object.aliases + object.acronyms
end

def member
m = Provider.unscoped.where("allocator.role_name IN ('ROLE_FOR_PROFIT_PROVIDER', 'ROLE_CONSORTIUM' , 'ROLE_CONSORTIUM_ORGANIZATION', 'ROLE_ALLOCATOR', 'ROLE_MEMBER')").where(deleted_at: nil).where(ror_id: object.id).first
return {} unless m.present?

{ "member_id" => m.symbol.downcase,
"member_role_id" => MEMBER_ROLES[m.role_name],
"member_role_name" => MEMBER_ROLES[m.role_name].titleize }
end

def member_id
member["member_id"]
end

def member_role_id
member["member_role_id"]
end

def member_role_name
member["member_role_name"]
end

def geolocation
{ "pointLongitude" => object.dig("geolocation", "longitude"),
"pointLatitude" => object.dig("geolocation", "latitude") }
Expand All @@ -153,7 +182,7 @@ def identifiers
end

def provider_id
object.member_id && %w(direct_member consortium_organization).include?(object.member_role["id"]) ? object.member_id : nil
member["member_id"] && %w(direct_member consortium_organization).include?(member["member_role_id"]) ? member["member_id"] : nil
end

def publications(**args)
Expand Down
21 changes: 0 additions & 21 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def self.find_by_id(id)
wikidata_data = find_by_wikidata_id(wikidata)
data = [data.first.reverse_merge(wikidata_data[:data].first)] if wikidata_data

datacite_data = find_datacite_member(id)
data = [data.first.reverse_merge(datacite_data)] if datacite_data

errors = response.body.fetch("errors", nil)

{ data: data, errors: errors }
Expand All @@ -49,9 +46,6 @@ def self.find_by_grid_id(id)
wikidata_data = find_by_wikidata_id(wikidata)
data = [data.first.reverse_merge(wikidata_data[:data].first)] if wikidata_data

datacite_data = find_datacite_member(data.first["id"])
data = [data.first.reverse_merge(datacite_data)] if datacite_data

errors = response.body.fetch("errors", nil)

{ data: data, errors: errors }
Expand All @@ -73,26 +67,11 @@ def self.find_by_crossref_funder_id(id)
wikidata_data = find_by_wikidata_id(wikidata)
data = [data.first.reverse_merge(wikidata_data[:data].first)] if wikidata_data

datacite_data = find_datacite_member(data.first["id"])
data = [data.first.reverse_merge(datacite_data)] if datacite_data

errors = response.body.fetch("errors", nil)

{ data: data, errors: errors }
end

def self.find_datacite_member(id)
member = Provider.unscoped.where("allocator.role_name IN ('ROLE_FOR_PROFIT_PROVIDER', 'ROLE_CONSORTIUM' , 'ROLE_CONSORTIUM_ORGANIZATION', 'ROLE_ALLOCATOR', 'ROLE_MEMBER')").where(deleted_at: nil).where(ror_id: id).first
return nil unless member.present?

{ "member_id" => member.symbol.downcase,
"member_role" => {
"id" => MEMBER_ROLES[member.role_name],
"name" => MEMBER_ROLES[member.role_name].titleize
}
}
end

def self.query(query, options={})
# rows = options[:limit] || 20
page = options[:offset] || 1
Expand Down
9 changes: 4 additions & 5 deletions spec/graphql/types/organization_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@
organization(id: "https://ror.org/013meh722") {
id
memberId
memberRole {
id
name
}
memberRoleId
memberRoleName
name
alternateName
wikipediaUrl
Expand Down Expand Up @@ -112,7 +110,8 @@

expect(response.dig("data", "organization", "id")).to eq("https://ror.org/013meh722")
expect(response.dig("data", "organization", "memberId")).to eq("lpsw")
expect(response.dig("data", "organization", "memberRole")).to eq("id"=>"direct_member", "name"=>"Direct Member")
expect(response.dig("data", "organization", "memberRoleId")).to eq("direct_member")
expect(response.dig("data", "organization", "memberRoleName")).to eq("Direct Member")
expect(response.dig("data", "organization", "name")).to eq("University of Cambridge")
expect(response.dig("data", "organization", "alternateName")).to eq(["Cambridge University"])
expect(response.dig("data", "organization", "wikipediaUrl")).to eq("http://en.wikipedia.org/wiki/University_of_Cambridge")
Expand Down
2 changes: 0 additions & 2 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@
expect(organization.inception_year).to eq("1209")
expect(organization.geolocation).to eq("latitude"=>52.205277777778, "longitude"=>0.11722222222222)
expect(organization.ringgold).to eq("2152")
expect(organization.member_id).to eq("lpsw")
expect(organization.member_role).to eq("id"=>"consortium_organization", "name"=>"Consortium Organization")
end

it "found funder" do
Expand Down

0 comments on commit 4dac152

Please sign in to comment.