diff --git a/app/graphql/types/client_type.rb b/app/graphql/types/client_type.rb index b68baeacf..7d99e39dc 100644 --- a/app/graphql/types/client_type.rb +++ b/app/graphql/types/client_type.rb @@ -8,8 +8,9 @@ class ClientType < GraphQL::Schema::Object field :description, String, null: true, description: "Description of the client" field :contact_name, String, null: true, description: "Client contact name" field :contact_email, String, null: true, description: "Client contact email" - field :prefixes, [PrefixType], null: false, description: "Prefixes managed by the client" do + field :prefixes, PrefixConnectionWithTotalCountType, null: false, description: "Prefixes managed by the client", connection: true, max_page_size: 100 do argument :query, String, required: false + argument :year, String, required: false argument :first, Int, required: false, default_value: 25 end @@ -26,7 +27,8 @@ class ClientType < GraphQL::Schema::Object def prefixes(**args) collection = object.prefixes collection = collection.query(args[:query]) if args[:query].present? - collection.page(1).per(args[:first]) + collection = collection.where('YEAR(prefix.created) = ?', args[:year]) if args[:year].present? + collection end def datasets(**args) diff --git a/app/graphql/types/facet_type.rb b/app/graphql/types/facet_type.rb new file mode 100644 index 000000000..757489d9a --- /dev/null +++ b/app/graphql/types/facet_type.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class FacetType < BaseObject + description "Summary information" + + field :id, String, null: true, description: "ID" + field :title, String, null: true, description: "Title" + field :count, Int, null: true, description: "Count" +end diff --git a/app/graphql/types/prefix_connection_with_total_count_type.rb b/app/graphql/types/prefix_connection_with_total_count_type.rb new file mode 100644 index 000000000..539091fe2 --- /dev/null +++ b/app/graphql/types/prefix_connection_with_total_count_type.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class PrefixConnectionWithTotalCountType < GraphQL::Types::Relay::BaseConnection + edge_type(PrefixEdgeType) + + field :total_count, Integer, null: false + field :years, [FacetType], null: false + + def total_count + object.nodes.size + end + + def years + if object.parent.class.name == "Provider" + collection = object.parent.provider_prefixes.joins(:prefix) + years = collection.where.not(prefixes: nil).order("YEAR(allocator_prefixes.created_at) DESC").group("YEAR(allocator_prefixes.created_at)").count + years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } } + elsif object.parent.class.name == "Client" + collection = object.parent.client_prefixes.joins(:prefix) + years = collection.where.not(prefixes: nil).order("YEAR(datacentre_prefixes.created_at) DESC").group("YEAR(datacentre_prefixes.created_at)").count + years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } } + else + {} + end + end +end diff --git a/app/graphql/types/prefix_edge_type.rb b/app/graphql/types/prefix_edge_type.rb new file mode 100644 index 000000000..1f745ee8f --- /dev/null +++ b/app/graphql/types/prefix_edge_type.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class PrefixEdgeType < GraphQL::Types::Relay::BaseEdge + node_type(PrefixType) +end diff --git a/app/graphql/types/provider_type.rb b/app/graphql/types/provider_type.rb index 472acd5aa..37c0aa34a 100644 --- a/app/graphql/types/provider_type.rb +++ b/app/graphql/types/provider_type.rb @@ -16,23 +16,27 @@ class ProviderType < BaseObject field :organization_type, String, null: true, description: "Type of organization" field :focus_area, String, null: true, description: "Field of science covered by member" field :joined, String, null: true, description: "Date provider joined DataCite" - field :prefixes, [PrefixType], null: false do + field :prefixes, PrefixConnectionWithTotalCountType, null: false, description: "Prefixes managed by the provider", connection: true, max_page_size: 100 do argument :query, String, required: false + argument :state, String, required: false + argument :year, String, required: false argument :first, Int, required: false, default_value: 25 end - field :clients, [ClientType], null: false, description: "Clients associated with the provider" do + field :clients, [ClientType], null: false, description: "Clients associated with the provider", max_page_size: 100 do argument :query, String, required: false argument :first, Int, required: false, default_value: 25 end def prefixes(**args) collection = object.prefixes + collection = collection.state(args[:state].underscore.dasherize) if args[:state].present? collection = collection.query(args[:query]) if args[:query].present? - collection.page(1).per(args[:first]) + collection = collection.where('YEAR(prefix.created) = ?', args[:year]) if args[:year].present? + collection end def clients(**args) - Client.query(args[:query], provider_id: object.uid, page: { number: 1, size: args[:first] }).records + Client.query(args[:query], provider_id: object.uid, page: { cursor: 1, size: args[:first] }).records end end diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 0e7e83fec..527e8bd2c 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -112,6 +112,10 @@ def query(query, options={}) from = 0 search_after = [options.dig(:page, :cursor)] sort = [{ created: { order: 'asc' }}] + elsif options.dig(:page, :cursor).present? + from = 0 + search_after = [options.dig(:page, :cursor)] + sort = [{ created: { order: 'asc' }}] else from = (options.dig(:page, :number) - 1) * options.dig(:page, :size) search_after = nil