FIX: display warning only if all users already added to the group (#10500)

If at least one user can be added to the group, proceed with that action
This commit is contained in:
Krzysztof Kotlarek 2020-08-21 13:38:09 +10:00 committed by GitHub
parent daddad7fd6
commit 7833853cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -329,12 +329,12 @@ class GroupsController < ApplicationController
'usernames or emails must be present'
)
end
if (usernames = group.users.where(id: users.map(&:id)).pluck(:username)).present?
usernames_already_in_group = group.users.where(id: users.map(&:id)).pluck(:username)
if usernames_already_in_group.present? && usernames_already_in_group.length == users.length
render_json_error(I18n.t(
"groups.errors.member_already_exist",
username: usernames.sort.join(", "),
count: usernames.size
username: usernames_already_in_group.sort.join(", "),
count: usernames_already_in_group.size
))
else
uniq_users = users.uniq

View File

@ -1213,11 +1213,25 @@ describe GroupsController do
expect(response.status).to eq(200)
end
it 'fails when multiple member already exists' do
it 'adds missing users even if some exists' do
user2.update!(username: 'alice')
user3 = Fabricate(:user, username: 'bob')
[user2, user3].each { |user| group.add(user) }
expect do
put "/groups/#{group.id}/members.json",
params: { user_emails: [user1.email, user2.email, user3.email].join(",") }
end.to change { group.users.count }.by(1)
expect(response.status).to eq(200)
end
it 'displays warning when all members already exists' do
user1.update!(username: 'john')
user2.update!(username: 'alice')
user3 = Fabricate(:user, username: 'bob')
[user1, user2, user3].each { |user| group.add(user) }
expect do
put "/groups/#{group.id}/members.json",
params: { user_emails: [user1.email, user2.email, user3.email].join(",") }
@ -1227,8 +1241,8 @@ describe GroupsController do
expect(response.parsed_body["errors"]).to include(I18n.t(
"groups.errors.member_already_exist",
username: "alice, bob",
count: 2
username: "alice, bob, john",
count: 3
))
end
end