FIX: Bulk invite should skip invite and add existing users to groups.

This commit is contained in:
Guo Xiang Tan 2019-04-15 21:26:03 +08:00 committed by Guo Xiang Tan
parent 6e6cdfaf80
commit 25514550f0
2 changed files with 41 additions and 6 deletions

View File

@ -55,8 +55,8 @@ module Jobs
file&.close
end
def get_group_ids(group_names, csv_line_number)
group_ids = []
def get_groups(group_names, csv_line_number)
groups = []
if group_names
group_names = group_names.split(';')
@ -67,7 +67,7 @@ module Jobs
if group_detail && guardian.can_edit_group?(group_detail)
# valid group
group_ids.push(group_detail.id)
groups.push(group_detail)
else
# invalid group
save_log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'"
@ -75,11 +75,13 @@ module Jobs
end
}
end
return group_ids
groups
end
def get_topic(topic_id, csv_line_number)
topic = nil
if topic_id
topic = Topic.find_by_id(topic_id)
if topic.nil?
@ -87,15 +89,31 @@ module Jobs
@failed += 1
end
end
return topic
end
def send_invite(csv_info, csv_line_number)
email = csv_info[0]
group_ids = get_group_ids(csv_info[1], csv_line_number)
groups = get_groups(csv_info[1], csv_line_number)
topic = get_topic(csv_info[2], csv_line_number)
begin
Invite.invite_by_email(email, @current_user, topic, group_ids)
if user = User.find_by_email(email)
if groups.present?
Group.transaction do
groups.each do |group|
group.add(user)
GroupActionLogger
.new(@current_user, group)
.log_add_user_to_group(user)
end
end
end
else
Invite.invite_by_email(email, @current_user, topic, groups.map(&:id))
end
rescue => e
save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}"
@sent -= 1

View File

@ -3,6 +3,7 @@ require 'rails_helper'
describe Jobs::BulkInvite do
describe '#execute' do
let(:user) { Fabricate(:user) }
let(:admin) { Fabricate(:admin) }
let!(:group1) { Fabricate(:group, name: 'group1') }
let!(:group2) { Fabricate(:group, name: 'group2') }
let!(:topic) { Fabricate(:topic, id: 999) }
@ -83,6 +84,22 @@ describe Jobs::BulkInvite do
group1.id
)
end
it 'adds existing users to valid groups' do
existing_user = Fabricate(:user, email: "test@discourse.org")
group2.update!(automatic: true)
expect do
described_class.new.execute(
current_user_id: admin.id,
filename: basename
)
end.to change { Invite.count }.by(1)
expect(Invite.exists?(email: "test2@discourse.org")).to eq(true)
expect(existing_user.reload.groups).to eq([group1])
end
end
end