chore: Merge contact copy over information (#2812)

fixes: #2767, #2773

Co-authored-by: Nithin David <1277421+nithindavid@users.noreply.github.com>
This commit is contained in:
Sojan Jose
2021-08-31 15:30:18 +05:30
committed by GitHub
parent 1ff9939a80
commit 5319af3dfc
6 changed files with 64 additions and 15 deletions

View File

@@ -1,4 +1,5 @@
class ContactMergeAction
include Events::Types
pattr_initialize [:account!, :base_contact!, :mergee_contact!]
def perform
@@ -11,7 +12,7 @@ class ContactMergeAction
merge_conversations
merge_messages
merge_contact_inboxes
remove_mergee_contact
merge_and_remove_mergee_contact
end
@base_contact
end
@@ -40,7 +41,18 @@ class ContactMergeAction
ContactInbox.where(contact_id: @mergee_contact.id).update(contact_id: @base_contact.id)
end
def remove_mergee_contact
def merge_and_remove_mergee_contact
mergable_attribute_keys = %w[identifier name email phone_number custom_attributes]
base_contact_attributes = base_contact.attributes.slice(*mergable_attribute_keys).compact_blank
mergee_contact_attributes = mergee_contact.attributes.slice(*mergable_attribute_keys).compact_blank
# attributes in base contact are given preference
merged_attributes = mergee_contact_attributes.deep_merge(base_contact_attributes)
# retaining old pubsub token to notify the contacts that are listening
mergee_pubsub_token = mergee_contact.pubsub_token
@mergee_contact.destroy!
Rails.configuration.dispatcher.dispatch(CONTACT_MERGED, Time.zone.now, contact: @base_contact, tokens: [mergee_pubsub_token])
@base_contact.update!(merged_attributes)
end
end