Skip to content

A platform for community discussion. Free, open, simple.

FIX: Count resulting bulk invites correctly (#18461)

Bianca Nenciucf646b2061fb
diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb
index 08fc977f459..ed83eb381df 100644
--- a/app/jobs/regular/bulk_invite.rb
+++ b/app/jobs/regular/bulk_invite.rb
@@ -9,6 +9,8 @@ module Jobs
@logs = []
@sent = 0
+ @skipped = 0
+ @warnings = 0
@failed = 0
@groups = {}
@user_fields = {}
@@ -39,8 +41,14 @@ module Jobs
invites.each do |invite|
if EmailAddressValidator.valid_value?(invite[:email])
# email is valid
- send_invite(invite)
- @sent += 1
+ result = send_invite(invite)
+ if Invite === result
+ @sent += 1
+ elsif User === result
+ @skipped += 1
+ else
+ @failed += 1
+ end
else
# invalid email
save_log "Invalid Email '#{invite[:email]}"
@@ -58,7 +66,7 @@ module Jobs
if group_names
group_names = group_names.split(';')
- group_names.each { |group_name|
+ group_names.each do |group_name|
group = fetch_group(group_name)
if group && can_edit_group?(group)
@@ -67,9 +75,9 @@ module Jobs
else
# invalid group
save_log "Invalid Group '#{group_name}'"
- @failed += 1
+ @warnings += 1
end
- }
+ end
end
groups
@@ -82,7 +90,7 @@ module Jobs
topic = Topic.find_by_id(topic_id)
if topic.nil?
save_log "Invalid Topic ID '#{topic_id}'"
- @failed += 1
+ @warnings += 1
end
end
@@ -94,7 +102,11 @@ module Jobs
fields.each do |key, value|
@user_fields[key] ||= UserField.includes(:user_field_options).where('name ILIKE ?', key).first || :nil
- next if @user_fields[key] == :nil
+ if @user_fields[key] == :nil
+ save_log "Invalid User Field '#{key}'"
+ @warnings += 1
+ next
+ end
# Automatically correct user field value
if @user_fields[key].field_type == "dropdown"
@@ -139,6 +151,8 @@ module Jobs
user.locale = locale
user.save!
end
+
+ user
else
if user_fields.present? || locale.present?
user = User.where(staged: true).find_by_email(email)
@@ -171,8 +185,8 @@ module Jobs
end
rescue => e
save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}"
- @sent -= 1
- @failed += 1
+
+ nil
end
end
@@ -182,17 +196,22 @@ module Jobs
def notify_user
if @current_user
- if (@sent > 0 && @failed == 0)
+ if @sent > 0 && @failed == 0
SystemMessage.create_from_system_user(
@current_user,
:bulk_invite_succeeded,
- sent: @sent
+ sent: @sent,
+ skipped: @skipped,
+ warnings: @warnings,
+ logs: @logs.join("\n")
)
else
SystemMessage.create_from_system_user(
@current_user,
:bulk_invite_failed,
sent: @sent,
+ skipped: @skipped,
+ warnings: @warnings,
failed: @failed,
logs: @logs.join("\n")
)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 1d08c5e04a1..02a5f4e6935 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3091,13 +3091,18 @@ en:
bulk_invite_succeeded:
title: "Bulk Invite Succeeded"
subject_template: "Bulk user invite processed successfully"
- text_body_template: "Your bulk user invite file was processed, %{sent} invites mailed."
+ text_body_template: |
+ Your bulk user invite file was processed, %{sent} invites mailed, %{skipped} skipped and %{warnings} warning(s).
+
+ ``` text
+ %{logs}
+ ```
bulk_invite_failed:
title: "Bulk Invite Failed"
subject_template: "Bulk user invite processed with errors"
text_body_template: |
- Your bulk user invite file was processed, %{sent} invites mailed with %{failed} error(s).
+ Your bulk user invite file was processed, %{sent} invites mailed, %{skipped} skipped, %{warnings} warning(s) and %{failed} error(s).
Here's the log:
diff --git a/spec/jobs/bulk_invite_spec.rb b/spec/jobs/bulk_invite_spec.rb
index 9d0984551d9..53d38d78885 100644
--- a/spec/jobs/bulk_invite_spec.rb
+++ b/spec/jobs/bulk_invite_spec.rb
@@ -9,7 +9,7 @@ RSpec.describe Jobs::BulkInvite do
fab!(:topic) { Fabricate(:topic) }
let(:staged_user) { Fabricate(:user, staged: true, active: false) }
let(:email) { '[email protected]' }
- let(:invites) { [{ email: staged_user.email }, { email: '[email protected]' }, { email: '[email protected]', groups: 'GROUP1;group2', topic_id: topic.id }] }
+ let(:invites) { [{ email: user.email }, { email: staged_user.email }, { email: '[email protected]' }, { email: '[email protected]', groups: 'GROUP1;group2', topic_id: topic.id }, { email: 'invalid' }] }
it 'raises an error when the invites array is missing' do
expect { Jobs::BulkInvite.new.execute(current_user_id: user.id) }
@@ -34,6 +34,12 @@ RSpec.describe Jobs::BulkInvite do
expect(invite.email).to eq(email)
expect(invite.invited_groups.pluck(:group_id)).to contain_exactly(group1.id, group2.id)
expect(invite.topic_invites.pluck(:topic_id)).to contain_exactly(topic.id)
+
+ post = Post.last
+ expect(post.raw).to include("3 invites")
+ expect(post.raw).to include("1 skipped")
+ expect(post.raw).to include("0 warning")
+ expect(post.raw).to include("1 error")
end
it 'does not create invited groups for automatic groups' do
@@ -47,6 +53,9 @@ RSpec.describe Jobs::BulkInvite do
invite = Invite.last
expect(invite.email).to eq(email)
expect(invite.invited_groups.pluck(:group_id)).to contain_exactly(group1.id)
+
+ post = Post.last
+ expect(post.raw).to include("1 warning")
end
it 'does not create invited groups record if the user can not manage the group' do