Skip to content

Commit

Permalink
[fix] Scope tag counts to active contributors (#2070)
Browse files Browse the repository at this point in the history
Fixes #2069
  • Loading branch information
mattwr18 authored Nov 19, 2024
1 parent ba88299 commit f93575d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/organizations/contributors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def destroy
end

def count
render json: { count: @organization.contributors.with_tags(params[:tag_list]).count }
render json: { count: @organization.contributors.active.with_tags(params[:tag_list]).count }
end

def conversations
Expand Down
2 changes: 1 addition & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def contributors_tags_with_count
ActsAsTaggableOn::Tag
.for_tenant(id)
.joins(:taggings)
.where(taggings: { taggable_type: Contributor.name })
.where(taggings: { taggable_type: Contributor.name, taggable_id: Contributor.active })
.select('tags.id, tags.name, count(taggings.id) as taggings_count')
.group('tags.id')
.all
Expand Down
15 changes: 15 additions & 0 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
describe '#contributors_tags_with_count' do
subject { organization.contributors_tags_with_count.pluck(:name, :count) }

it 'makes one database query' do
expect { subject }.to make_database_queries(count: 1)
end

context 'given a contributor with a tag' do
let!(:contributor) { create(:contributor, tag_list: %w[Homeowner], organization: organization) }
it { should eq([['Homeowner', 1]]) }
Expand All @@ -95,6 +99,17 @@
let!(:request) { create(:request, tag_list: %w[Homeowner], organization: organization) }
it { should eq([['Homeowner', 1]]) }
end

context 'given non-active contributors with the same tag' do
before do
create(:contributor, tag_list: %w[Homeowner], deactivated_at: 1.day.ago, organization: organization)
create(:contributor, tag_list: 'teacher', unsubscribed_at: 1.day.ago, organization: organization)
end

it "does not count inactive contributor's tags" do
expect(subject).to eq([['Homeowner', 1]])
end
end
end
end
end
16 changes: 15 additions & 1 deletion spec/requests/contributors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,27 @@
end

describe 'GET /count' do
subject { -> { get count_organization_contributors_path(organization, tag_list: ['teacher'], as: user) } }

let!(:teachers) { create_list(:contributor, 2, tag_list: 'teacher', organization: organization) }
let!(:other_teachers) { create_list(:contributor, 2, tag_list: 'teacher') }

it 'returns count of contributors with a specific tag within the organization' do
get count_organization_contributors_path(organization, tag_list: ['teacher'], as: user)
subject.call
expect(response.body).to eq({ count: 2 }.to_json)
end

context 'given non-active contributors' do
before do
create(:contributor, tag_list: 'teacher', deactivated_at: 1.day.ago, organization: organization)
create(:contributor, tag_list: 'teacher', unsubscribed_at: 1.day.ago, organization: organization)
subject.call
end

it 'returns the count of contributors with a specific tag for active contributors' do
expect(response.body).to eq({ count: 2 }.to_json)
end
end
end

describe 'GET /conversations' do
Expand Down

0 comments on commit f93575d

Please sign in to comment.