From 1c4566df66890699d6ec0d8f5be2dd60a5e0a717 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 19 Mar 2020 01:52:38 +0530 Subject: [PATCH] Bug: Migrate messages while merging contact (#624) Bug: Migrate messages while merging contact --- app/actions/contact_merge_action.rb | 5 +++++ app/models/contact.rb | 1 + spec/actions/contact_merge_action_spec.rb | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/app/actions/contact_merge_action.rb b/app/actions/contact_merge_action.rb index 61a26e185..393b3861b 100644 --- a/app/actions/contact_merge_action.rb +++ b/app/actions/contact_merge_action.rb @@ -5,6 +5,7 @@ class ContactMergeAction ActiveRecord::Base.transaction do validate_contacts merge_conversations + merge_messages merge_contact_inboxes remove_mergee_contact end @@ -26,6 +27,10 @@ class ContactMergeAction Conversation.where(contact_id: @mergee_contact.id).update(contact_id: @base_contact.id) end + def merge_messages + Message.where(contact_id: @mergee_contact.id).update(contact_id: @base_contact.id) + end + def merge_contact_inboxes ContactInbox.where(contact_id: @mergee_contact.id).update(contact_id: @base_contact.id) end diff --git a/app/models/contact.rb b/app/models/contact.rb index 112a261c8..9e07fc8fe 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -28,6 +28,7 @@ class Contact < ApplicationRecord has_many :conversations, dependent: :destroy has_many :contact_inboxes, dependent: :destroy has_many :inboxes, through: :contact_inboxes + has_many :messages, dependent: :destroy def get_source_id(inbox_id) contact_inboxes.find_by!(inbox_id: inbox_id).source_id diff --git a/spec/actions/contact_merge_action_spec.rb b/spec/actions/contact_merge_action_spec.rb index bf95b7291..69f481c73 100644 --- a/spec/actions/contact_merge_action_spec.rb +++ b/spec/actions/contact_merge_action_spec.rb @@ -10,6 +10,7 @@ describe ::ContactMergeAction do before do 2.times.each { create(:conversation, contact: base_contact) } 2.times.each { create(:conversation, contact: mergee_contact) } + 2.times.each { create(:message, contact: mergee_contact) } end describe '#perform' do @@ -32,6 +33,13 @@ describe ::ContactMergeAction do end end + context 'when mergee contact has messages' do + it 'moves the messages to base contact' do + contact_merge + expect(base_contact.messages.count).to be 2 + end + end + context 'when contacts belong to a different account' do it 'throws an exception' do new_account = create(:account)