Merge pull request #2474 from techAPJ/add-topic-id-to-bulk-invite

FEATURE: Add topic invitation support to Bulk Invite
This commit is contained in:
Robin Ward 2014-06-26 13:30:30 -04:00
commit 6907739a0b
4 changed files with 78 additions and 45 deletions

View File

@ -49,52 +49,59 @@ module Jobs
def read_csv_file(csv_path)
CSV.foreach(csv_path) do |csv_info|
if !csv_info[0].nil?
if validate_email(csv_info[0])
# email is valid, now check for groups
if !csv_info[1].nil?
# group(s) present
send_invite_with_groups(csv_info[0], csv_info[1], $INPUT_LINE_NUMBER)
else
# no group present
send_invite_without_group(csv_info[0])
end
if csv_info[0]
if (EmailValidator.email_regex =~ csv_info[0])
# email is valid
send_invite(csv_info, $INPUT_LINE_NUMBER)
@sent += 1
else
# invalid email
log "Invalid email '#{csv_info[0]}' at line number '#{$INPUT_LINE_NUMBER}'"
log "Invalid Email '#{csv_info[0]}' at line number '#{$INPUT_LINE_NUMBER}'"
@failed += 1
end
end
end
end
def validate_email(email)
/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/.match(email)
end
def send_invite_with_groups(email, group_names, csv_line_number)
def get_group_ids(group_names, csv_line_number)
group_ids = []
group_names = group_names.split(';')
group_names.each { |group_name|
group_detail = Group.find_by_name(group_name)
if !group_detail.nil?
# valid group
group_ids.push(group_detail.id)
else
# invalid group
log "Invalid group '#{group_name}' at line number '#{csv_line_number}'"
end
}
Invite.invite_by_email(email, @current_user, topic=nil, group_ids)
if group_names
group_names = group_names.split(';')
group_names.each { |group_name|
group_detail = Group.find_by_name(group_name)
if group_detail
# valid group
group_ids.push(group_detail.id)
else
# invalid group
log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'"
@failed += 1
end
}
end
return group_ids
end
def send_invite_without_group(email)
Invite.invite_by_email(email, @current_user, topic=nil)
def get_topic(topic_id, csv_line_number)
topic = nil
if topic_id
topic = Topic.find_by_id(topic_id)
if topic.nil?
log "Invalid Topic ID '#{topic_id}' at line number '#{csv_line_number}'"
@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)
topic = get_topic(csv_info[2], csv_line_number)
Invite.invite_by_email(email, @current_user, topic, group_ids)
end
def log(message)
puts(message) rescue nil
save_log(message)
end

View File

@ -1291,7 +1291,7 @@ en:
bulk_invite_failed:
subject_template: "Bulk Invite processed with some errors!"
text_body_template: |
The bulk invite has been processed, %{sent} invites sent and %{failed} invites failed.
The bulk invite has been processed, %{sent} invites sent with %{failed} error(s).
Here's the log:

View File

@ -1 +1 @@
jeff@gmail.com sam@yahoo.com robin@outlook.com neil@aol.com regis@live.com
jeff@gmail.com,discourse;ubuntu,100 sam@yahoo.com,ubuntu, robin@outlook.com,discourse,200 neil@aol.com,random, regis@live.com,,8888
1 jeff jeff@gmail.com gmail.com sam discourse;ubuntu yahoo.com robin 100 sam@yahoo.com outlook.com neil ubuntu aol.com regis robin@outlook.com live.com discourse 200 neil@aol.com random regis@live.com 8888

View File

@ -24,35 +24,61 @@ describe Jobs::BulkInvite do
it 'reads csv file' do
bulk_invite.current_user = user
bulk_invite.read_csv_file(csv_file)
Invite.where(email: 'robin@outlook.com').exists?.should be_true
Invite.where(email: "robin@outlook.com").exists?.should be_true
end
end
context '.send_invite_with_groups' do
context '.send_invite' do
let(:bulk_invite) { Jobs::BulkInvite.new }
let(:user) { Fabricate(:user) }
let(:group) { Fabricate(:group) }
let(:topic) { Fabricate(:topic) }
let(:email) { "evil@trout.com" }
let(:csv_info) { [] }
it 'creates an invite' do
csv_info[0] = email
it 'creates an invite to the group' do
bulk_invite.current_user = user
bulk_invite.send_invite_with_groups(email, group.name, 1)
bulk_invite.send_invite(csv_info, 1)
Invite.where(email: email).exists?.should be_true
end
it 'creates an invite with group' do
csv_info[0] = email
csv_info[1] = group.name
bulk_invite.current_user = user
bulk_invite.send_invite(csv_info, 1)
invite = Invite.where(email: email).first
invite.should be_present
InvitedGroup.where(invite_id: invite.id, group_id: group.id).exists?.should be_true
end
end
context '.send_invite_without_group' do
let(:bulk_invite) { Jobs::BulkInvite.new }
let(:user) { Fabricate(:user) }
let(:email) { "evil@trout.com" }
it 'creates an invite with topic' do
csv_info[0] = email
csv_info[2] = topic
it 'creates an invite' do
bulk_invite.current_user = user
bulk_invite.send_invite_without_group(email)
Invite.where(email: email).exists?.should be_true
bulk_invite.send_invite(csv_info, 1)
invite = Invite.where(email: email).first
invite.should be_present
TopicInvite.where(invite_id: invite.id, topic_id: topic.id).exists?.should be_true
end
it 'creates an invite with group and topic' do
csv_info[0] = email
csv_info[1] = group.name
csv_info[2] = topic
bulk_invite.current_user = user
bulk_invite.send_invite(csv_info, 1)
invite = Invite.where(email: email).first
invite.should be_present
InvitedGroup.where(invite_id: invite.id, group_id: group.id).exists?.should be_true
TopicInvite.where(invite_id: invite.id, topic_id: topic.id).exists?.should be_true
end
end
end