Class: Merger::ContactMerger
- Inherits:
-
BasePartyMerger
- Object
- BasePartyMerger
- Merger::ContactMerger
- Defined in:
- app/services/merger/contact_merger.rb
Overview
This class is designed to merge two customers
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(contact_master, contact_duplicate) ⇒ ContactMerger
constructor
A new instance of ContactMerger.
- #perform_merge! ⇒ Object
Constructor Details
#initialize(contact_master, contact_duplicate) ⇒ ContactMerger
Returns a new instance of ContactMerger.
21 22 23 24 25 26 |
# File 'app/services/merger/contact_merger.rb', line 21 def initialize(contact_master, contact_duplicate) @survivor = (contact_master.is_a?Contact) ? contact_master : Contact.find(contact_master) @duplicate = (contact_duplicate.is_a?Contact) ? contact_duplicate : Contact.find(contact_duplicate) @results = [] @warnings = [] end |
Class Method Details
.inventory(contact) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/services/merger/contact_merger.rb', line 5 def self.inventory(contact) [ contact, contact.contact_points.to_a, contact.addresses.to_a, # contact.activities.to_a, contact.managed_employees.to_a, contact.opportunities.to_a, contact.orders.to_a, contact.spiff_orders.to_a, contact.spiff_enrollments.to_a, contact.support_case_participants.to_a, contact.room_plans.to_a ].flatten.compact.uniq end |
Instance Method Details
#perform_merge! ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'app/services/merger/contact_merger.rb', line 28 def perform_merge! # Customer passed as @customer_duplicate will be destroyed if the merge is successful Contact.transaction do address_remap = {} contact_point_remap = {} address_remap = {} @survivor.merged_from_ids ||= [] @survivor.merged_from_ids << @duplicate.id @results << "Contact #{@duplicate.id} #{@duplicate.name} is a duplicate of #{@survivor.id} #{@survivor.name}, merging" # Move other things like contact points, activities, etc. @duplicate.contact_points.each do |ccp| if detected_dupe = @survivor.contact_points.detect{|cp2| cp2.detail == ccp.detail and cp2.category == ccp.category } @results << "Contact #{@duplicate.id}'s Contact Point #{ccp.id} #{ccp.category} #{ccp.detail} was ignored since it already exits in Contact #{@survivor.id}" contact_point_remap[ccp.id] = detected_dupe.id else @results << "Contact #{@duplicate.id}'s Contact Point #{ccp.id} #{ccp.category} #{ccp.detail} was added" contact_point_remap[ccp.id] = ccp.id @survivor.contact_points << ccp end end # Move contact addresses into the surviving contact unless duplicates @duplicate.addresses.each do |cad| if detected_dupe = @survivor.addresses.detect{|cad2| cad2 == cad} @results << "Contact #{@duplicate.id}'s Address id #{cad.id} will be ignored as it is a duplicate of address id #{detected_dupe.id} in Contact id #{@survivor.id} #{@survivor.name}" address_remap[cad.id] = detected_dupe.id else @results << "Contact #{@duplicate.id}'s Address id #{cad.id} will be to Contact id #{@survivor.id} #{@survivor.name}" cad.update_column(:party_id, @survivor.id) end end # Reload duplicate to pick up any DB-level changes (e.g., ON DELETE nullify # cascades that may have nullified FK references like profile_image_id if a # previous contact merge in this batch destroyed a shared image). @duplicate.reload # Grab everything else @survivor.attributes = @duplicate.attributes.merge(@survivor.attributes) {|k,oldval,newval| newval.blank? ? oldval : newval } # Safety check: ensure profile_image still exists before saving. # A previous contact merge in the same batch may have cascade-deleted a # shared image via dependent: :destroy on belongs_to :profile_image. if @survivor.profile_image_id.present? && !Image.exists?(@survivor.profile_image_id) @survivor.profile_image_id = nil end @survivor.save! # Remap if @duplicate.activities.present? @results << "Contact #{@duplicate.id}'s Activities (#{@duplicate.activities.pluck(:id).join(',')}) moved to contact id #{@survivor.id} #{@survivor.name}" @duplicate.activities.each do |a| a.update( party_id: @survivor.id, customer_id: @survivor.customer_id, notes: a.notes.presence || a.description || '-' ) end end # credit applications @duplicate.primary_contact_credit_applications.update_all(primary_contact_id: @survivor.id) @duplicate.owner_credit_applications.update_all(owner_id: @survivor.id) @duplicate.accounts_payable_credit_applications.update_all(accounts_payable_contact_id: @survivor.id) @duplicate.purchasing_agent_credit_applications.update_all(purchasing_agent_id: @survivor.id) # managed employees if @duplicate.managed_employees.present? @results << "Contact #{@duplicate.id}'s Managed Employees (#{@duplicate.managed_employees.pluck(:id).join(',')}) moved to contact id #{@survivor.id} #{@survivor.name}" @duplicate.managed_employees.update_all("parent_id = #{@survivor.id}") end # opportunities if @duplicate.opportunities.present? @results << "Contact #{@duplicate.id}'s Opportunities (#{@duplicate.opportunities.pluck(:id).join(',')}) moved to contact id #{@survivor.id} #{@survivor.name}" @duplicate.opportunities.update_all("contact_id = #{@survivor.id}") end # orders if @duplicate.orders.present? @results << "Contact #{@duplicate.id}'s orders (#{@duplicate.orders.pluck(:id).join(',')}) moved to contact id #{@survivor.id} #{@survivor.name}" @duplicate.orders.update_all("contact_id = #{@survivor.id}") end # spiff orders if @duplicate.spiff_orders.present? @results << "Contact #{@duplicate.id}'s spiff_orders (#{@duplicate.spiff_orders.pluck(:id).join(',')}) moved to contact id #{@survivor.id} #{@survivor.name}" @duplicate.spiff_orders.update_all("spiff_rep_id = #{@survivor.id}") end # spiff enrollments @duplicate.spiff_enrollments.each do |se| @results << "Contact #{@duplicate.id}'s spiff enrollment #{se.id} moved" @survivor.spiff_enrollments << se end # visits @duplicate.visits.update_all(user_id: @survivor.id) # room plans @duplicate.room_plans.update_all(party_id: @survivor.id) @survivor.activities.create(:new_note => "Contact Merge Results.\n#{@results.join('\n')}\n\nWarnings: #{@warnings.join('\n')}") append_results merge_destination_call_records(@duplicate, @survivor) append_results merge_origin_call_records(@duplicate, @survivor) append_results merge_origin_call_logs(@duplicate, @survivor) append_results merge_destination_call_logs(@duplicate, @survivor) append_results merge_queue_call_logs(@duplicate, @survivor) append_results merge_inbound_communications(@duplicate, @survivor) append_results merge_party_topics(@duplicate, @survivor) append_results merge_support_case_participants(@duplicate, @survivor) append_results merge_opportunity_participants(@duplicate, @survivor) append_results (@duplicate, @survivor) @duplicate.reload #ensure no stale links remain # Detach profile_image from the duplicate before destroying it. # The duplicate's image may have been transferred to the survivor via # the mass attribute merge. Without this, dependent: :destroy would # cascade-delete the image that the survivor now references. detach_shared_profile_image(@duplicate) @duplicate.destroy @results end end |