diff --git a/app/models/doi/graphql_query.rb b/app/models/doi/graphql_query.rb index 1da39ed92..2399d7652 100644 --- a/app/models/doi/graphql_query.rb +++ b/app/models/doi/graphql_query.rb @@ -116,6 +116,10 @@ def must def filters options = @options + + # turn ids into an array if provided as comma-separated string + options[:ids] = options[:ids].split(",") if options[:ids].is_a?(String) + filter = [] filter << { terms: { doi: options[:ids].map(&:upcase) } } if options[:ids].present? filter << { term: { "types.resourceTypeGeneral": options[:resource_type_id].underscore.camelize } } if options[:resource_type_id].present? diff --git a/spec/models/doi/graphql_query_builder_spec.rb b/spec/models/doi/graphql_query_builder_spec.rb index b6d5a125a..c506a9017 100644 --- a/spec/models/doi/graphql_query_builder_spec.rb +++ b/spec/models/doi/graphql_query_builder_spec.rb @@ -57,4 +57,19 @@ expect(described_class.new("foo/bar", {}).clean_query).to eq("foo\\/bar") end end + + describe "filters" do + it "is an empty array if not set" do + expect(described_class.new("", {}).filters).to eq([]) + expect(described_class.new(nil, {}).filters).to eq([]) + end + + it "can filter for ids" do + expect(described_class.new("foo", { ids: ["bar"] }).filters).to eq([{ terms: { doi: ["BAR"] } }]) + end + + it "can filter for ids as single string" do + expect(described_class.new("foo", { ids: "bar" }).filters).to eq([{ terms: { doi: ["BAR"] } }]) + end + end end