Commit 0f117a67 authored by Vijay Hawoldar's avatar Vijay Hawoldar

Adjusts Member awaiting scope to be distinct

Users can potentially be awaiting members of multiple groups but the
scope need only return distinct results

Changelog: changed
EE: true
parent d4071d5a
......@@ -30,11 +30,13 @@ module EE
includes(:user, source: [:route, :parent])
end
scope :awaiting_or_invited_for_group, -> (group) do
scope :distinct_awaiting_or_invited_for_group, -> (group) do
awaiting
.or(::Member.invite)
.in_hierarchy(group)
.select('DISTINCT ON (members.user_id, members.invite_email) members.*')
.includes(:user)
.order(:user_id, :invite_email)
end
end
......
......@@ -105,7 +105,7 @@ module EE
bad_request! unless group.root?
bad_request! unless can?(current_user, :admin_group_member, group)
members = ::Member.awaiting_or_invited_for_group(group)
members = ::Member.distinct_awaiting_or_invited_for_group(group)
present paginate(members), with: ::API::Entities::PendingMember
end
......
......@@ -246,7 +246,7 @@ RSpec.describe Member, type: :model do
end
end
describe '.awaiting_or_invited_for_group' do
describe '.distinct_awaiting_or_invited_for_group' do
let_it_be(:active_group_member) { create(:group_member, group: group) }
let_it_be(:awaiting_group_member) { create(:group_member, :awaiting, group: group) }
let_it_be(:awaiting_subgroup_member) { create(:group_member, :awaiting, group: sub_group) }
......@@ -254,8 +254,11 @@ RSpec.describe Member, type: :model do
let_it_be(:awaiting_invited_member) { create(:group_member, :awaiting, :invited, group: group) }
let_it_be(:active_invited_member) { create(:group_member, :invited, group: group) }
subject(:results) { described_class.distinct_awaiting_or_invited_for_group(group) }
it 'returns the correct members' do
expect(described_class.awaiting_or_invited_for_group(group)).to match_array [
expect(results.size).to eq 5
expect(results).to match_array [
awaiting_group_member,
awaiting_subgroup_member,
awaiting_project_member,
......@@ -263,5 +266,21 @@ RSpec.describe Member, type: :model do
active_invited_member
]
end
it 'does not return additional results for duplicates' do
create(:group_member, :awaiting, group: sub_group, user: awaiting_group_member.user)
create(:group_member, :invited, group: sub_group, invite_email: awaiting_invited_member.invite_email)
expect(results.size).to eq 5
expect(results.map(&:user_id).compact).to contain_exactly(
awaiting_group_member.user_id,
awaiting_subgroup_member.user_id,
awaiting_project_member.user_id
)
expect(results.map(&:invite_email).compact).to contain_exactly(
awaiting_invited_member.invite_email,
active_invited_member.invite_email
)
end
end
end
......@@ -1148,6 +1148,20 @@ RSpec.describe API::Members do
]
end
it 'returns only one membership per user' do
create(:group_member, :awaiting, group: subgroup, user: pending_group_member.user)
create(:group_member, :awaiting, :invited, group: subgroup, invite_email: pending_invited_member.invite_email)
get api(url, owner)
expect(json_response.map { |m| m['id'] }).to match_array [
pending_group_member.id,
pending_subgroup_member.id,
pending_project_member.id,
pending_invited_member.id
]
end
it 'paginates the response' do
get api(url, owner)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment