Refactor code and update specs
This commit is contained in:
parent
c2055732c7
commit
b227f736f1
|
@ -5,6 +5,7 @@ module Jobs
|
||||||
|
|
||||||
class BulkInvite < Jobs::Base
|
class BulkInvite < Jobs::Base
|
||||||
sidekiq_options retry: false
|
sidekiq_options retry: false
|
||||||
|
attr_accessor :current_user
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@logs = []
|
@logs = []
|
||||||
|
@ -16,7 +17,7 @@ module Jobs
|
||||||
filename = args[:filename]
|
filename = args[:filename]
|
||||||
identifier = args[:identifier]
|
identifier = args[:identifier]
|
||||||
chunks = args[:chunks].to_i
|
chunks = args[:chunks].to_i
|
||||||
current_user = User.find_by(id: args[:current_user_id])
|
@current_user = User.find_by(id: args[:current_user_id])
|
||||||
|
|
||||||
raise Discourse::InvalidParameters.new(:filename) if filename.blank?
|
raise Discourse::InvalidParameters.new(:filename) if filename.blank?
|
||||||
raise Discourse::InvalidParameters.new(:identifier) if identifier.blank?
|
raise Discourse::InvalidParameters.new(:identifier) if identifier.blank?
|
||||||
|
@ -26,10 +27,10 @@ module Jobs
|
||||||
csv_path = get_csv_path(filename, identifier, chunks)
|
csv_path = get_csv_path(filename, identifier, chunks)
|
||||||
|
|
||||||
# read csv file, and send out invitations
|
# read csv file, and send out invitations
|
||||||
read_csv_file(csv_path, current_user)
|
read_csv_file(csv_path)
|
||||||
|
|
||||||
# send notification to user regarding progress
|
# send notification to user regarding progress
|
||||||
notify_user(current_user)
|
notify_user
|
||||||
|
|
||||||
# since emails have already been sent out, delete the uploaded csv file
|
# since emails have already been sent out, delete the uploaded csv file
|
||||||
FileUtils.rm_rf(csv_path) rescue nil
|
FileUtils.rm_rf(csv_path) rescue nil
|
||||||
|
@ -46,17 +47,17 @@ module Jobs
|
||||||
return csv_path
|
return csv_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_csv_file(csv_path, current_user)
|
def read_csv_file(csv_path)
|
||||||
CSV.foreach(csv_path) do |csv_info|
|
CSV.foreach(csv_path) do |csv_info|
|
||||||
if !csv_info[0].nil?
|
if !csv_info[0].nil?
|
||||||
if validate_email(csv_info[0])
|
if validate_email(csv_info[0])
|
||||||
# email is valid, now check for groups
|
# email is valid, now check for groups
|
||||||
if !csv_info[1].nil?
|
if !csv_info[1].nil?
|
||||||
# group(s) present
|
# group(s) present
|
||||||
send_invite_with_groups(csv_info[0], csv_info[1], current_user, $INPUT_LINE_NUMBER)
|
send_invite_with_groups(csv_info[0], csv_info[1], $INPUT_LINE_NUMBER)
|
||||||
else
|
else
|
||||||
# no group present
|
# no group present
|
||||||
send_invite_without_group(csv_info[0], current_user)
|
send_invite_without_group(csv_info[0])
|
||||||
end
|
end
|
||||||
@sent += 1
|
@sent += 1
|
||||||
else
|
else
|
||||||
|
@ -72,7 +73,7 @@ module Jobs
|
||||||
/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/.match(email)
|
/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/.match(email)
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_invite_with_groups(email, group_names, current_user, csv_line_number)
|
def send_invite_with_groups(email, group_names, csv_line_number)
|
||||||
group_ids = []
|
group_ids = []
|
||||||
group_names = group_names.split(';')
|
group_names = group_names.split(';')
|
||||||
group_names.each { |group_name|
|
group_names.each { |group_name|
|
||||||
|
@ -85,11 +86,11 @@ module Jobs
|
||||||
log "Invalid group '#{group_name}' at line number '#{csv_line_number}'"
|
log "Invalid group '#{group_name}' at line number '#{csv_line_number}'"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
Invite.invite_by_email(email, current_user, topic=nil, group_ids)
|
Invite.invite_by_email(email, @current_user, topic=nil, group_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_invite_without_group(email, current_user)
|
def send_invite_without_group(email)
|
||||||
Invite.invite_by_email(email, current_user, topic=nil)
|
Invite.invite_by_email(email, @current_user, topic=nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def log(message)
|
def log(message)
|
||||||
|
@ -101,12 +102,12 @@ module Jobs
|
||||||
@logs << "[#{Time.now}] #{message}"
|
@logs << "[#{Time.now}] #{message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_user(current_user)
|
def notify_user
|
||||||
if current_user
|
if @current_user
|
||||||
if (@sent > 0 && @failed == 0)
|
if (@sent > 0 && @failed == 0)
|
||||||
SystemMessage.create(current_user, :bulk_invite_succeeded, sent: @sent)
|
SystemMessage.create(@current_user, :bulk_invite_succeeded, sent: @sent)
|
||||||
else
|
else
|
||||||
SystemMessage.create(current_user, :bulk_invite_failed, sent: @sent, failed: @failed, logs: @logs.join("\n"))
|
SystemMessage.create(@current_user, :bulk_invite_failed, sent: @sent, failed: @failed, logs: @logs.join("\n"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,12 +17,14 @@ describe Jobs::BulkInvite do
|
||||||
end
|
end
|
||||||
|
|
||||||
context '.read_csv_file' do
|
context '.read_csv_file' do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
let(:bulk_invite) { Jobs::BulkInvite.new }
|
let(:bulk_invite) { Jobs::BulkInvite.new }
|
||||||
let(:csv_file) { File.new("#{Rails.root}/spec/fixtures/csv/discourse.csv") }
|
let(:csv_file) { File.new("#{Rails.root}/spec/fixtures/csv/discourse.csv") }
|
||||||
let(:user) { Fabricate(:user) }
|
|
||||||
|
|
||||||
it 'reads csv file' do
|
it 'reads csv file' do
|
||||||
bulk_invite.read_csv_file(csv_file, user)
|
bulk_invite.current_user = user
|
||||||
|
bulk_invite.read_csv_file(csv_file)
|
||||||
|
Invite.where(email: 'robin@outlook.com').exists?.should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -33,7 +35,8 @@ describe Jobs::BulkInvite do
|
||||||
let(:email) { "evil@trout.com" }
|
let(:email) { "evil@trout.com" }
|
||||||
|
|
||||||
it 'creates an invite to the group' do
|
it 'creates an invite to the group' do
|
||||||
bulk_invite.send_invite_with_groups(email, group.name, user, 1)
|
bulk_invite.current_user = user
|
||||||
|
bulk_invite.send_invite_with_groups(email, group.name, 1)
|
||||||
invite = Invite.where(email: email).first
|
invite = Invite.where(email: email).first
|
||||||
invite.should be_present
|
invite.should be_present
|
||||||
InvitedGroup.where(invite_id: invite.id, group_id: group.id).exists?.should be_true
|
InvitedGroup.where(invite_id: invite.id, group_id: group.id).exists?.should be_true
|
||||||
|
@ -46,7 +49,8 @@ describe Jobs::BulkInvite do
|
||||||
let(:email) { "evil@trout.com" }
|
let(:email) { "evil@trout.com" }
|
||||||
|
|
||||||
it 'creates an invite' do
|
it 'creates an invite' do
|
||||||
bulk_invite.send_invite_without_group(email, user)
|
bulk_invite.current_user = user
|
||||||
|
bulk_invite.send_invite_without_group(email)
|
||||||
Invite.where(email: email).exists?.should be_true
|
Invite.where(email: email).exists?.should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue