Skip to content

Commit

Permalink
support filtering contacts by role_name. #697
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 29, 2021
1 parent 0889833 commit 1f06baa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 29 additions & 5 deletions spec/requests/contacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -106,7 +130,7 @@
attributes = json.dig("data", 0, "attributes")
expect(attributes["name"]).to eq("Josiah Carberry")
expect(attributes["email"]).to eq("[email protected]")
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(
Expand Down

0 comments on commit 1f06baa

Please sign in to comment.