Class: Merger::BasePartyMerger

Inherits:
BaseMerger
  • Object
show all
Defined in:
app/services/merger/base_party_merger.rb

Instance Method Summary collapse

Instance Method Details

#detach_shared_profile_image(party) ⇒ Object

Detach profile_image from a party about to be destroyed.
Party has belongs_to :profile_image, dependent: :destroy, so destroying
a party cascade-deletes its Image. If any other party still references the
same image (e.g., the merge survivor or a contact created from the party),
we must detach it first to prevent cascade deletion of a shared image.



8
9
10
11
12
13
14
15
16
17
# File 'app/services/merger/base_party_merger.rb', line 8

def detach_shared_profile_image(party)
  return unless party.profile_image_id.present?

  party.reload # Ensure we have the latest DB value
  return unless party.profile_image_id.present?

  if Party.where(profile_image_id: party.profile_image_id).where.not(id: party.id).exists?
    party.update_column(:profile_image_id, nil)
  end
end

#merge_destination_call_logs(origin_party, target_party) ⇒ Object



37
38
39
40
41
# File 'app/services/merger/base_party_merger.rb', line 37

def merge_destination_call_logs(origin_party, target_party)
  call_count = origin_party.destination_call_logs.size
  origin_party.destination_call_logs.update_all({ to_party_id: target_party.id })
  ["Migrated #{call_count} destination call logs"]
end

#merge_destination_call_records(origin_party, target_party) ⇒ Object



25
26
27
28
29
# File 'app/services/merger/base_party_merger.rb', line 25

def merge_destination_call_records(origin_party, target_party)
  call_count = origin_party.destination_call_records.size
  origin_party.destination_call_records.update_all({ destination_party_id: target_party.id })
  ["Migrated #{call_count} destination call records"]
end

#merge_inbound_communications(origin_party, target_party) ⇒ Object



68
69
70
71
72
# File 'app/services/merger/base_party_merger.rb', line 68

def merge_inbound_communications(origin_party, target_party)
  communication_count = origin_party.inbound_communications.size
  origin_party.inbound_communications.update_all({ recipient_party_id: target_party.id })
  ["Migrated #{communication_count} communications."]
end

#merge_opportunity_participants(origin_party, target_party) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/services/merger/base_party_merger.rb', line 90

def merge_opportunity_participants(origin_party, target_party)
  # move opportunity participants
  count = 0
  origin_party.opportunity_participants.group_by(&:opportunity).each do |opportunity, participants|
    participants.each do |p|
      if opportunity.opportunity_participants.where(party_id: target_party.id).exists? || opportunity.customer_id == target_party.id || opportunity.contact_id == target_party.id
        p.delete
      else
        count += 1
        p.update_column(:party_id, target_party.id)
      end
    end
  end
  ["Migrated #{count} opportunity participants"]
end

#merge_origin_call_logs(origin_party, target_party) ⇒ Object



31
32
33
34
35
# File 'app/services/merger/base_party_merger.rb', line 31

def merge_origin_call_logs(origin_party, target_party)
  call_count = origin_party.origin_call_logs.size
  origin_party.origin_call_logs.update_all({ from_party_id: target_party.id })
  ["Migrated #{call_count} origin call logs"]
end

#merge_origin_call_records(origin_party, target_party) ⇒ Object



19
20
21
22
23
# File 'app/services/merger/base_party_merger.rb', line 19

def merge_origin_call_records(origin_party, target_party)
  call_count = origin_party.origin_call_records.size
  origin_party.origin_call_records.update_all({ origin_party_id: target_party.id })
  ["Migrated #{call_count} origin call records"]
end

#merge_party_topics(origin_party, target_party) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/merger/base_party_merger.rb', line 49

def merge_party_topics(origin_party, target_party)
  topic_count = 0
  origin_party.party_topics.each do |pt_o|
    if target_party.party_topics.detect{|pt_t| pt_t.topic_id == pt_o.topic_id }
      pt_o.delete #Ignore
    else
      topic_count += 1
      pt_o.update_column(:party_id, target_party.id)
    end
  end
  ["Migrated #{topic_count} party topics."]
end

#merge_queue_call_logs(origin_party, target_party) ⇒ Object



43
44
45
46
47
# File 'app/services/merger/base_party_merger.rb', line 43

def merge_queue_call_logs(origin_party, target_party)
  call_count = origin_party.queue_call_logs.size
  origin_party.queue_call_logs.update_all({ caller_party_id: target_party.id })
  ["Migrated #{call_count} queue call logs"]
end

#merge_sms_messages(origin_party, target_party) ⇒ Object



62
63
64
65
66
# File 'app/services/merger/base_party_merger.rb', line 62

def merge_sms_messages(origin_party, target_party)
  SmsMessage.where(sender_party_id: origin_party.id).update_all(sender_party_id: target_party.id)
  SmsMessage.where(recipient_party_id: origin_party.id).update_all(recipient_party_id: target_party.id)
  ["Migrated SMS Messages"]
end

#merge_support_case_participants(origin_party, target_party) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/merger/base_party_merger.rb', line 74

def merge_support_case_participants(origin_party, target_party)
  role = SupportCaseParticipant::ROLES.detect { |k, v| v == target_party.profile_id && k != 'iso' && k != 'unknown' }&.first || 'unknown'
  count = 0
  origin_party.support_case_participants.group_by(&:support_case).each do |support_case, participants|
    participants.each do |p|
      if support_case.support_case_participants.where(party_id: target_party.id).exists?
        p.destroy
      else
        count += 1
        p.update_columns(party_id: target_party.id, role: role)
      end
    end
  end
  ["Migrated #{count} support case participants"]
end