From bd9bac75adfe944cdeec1d5dd55dfdb2be37c735 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 22 Jun 2021 15:57:48 +0530 Subject: [PATCH] fix: Adds condition to handle base and mergee contact being same (#2485) When an agent updates a contact email in the dashboard and the contact also update his email via the email collect box, Merge action receives the same base and mergee contacts which could cause data corruption. --- app/actions/contact_merge_action.rb | 4 ++++ spec/actions/contact_merge_action_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/actions/contact_merge_action.rb b/app/actions/contact_merge_action.rb index f2d9b6d06..3a3ef11ed 100644 --- a/app/actions/contact_merge_action.rb +++ b/app/actions/contact_merge_action.rb @@ -2,6 +2,10 @@ class ContactMergeAction pattr_initialize [:account!, :base_contact!, :mergee_contact!] def perform + # This case happens when an agent updates a contact email in dashboard, + # while the contact also update his email via email collect box + return @base_contact if base_contact.id == mergee_contact.id + ActiveRecord::Base.transaction do validate_contacts merge_conversations diff --git a/spec/actions/contact_merge_action_spec.rb b/spec/actions/contact_merge_action_spec.rb index 815518ae3..9cbdeb343 100644 --- a/spec/actions/contact_merge_action_spec.rb +++ b/spec/actions/contact_merge_action_spec.rb @@ -19,6 +19,14 @@ describe ::ContactMergeAction do expect { mergee_contact.reload }.to raise_error(ActiveRecord::RecordNotFound) end + context 'when base contact and merge contact are same' do + it 'does not delete contact' do + mergee_contact = base_contact + contact_merge + expect { mergee_contact.reload }.not_to raise_error(ActiveRecord::RecordNotFound) + end + end + context 'when mergee contact has conversations' do it 'moves the conversations to base contact' do contact_merge