2019-12-24 07:57:25 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ::ContactMergeAction do
|
|
|
|
subject(:contact_merge) { described_class.new(account: account, base_contact: base_contact, mergee_contact: mergee_contact).perform }
|
|
|
|
|
|
|
|
let!(:account) { create(:account) }
|
2021-08-31 10:00:18 +00:00
|
|
|
let!(:base_contact) do
|
|
|
|
create(:contact, identifier: 'base_contact', email: 'old@old.com', phone_number: '', custom_attributes: { val_test: 'old', val_empty_old: '' },
|
|
|
|
account: account)
|
|
|
|
end
|
|
|
|
let!(:mergee_contact) do
|
|
|
|
create(:contact, identifier: '', email: 'new@new.com', phone_number: '+12212345',
|
|
|
|
custom_attributes: { val_test: 'new', val_new: 'new', val_empty_new: '' }, account: account)
|
|
|
|
end
|
2019-12-24 07:57:25 +00:00
|
|
|
|
|
|
|
before do
|
2021-08-03 14:41:52 +00:00
|
|
|
2.times.each do
|
|
|
|
create(:conversation, contact: base_contact)
|
|
|
|
create(:conversation, contact: mergee_contact)
|
|
|
|
create(:message, sender: mergee_contact)
|
|
|
|
end
|
2019-12-24 07:57:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#perform' do
|
|
|
|
it 'deletes mergee_contact' do
|
|
|
|
contact_merge
|
|
|
|
expect { mergee_contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
2021-08-31 10:00:18 +00:00
|
|
|
it 'copies information from mergee contact to base contact' do
|
|
|
|
contact_merge
|
|
|
|
base_contact.reload
|
|
|
|
expect(base_contact.identifier).to eq('base_contact')
|
|
|
|
expect(base_contact.email).to eq('old@old.com')
|
|
|
|
expect(base_contact.phone_number).to eq('+12212345')
|
|
|
|
expect(base_contact.custom_attributes['val_test']).to eq('old')
|
|
|
|
expect(base_contact.custom_attributes['val_new']).to eq('new')
|
|
|
|
expect(base_contact.custom_attributes['val_empty_old']).to eq('')
|
|
|
|
expect(base_contact.custom_attributes['val_empty_new']).to eq('')
|
|
|
|
end
|
|
|
|
|
2021-06-22 10:27:48 +00:00
|
|
|
context 'when base contact and merge contact are same' do
|
|
|
|
it 'does not delete contact' do
|
|
|
|
mergee_contact = base_contact
|
|
|
|
contact_merge
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(mergee_contact.reload).not_to be_nil
|
2021-06-22 10:27:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
context 'when mergee contact has conversations' do
|
|
|
|
it 'moves the conversations to base contact' do
|
|
|
|
contact_merge
|
|
|
|
expect(base_contact.conversations.count).to be 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when mergee contact has contact inboxes' do
|
|
|
|
it 'moves the contact inboxes to base contact' do
|
|
|
|
contact_merge
|
|
|
|
expect(base_contact.contact_inboxes.count).to be 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-18 20:22:38 +00:00
|
|
|
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
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
context 'when contacts belong to a different account' do
|
|
|
|
it 'throws an exception' do
|
|
|
|
new_account = create(:account)
|
|
|
|
expect do
|
|
|
|
described_class.new(account: new_account, base_contact: base_contact,
|
|
|
|
mergee_contact: mergee_contact).perform
|
2020-05-05 18:40:56 +00:00
|
|
|
end.to raise_error('contact does not belong to the account')
|
2019-12-24 07:57:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|