DEV: don't merge email address if target user is not human. (#13915)

DEV: don’t merge email address if target user is not human. (#13915)

While merging two user accounts don’t merge the source user’s email address if the target user is not a human.

Co-authored-by: Alan Guo Xiang Tan gxtan1990@gmail.com

diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb
index c322d5f..1284dd6 100644
--- a/app/services/user_merger.rb
+++ b/app/services/user_merger.rb
@@ -363,7 +363,9 @@ class UserMerger
 
     update_user_id(:user_custom_fields, conditions: "x.name = y.name")
 
-    update_user_id(:user_emails, conditions: "x.email = y.email OR y.primary = false", updates: '"primary" = false')
+    if @target_user.human?
+      update_user_id(:user_emails, conditions: "x.email = y.email OR y.primary = false", updates: '"primary" = false')
+    end
 
     UserExport.where(user_id: @source_user.id).update_all(user_id: @target_user.id)
 
diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb
index 55996ac..06d3a51 100644
--- a/spec/services/user_merger_spec.rb
+++ b/spec/services/user_merger_spec.rb
@@ -848,6 +848,15 @@ describe UserMerger do
     expect(UserEmail.where(user_id: source_user.id).count).to eq(0)
   end
 
+  it "skips merging email addresses when target user is not human" do
+    target_user = Discourse.system_user
+    merge_users!(source_user, target_user)
+
+    emails = UserEmail.where(user_id: target_user.id).pluck(:email, :primary)
+    expect(emails).to contain_exactly([target_user.email, true])
+    expect(UserEmail.exists?(user_id: source_user.id)).to eq(false)
+  end
+
   it "updates exports" do
     UserExport.create(file_name: "user-archive-alice1-190218-003249", user_id: source_user.id)
 

GitHub sha: 4ec2c1e9a9e6e967b66c8aa878fd195b0bccfac0

This commit appears in #13915 which was approved by tgxworld. It was merged by vinothkannans.