FEATURE: Add topic invitation support to Bulk Invite

This commit is contained in:
Arpit Jalan 2014-06-26 20:46:53 +05:30
parent 00910679ad
commit 52f9984804
3 changed files with 77 additions and 42 deletions

View File

@ -47,54 +47,63 @@ module Jobs
return csv_path
end
def validate_email(email)
/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/.match(email)
end
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
# 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.nil?
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
}
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.nil?
topic = Topic.find_by_id(topic_id)
if topic.nil?
log "Invalid Topic ID '#{topic_id}' at line number '#{csv_line_number}'"
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

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