DEV: Update bulk-invite logs and PM template (#27057)

# Preview

<img width="754" alt="Screenshot 2024-05-17 at 8 50 03 AM" src="https://github.com/discourse/discourse/assets/50783505/6710234f-0195-42be-b70e-9d57ba48bb4a">


# New Logs

```
[2024-05-17 08:49:54 -0600] Invalid User Field 'backend name' for 'foobarbing@gmail.com'
[2024-05-17 08:49:54 -0600] Invalid Email 'test
[2024-05-17 08:49:54 -0600] Invalid Email 'this@$@**.com
```
This commit is contained in:
Isaac Janzen 2024-05-17 12:21:21 -06:00 committed by GitHub
parent 6060e4618c
commit ede0fa5802
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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) { [] }