diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb index 29f94a439da..c4422d0d37c 100644 --- a/app/jobs/regular/bulk_invite.rb +++ b/app/jobs/regular/bulk_invite.rb @@ -10,8 +10,10 @@ module Jobs @logs = [] @sent = 0 @skipped = 0 + @skipped_emails = [] @warnings = 0 @failed = 0 + @failed_emails = [] @groups = {} @user_fields = {} @valid_groups = {} @@ -48,21 +50,25 @@ module Jobs @sent += 1 elsif User === result @skipped += 1 + @skipped_emails << invite[:email] else @failed += 1 + @failed_emails << invite[:email] end else # invalid email save_log "Invalid Email '#{invite[:email]}" @failed += 1 + @failed_emails << invite[:email] end end rescue Exception => e save_log "Bulk Invite Process Failed -- '#{e.message}'" @failed += 1 + @failed_emails << invite[:email] end - def get_groups(group_names) + def get_groups(group_names, email) groups = [] if group_names @@ -76,7 +82,7 @@ module Jobs groups.push(group) else # invalid group - save_log "Invalid Group '#{group_name}'" + save_log "Invalid Group '#{group_name}' for '#{email}'" @warnings += 1 end end @@ -85,13 +91,13 @@ module Jobs groups end - def get_topic(topic_id) + def get_topic(topic_id, email) topic = nil if topic_id topic = Topic.find_by_id(topic_id) if topic.nil? - save_log "Invalid Topic ID '#{topic_id}'" + save_log "Invalid Topic ID '#{topic_id}' for '#{email}'" @warnings += 1 end end @@ -99,7 +105,7 @@ module Jobs topic end - def get_user_fields(fields) + def get_user_fields(fields, email) user_fields = {} fields.each do |key, value| @@ -108,7 +114,7 @@ module Jobs .where("name ILIKE ?", key) .first || :nil if @user_fields[key] == :nil - save_log "Invalid User Field '#{key}'" + save_log "Invalid User Field '#{key}' for '#{email}'" @warnings += 1 next end @@ -127,10 +133,10 @@ module Jobs def send_invite(invite) email = invite[:email] - groups = get_groups(invite[:groups]) - topic = get_topic(invite[:topic_id]) + groups = get_groups(invite[:groups], email) + topic = get_topic(invite[:topic_id], email) locale = invite[:locale] - user_fields = get_user_fields(invite.except(:email, :groups, :topic_id, :locale)) + user_fields = get_user_fields(invite.except(:email, :groups, :topic_id, :locale), email) begin if user = Invite.find_user_by_email(email) @@ -202,6 +208,7 @@ module Jobs :bulk_invite_succeeded, sent: @sent, skipped: @skipped, + skipped_emails: @skipped_emails.join("\n"), warnings: @warnings, logs: @logs.join("\n"), ) @@ -211,8 +218,10 @@ module Jobs :bulk_invite_failed, sent: @sent, skipped: @skipped, + skipped_emails: @skipped_emails.join("\n"), warnings: @warnings, failed: @failed, + failed_emails: @failed_emails.join("\n"), logs: @logs.join("\n"), ) end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e7ef4712482..dbbc913454f 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -3474,6 +3474,12 @@ en: text_body_template: | Your bulk user invite file was processed, %{sent} invites mailed, %{skipped} skipped and %{warnings} warning(s). + Skipped Invites for Emails: + + ``` text + %{skipped_emails} + ``` + ``` text %{logs} ``` @@ -3484,6 +3490,18 @@ en: text_body_template: | Your bulk user invite file was processed, %{sent} invites mailed, %{skipped} skipped, %{warnings} warning(s) and %{failed} error(s). + Skipped Invites for Emails: + + ``` text + %{skipped_emails} + ``` + + Failed Invites for Emails: + + ``` text + %{failed_emails} + ``` + Here's the log: ``` text diff --git a/spec/jobs/bulk_invite_spec.rb b/spec/jobs/bulk_invite_spec.rb index 3dbf0a46baf..ceca0ab0d49 100644 --- a/spec/jobs/bulk_invite_spec.rb +++ b/spec/jobs/bulk_invite_spec.rb @@ -20,6 +20,13 @@ RSpec.describe Jobs::BulkInvite do ] end + def parse_skipped_and_failed_emails(input) + skipped_invites_emails = input[/Skipped Invites for Emails?:\s+``` text\n(.+?)\n```/m, 1] + failed_invites_emails = input[/Failed Invites for Emails?:\s+``` text\n(.+?)\n```/m, 1] + + { skipped_invites: skipped_invites_emails, failed_invites: failed_invites_emails } + end + it "raises an error when the invites array is missing" do expect { Jobs::BulkInvite.new.execute(current_user_id: user.id) }.to raise_error( Discourse::InvalidParameters, @@ -127,6 +134,18 @@ RSpec.describe Jobs::BulkInvite do expect(new_staged_user.user_fields[user_field.id.to_s]).to eq("value 3") end + it "includes any skipped and failed emails in the private message" do + described_class.new.execute( + current_user_id: admin.id, + invites: [{ email: "bad_email" }, { email: user.email }, { email: "test@discourse.org" }], + ) + + post = Post.last + result = parse_skipped_and_failed_emails(post.raw) + expect(result[:skipped_invites]).to eq(user.email) + expect(result[:failed_invites]).to eq("bad_email") + end + context "when there are more than 200 invites" do let(:bulk_invites) { [] }