From afd1bff021fcb4feff0178ec07c0d2fc18239131 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 12 May 2019 00:03:18 +0200 Subject: [PATCH] filter facets for state and years. #269 --- ...prefix_connection_with_total_count_type.rb | 48 +++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/app/graphql/types/prefix_connection_with_total_count_type.rb b/app/graphql/types/prefix_connection_with_total_count_type.rb index 539091fe2..a044238c0 100644 --- a/app/graphql/types/prefix_connection_with_total_count_type.rb +++ b/app/graphql/types/prefix_connection_with_total_count_type.rb @@ -4,21 +4,61 @@ class PrefixConnectionWithTotalCountType < GraphQL::Types::Relay::BaseConnection edge_type(PrefixEdgeType) field :total_count, Integer, null: false + field :states, [FacetType], null: false field :years, [FacetType], null: false def total_count object.nodes.size end + def states + args = self.object.arguments + + if object.parent.class.name == "Provider" + collection = object.parent.provider_prefixes.joins(:prefix) + + if args[:state].present? + [{ id: args[:state], + title: args[:state].underscore.humanize, + count: collection.count }] + else + [{ id: "withoutClient", + title: "Without client", + count: collection.state("without-client").count }, + { id: "withClient", + title: "With client", + count: collection.state("with-client").count }] + end + else + [] + end + end + def years + args = self.object.arguments + 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 } } + + if args[:year].present? + [{ id: args[:year], + title: args[:year], + count: collection.where('YEAR(allocator_prefixes.created_at) = ?', args[:year]).count }] + else + 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 } } + end 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 } } + + if args[:year].present? + [{ id: args[:year], + title: args[:year], + count: collection.where('YEAR(datacentre_prefixes.created_at) = ?', args[:year]).count }] + else + 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 } } + end else {} end