Bug: Migrate messages while merging contact (#624)
Bug: Migrate messages while merging contact
This commit is contained in:
parent
9d170fc727
commit
1c4566df66
3 changed files with 14 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue