Class: Merger::ContactToCustomerMerger

Inherits:
BasePartyMerger
  • Object
show all
Defined in:
app/services/merger/contact_to_customer_merger.rb

Overview

This class is designed to merge a contact into the customer (sometimes homeowners have a company where its not needed)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_master, contact_duplicate) ⇒ ContactToCustomerMerger

Returns a new instance of ContactToCustomerMerger.



21
22
23
24
25
26
# File 'app/services/merger/contact_to_customer_merger.rb', line 21

def initialize(customer_master, contact_duplicate)
  @survivor = (Customer === customer_master) ? customer_master : Customer.find(customer_master)
  @duplicate = (Contact === contact_duplicate) ? 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_to_customer_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
# File 'app/services/merger/contact_to_customer_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 = {}

    @results << "Contact #{@duplicate.id} #{@duplicate.name} will be merged into #{@survivor.id} #{@survivor.name}"
    #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 Customer id #{@survivor.id} #{@survivor.name}"
        address_remap[cad.id] = detected_dupe.id
      else
        @results << "Contact #{@duplicate.id}'s Address id #{cad.id} will added to Customer id #{@survivor.id} #{@survivor.name}"
        cad.update_column(:party_id, @survivor.id)
      end
    end

    @survivor.save!
    #Remap
    if @duplicate.activities.present?
      @results << "Contact #{@duplicate.id}'s Activities (#{@duplicate.activities.pluck(:id).join(',')}) moved to customer 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
    if @duplicate.managed_employees.present?
      @results << "Contact #{@duplicate.id}'s Managed Employees will be disconnted (#{@duplicate.managed_employees.pluck(:id).join(',')})"
      @duplicate.managed_employees.update_all(parent_id: nil)
    end
    if @duplicate.opportunities.present?
      @results << "Contact #{@duplicate.id}'s Opportunities (#{@duplicate.opportunities.pluck(:id).join(',')}) reset"
      @duplicate.opportunities.update_all(contact_id: nil)
    end
    if @duplicate.orders.present?
      @results << "Contact #{@duplicate.id}'s orders (#{@duplicate.orders.pluck(:id).join(',')}) reset"
      @duplicate.orders.update_all(contact_id: nil)
    end
    if @duplicate.spiff_orders.present?
      @results << "Contact #{@duplicate.id}'s spiff_orders (#{@duplicate.spiff_orders.pluck(:id).join(',')}) reset"
      @duplicate.spiff_orders.update_all(spiff_rep_id: nil)
    end
    @duplicate.spiff_enrollments.each do |se|
      @results << "Contact #{@duplicate.id}'s spiff enrollment #{se.id} deleted"
      se.destroy
    end
    #Visits
    @duplicate.visits.update_all(user_id: @survivor.id)

    #Room Plans
    @duplicate.room_plans.update_all(party_id: @survivor.id)

    @survivor.quick_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)

    @duplicate.reload #ensure no stale links remain
    @duplicate.destroy
    @results
  end
end