From 1f06baa1bcbb78cb544713fc342f0c55bfe0bb47 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 29 Jan 2021 08:17:49 +0100 Subject: [PATCH] support filtering contacts by role_name. #697 --- app/controllers/contacts_controller.rb | 1 + app/models/concerns/indexable.rb | 3 +++ spec/requests/contacts_spec.rb | 34 ++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index 9f8f1be3e..a1e54fa83 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -31,6 +31,7 @@ def index else Contact.query( params[:query], + role_name: params[:role_name], provider_id: params[:provider_id], page: page, sort: sort, diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 148a6fada..bf097d703 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -636,6 +636,9 @@ def query(query, options = {}) if options[:provider_id].present? filter << { term: { provider_id: options[:provider_id] } } end + if options[:role_name].present? + filter << { term: { role_name: options[:role_name] } } + end end # ES query can be optionally defined in different ways diff --git a/spec/requests/contacts_spec.rb b/spec/requests/contacts_spec.rb index ea5d7d7fd..ed40324b0 100644 --- a/spec/requests/contacts_spec.rb +++ b/spec/requests/contacts_spec.rb @@ -5,7 +5,7 @@ describe ContactsController, type: :request, elasticsearch: true do let(:bearer) { User.generate_token } let(:provider) { create(:provider) } - let!(:contact) { create(:contact, provider: provider) } + let!(:contact) { create(:contact, provider: provider, role_name: ["billing"]) } let(:params) do { "data" => { @@ -49,12 +49,36 @@ end describe "GET /contacts query" do - before { get "/contacts?query=carberry", nil, headers } + let!(:contacts) { create_list(:contact, 3) } + + before do + Contact.import + sleep 1 + end it "returns contacts" do + get "/contacts?query=carberry", nil, headers + expect(last_response.status).to eq(200) - expect(json).not_to be_empty - expect(json["data"].size).to eq(0) + expect(json["data"].size).to eq(4) + expect(json.dig("meta", "total")).to eq(4) + end + end + + describe "GET /contacts query role_name" do + let!(:contacts) { create_list(:contact, 3) } + + before do + Contact.import + sleep 1 + end + + it "returns contacts" do + get "/contacts?role-name=billing", nil, headers + + expect(last_response.status).to eq(200) + expect(json["data"].size).to eq(1) + expect(json.dig("meta", "total")).to eq(1) end end @@ -106,7 +130,7 @@ attributes = json.dig("data", 0, "attributes") expect(attributes["name"]).to eq("Josiah Carberry") expect(attributes["email"]).to eq("josiah@example.org") - expect(attributes["roleName"]).to eq(["voting"]) + expect(attributes["roleName"]).to eq(["billing"]) relationships = json.dig("data", 0, "relationships") expect(relationships.dig("provider", "data", "id")).to eq(