Class: Merger::BasePartyMerger

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

Overview

Service object: base party merger.

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.



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

def detach_shared_profile_image(party)
  return if party.profile_image_id.blank?

  party.reload # Ensure we have the latest DB value
  return if party.profile_image_id.blank?

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

#merge_destination_call_logs(origin_party, target_party) ⇒ Object



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

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



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

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



81
82
83
84
85
# File 'app/services/merger/base_party_merger.rb', line 81

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/merger/base_party_merger.rb', line 103

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



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

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



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

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_research_runs(origin_party, target_party) ⇒ Object

Move Lead Enrichment runs (and their findings, via FK) from the
origin party to the target. The party_research_runs.party_id FK
has no ON DELETE clause, so destroying the origin party with runs
still attached raises PG::ForeignKeyViolation on
fk_rails_cf5884f082 and the whole merge transaction rolls back.
Runs are historical (one row per "Enrich" click); both parties'
runs survive on the target after merge — no dedup needed since the
schema has no uniqueness on party_id.



75
76
77
78
79
# File 'app/services/merger/base_party_merger.rb', line 75

def merge_party_research_runs(origin_party, target_party)
  run_count = PartyResearchRun.where(party_id: origin_party.id).count
  PartyResearchRun.where(party_id: origin_party.id).update_all(party_id: target_party.id)
  ["Migrated #{run_count} party research run(s)"]
end

#merge_party_topics(origin_party, target_party) ⇒ Object



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

def merge_party_topics(origin_party, target_party)
  topic_count = 0
  origin_party.party_topics.each do |pt_o|
    if target_party.party_topics.find { |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



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

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



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

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



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

def merge_support_case_participants(origin_party, target_party)
  role = SupportCaseParticipant::ROLES.find { |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