From dbb329898ebe0eaad8b0eff20229ccf1de6ee4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Delmaire?= Date: Wed, 23 Aug 2023 14:48:19 +0200 Subject: [PATCH] FilterService: when integer passed for global search, search only in model id --- backend/app/services/filter_service.rb | 5 ++++- backend/spec/services/filter_service_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/app/services/filter_service.rb b/backend/app/services/filter_service.rb index 73b1bde9a..c1c27c9a4 100644 --- a/backend/app/services/filter_service.rb +++ b/backend/app/services/filter_service.rb @@ -68,7 +68,10 @@ def global_search_query(value) def call global_search = @filters.find { |filter_item| filter_item["key"] == "global_search" } - if global_search + + if global_search && global_search["value"].to_i.to_s == global_search["value"] + return @items.where(id: global_search["value"]) + elsif global_search @items = @items.where(global_search_query(global_search["value"])) @filters.reject! { |filter_item| filter_item["key"] == "global_search" } end diff --git a/backend/spec/services/filter_service_spec.rb b/backend/spec/services/filter_service_spec.rb index 3de8ebcd1..5013c6907 100644 --- a/backend/spec/services/filter_service_spec.rb +++ b/backend/spec/services/filter_service_spec.rb @@ -35,6 +35,14 @@ it "returns items matching the global search" do expect(subject.map(&:id)).to match_array([@enrollment_franceconnect["id"], @enrollment_franceconnect2["id"]]) end + + context "when value is an exact enrollment id" do + let(:filters) { [{"key" => "global_search", "value" => @enrollment_franceconnect.id.to_s}].to_json } + + it "returns only this item" do + expect(subject.map(&:id)).to eq([@enrollment_franceconnect["id"]]) + end + end end context "when global_search is used with fuzzy matching" do