FIX: Bulk invite should skip invite and add existing users to groups.

FIX: Bulk invite should skip invite and add existing users to groups.

diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb
index f2de4c9..61d2875 100644
--- a/app/jobs/regular/bulk_invite.rb
+++ b/app/jobs/regular/bulk_invite.rb
@@ -55,8 +55,8 @@ module Jobs
       file&.close
     end
 
-    def get_group_ids(group_names, csv_line_number)
-      group_ids = []
+    def get_groups(group_names, csv_line_number)
+      groups = []
 
       if group_names
         group_names = group_names.split(';')
@@ -67,7 +67,7 @@ module Jobs
 
           if group_detail && guardian.can_edit_group?(group_detail)
             # valid group
-            group_ids.push(group_detail.id)
+            groups.push(group_detail)
           else
             # invalid group
             save_log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'"
@@ -75,11 +75,13 @@ module Jobs
           end
         }
       end
-      return group_ids
+
+      groups
     end
 
     def get_topic(topic_id, csv_line_number)
       topic = nil
+
       if topic_id
         topic = Topic.find_by_id(topic_id)
         if topic.nil?
@@ -87,15 +89,31 @@ module Jobs
           @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)
+      groups = get_groups(csv_info[1], csv_line_number)
       topic = get_topic(csv_info[2], csv_line_number)
+
       begin
-        Invite.invite_by_email(email, @current_user, topic, group_ids)
+        if user = User.find_by_email(email)
+          if groups.present?
+            Group.transaction do
+              groups.each do |group|
+                group.add(user)
+
+                GroupActionLogger
+                  .new(@current_user, group)
+                  .log_add_user_to_group(user)
+              end
+            end
+          end
+        else
+          Invite.invite_by_email(email, @current_user, topic, groups.map(&:id))
+        end
       rescue => e
         save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}"
         @sent -= 1
diff --git a/spec/jobs/bulk_invite_spec.rb b/spec/jobs/bulk_invite_spec.rb
index ef08836..046cad0 100644
--- a/spec/jobs/bulk_invite_spec.rb
+++ b/spec/jobs/bulk_invite_spec.rb
@@ -3,6 +3,7 @@ require 'rails_helper'
 describe Jobs::BulkInvite do
   describe '#execute' do
     let(:user) { Fabricate(:user) }
+    let(:admin) { Fabricate(:admin) }
     let!(:group1) { Fabricate(:group, name: 'group1') }
     let!(:group2) { Fabricate(:group, name: 'group2') }
     let!(:topic) { Fabricate(:topic, id: 999) }
@@ -83,6 +84,22 @@ describe Jobs::BulkInvite do
         group1.id
       )
     end
+
+    it 'adds existing users to valid groups' do
+      existing_user = Fabricate(:user, email: "test@discourse.org")
+
+      group2.update!(automatic: true)
+
+      expect do
+        described_class.new.execute(
+          current_user_id: admin.id,
+          filename: basename
+        )
+      end.to change { Invite.count }.by(1)
+
+      expect(Invite.exists?(email: "test2@discourse.org")).to eq(true)
+      expect(existing_user.reload.groups).to eq([group1])
+    end
   end
 
 end

GitHub sha: 25514550