Commit b933eef0 authored by Doug Stull's avatar Doug Stull Committed by Vitali Tatarintev

Allow members added by user record to be found if existing

- allows existing members to see users by user record
parent 3f0faea1
...@@ -47,16 +47,15 @@ module Members ...@@ -47,16 +47,15 @@ module Members
end end
end end
if user_ids.present?
# we should handle the idea of existing members where users are passed as users - https://gitlab.com/gitlab-org/gitlab/-/issues/352617
# the below will automatically discard invalid user_ids # the below will automatically discard invalid user_ids
users.concat(User.id_in(user_ids)) users.concat(User.id_in(user_ids)) if user_ids.present?
users.uniq! # de-duplicate just in case as there is no controlling if user records and ids are sent multiple times
if users.present?
# helps not have to perform another query per user id to see if the member exists later on when fetching # helps not have to perform another query per user id to see if the member exists later on when fetching
existing_members = source.members_and_requesters.where(user_id: user_ids).index_by(&:user_id) # rubocop:disable CodeReuse/ActiveRecord existing_members = source.members_and_requesters.where(user_id: users).index_by(&:user_id) # rubocop:disable CodeReuse/ActiveRecord
end end
users.uniq! # de-duplicate just in case as there is no controlling if user records and ids are sent multiple times
[emails, users, existing_members] [emails, users, existing_members]
end end
end end
......
...@@ -371,8 +371,7 @@ RSpec.shared_examples_for "bulk member creation" do ...@@ -371,8 +371,7 @@ RSpec.shared_examples_for "bulk member creation" do
it 'returns a Member objects' do it 'returns a Member objects' do
members = described_class.add_users(source, [user1, user2], :maintainer) members = described_class.add_users(source, [user1, user2], :maintainer)
expect(members).to be_a Array expect(members.map(&:user)).to contain_exactly(user1, user2)
expect(members.size).to eq(2)
expect(members).to all(be_a(member_type)) expect(members).to all(be_a(member_type))
expect(members).to all(be_persisted) expect(members).to all(be_persisted)
end end
...@@ -394,20 +393,18 @@ RSpec.shared_examples_for "bulk member creation" do ...@@ -394,20 +393,18 @@ RSpec.shared_examples_for "bulk member creation" do
end end
context 'with de-duplication' do context 'with de-duplication' do
it 'with the same user by id and user' do it 'has the same user by id and user' do
members = described_class.add_users(source, [user1.id, user1, user1.id, user2, user2.id, user2], :maintainer) members = described_class.add_users(source, [user1.id, user1, user1.id, user2, user2.id, user2], :maintainer)
expect(members).to be_a Array expect(members.map(&:user)).to contain_exactly(user1, user2)
expect(members.size).to eq(2)
expect(members).to all(be_a(member_type)) expect(members).to all(be_a(member_type))
expect(members).to all(be_persisted) expect(members).to all(be_persisted)
end end
it 'with the same user sent more than once' do it 'has the same user sent more than once' do
members = described_class.add_users(source, [user1, user1], :maintainer) members = described_class.add_users(source, [user1, user1], :maintainer)
expect(members).to be_a Array expect(members.map(&:user)).to contain_exactly(user1)
expect(members.size).to eq(1)
expect(members).to all(be_a(member_type)) expect(members).to all(be_a(member_type))
expect(members).to all(be_persisted) expect(members).to all(be_persisted)
end end
...@@ -418,15 +415,35 @@ RSpec.shared_examples_for "bulk member creation" do ...@@ -418,15 +415,35 @@ RSpec.shared_examples_for "bulk member creation" do
source.add_user(user1, :developer) source.add_user(user1, :developer)
end end
it 'supports existing users as expected' do it 'has the same user sent more than once with the member already existing' do
expect do
members = described_class.add_users(source, [user1, user1, user2], :maintainer)
expect(members.map(&:user)).to contain_exactly(user1, user2)
expect(members).to all(be_a(member_type))
expect(members).to all(be_persisted)
end.to change { Member.count }.by(1)
end
it 'supports existing users as expected with user_ids passed' do
user3 = create(:user) user3 = create(:user)
expect do
members = described_class.add_users(source, [user1.id, user2, user3.id], :maintainer) members = described_class.add_users(source, [user1.id, user2, user3.id], :maintainer)
expect(members.map(&:user)).to contain_exactly(user1, user2, user3)
expect(members).to all(be_a(member_type))
expect(members).to all(be_persisted)
end.to change { Member.count }.by(2)
end
expect(members).to be_a Array it 'supports existing users as expected without user ids passed' do
expect(members.size).to eq(3) user3 = create(:user)
expect do
members = described_class.add_users(source, [user1, user2, user3], :maintainer)
expect(members.map(&:user)).to contain_exactly(user1, user2, user3)
expect(members).to all(be_a(member_type)) expect(members).to all(be_a(member_type))
expect(members).to all(be_persisted) expect(members).to all(be_persisted)
end.to change { Member.count }.by(2)
end end
end end
......
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