Class: Merger::ContactPointMerge

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

Instance Method Summary collapse

Constructor Details

#initialize(party_id) ⇒ ContactPointMerge

Returns a new instance of ContactPointMerge.



3
4
5
# File 'app/services/merger/contact_point_merge.rb', line 3

def initialize(party_id)
  @party_id = party_id
end

Instance Method Details

#perform_merge!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'app/services/merger/contact_point_merge.rb', line 7

def perform_merge!
  contacts_ids = [@party_id]
  contacts = Customer.find(@party_id).contacts
  contacts.map{ |r| contacts_ids << r.id } if contacts.present?
  @contact_points = ContactPoint.where(party_id: contacts_ids)
  cp_ids = @contact_points.group(:party_id,:category,:detail).select("min(id) as id").map{|r| r.id}
  @unique_contact_points =  @contact_points.where(id: cp_ids)
  contact_points_remap = {}
  @results = []

  @unique_contact_points.each do |uc|
    notes = [uc.notes]
    locator_record_id = [uc.locator_record_id]
    if (found_contact_points = @contact_points.select{ |cp| uc.id != cp.id and uc.party_id == cp.party_id and uc.category == cp.category and uc.detail == cp.detail }).present?
      @results << "Contact Point #{uc.id} has duplicates, pruning #{found_contact_points.length} records"
      found_contact_points.each do |f|
        contact_points_remap[f.id] = uc.id
        notes << f.notes if f.notes.present?
        locator_record_id << f.locator_record_id if f.locator_record_id.present?
      end
    end
    notes = notes.compact.present? ? notes.compact.map{ |n| n }.join(', ') : nil
    locator_record_id = locator_record_id.compact.present? ? locator_record_id.compact.max : nil
    uc.update_columns(notes: notes, locator_record_id: locator_record_id)
  end

  # Let's remap duplicates and replacements
  contact_points_remap.each do |duplicate, replacement|
    Rails.logger.info " ! Remapping duplicate #{duplicate} to #{replacement}"
    # CommunicationRecipient
    remap_crc = CommunicationRecipient.where("contact_point_id = #{duplicate}").update_all("contact_point_id = #{replacement}")
    Rails.logger.info "CommunicationRecipient updated: #{remap_crc}"
    # ContactPointsFeed
    sql_feed = <<-SQL
      update contact_points_feeds set contact_point_id = #{replacement} where contact_point_id = #{duplicate};
    SQL
    remap_cpf = ActiveRecord::Base.connection.execute(sql_feed).to_a.count
    Rails.logger.info "Contact_Points_Feeds updated: #{remap_cpf}"
    # ContactPointsQuote
    sql_quote = <<-SQL
      update contact_points_quotes set contact_point_id = #{replacement} where contact_point_id = #{duplicate};
    SQL
    remap_cpq = ActiveRecord::Base.connection.execute(sql_quote).to_a.count
    Rails.logger.info "Contact_Points_Feeds updated: #{remap_cpq}"
    # CustomerRecord
    remap_cre = CustomerRecord.where("email_contact_point_id = #{duplicate}").update_all("email_contact_point_id = #{replacement}")
    Rails.logger.info "CustomerRecord updated: #{remap_cre}"
    remap_crp = CustomerRecord.where("portal_contact_point_id = #{duplicate}").update_all("portal_contact_point_id = #{replacement}")
    Rails.logger.info "CustomerRecord updated: #{remap_crp}"
    remap_crh = CustomerRecord.where("phone_contact_point_id = #{duplicate}").update_all("phone_contact_point_id = #{replacement}")
    Rails.logger.info "CustomerRecord updated: #{remap_crh}"
    # EmployeePhoneStatus
    remap_epsc = EmployeePhoneStatus.where("contact_point_id = #{duplicate}").update_all("contact_point_id = #{replacement}")
    Rails.logger.info "EmployeePhoneStatus updated: #{remap_epsc}"
    # NotificationChannel
    remap_ncc = NotificationChannel.where("contact_point_id = #{duplicate}").update_all("contact_point_id = #{replacement}")
    Rails.logger.info "NotificationChannel updated: #{remap_ncc}"
    # OpportunityParticipant
    remap_opc = OpportunityParticipant.where("contact_point_id = #{duplicate}").update_all("contact_point_id = #{replacement}")
    Rails.logger.info "OpportunityParticipant updated: #{remap_opc}"
    # RoomConfiguration
    remap_rcc = RoomConfiguration.where("contact_point_id = #{duplicate}").update_all("contact_point_id = #{replacement}")
    Rails.logger.info "RoomConfiguration updated: #{remap_rcc}"
    # ServiceJob
    remap_sjhp = ServiceJob.where("home_phone_contact_point_id = #{duplicate}").update_all("home_phone_contact_point_id = #{replacement}")
    Rails.logger.info "ServiceJob updated: #{remap_sjhp}"
    remap_sjcp = ServiceJob.where("cell_phone_contact_point_id = #{duplicate}").update_all("cell_phone_contact_point_id = #{replacement}")
    Rails.logger.info "ServiceJob updated: #{remap_sjcp}"
    remap_sjbp = ServiceJob.where("business_phone_contact_point_id = #{duplicate}").update_all("business_phone_contact_point_id = #{replacement}")
    Rails.logger.info "ServiceJob updated: #{remap_sjbp}"
    remap_sjec = ServiceJob.where("email_contact_point_id = #{duplicate}").update_all("email_contact_point_id = #{replacement}")
    Rails.logger.info "ServiceJob updated: #{remap_sjec}"
    # SupportCaseParticipant
    remap_scpp = SupportCaseParticipant.where("phone_id = #{duplicate}").update_all("phone_id = #{replacement}")
    Rails.logger.info "SupportCaseParticipant updated: #{remap_scpp}"
    remap_scpe = SupportCaseParticipant.where("email_id = #{duplicate}").update_all("email_id = #{replacement}")
    Rails.logger.info "SupportCaseParticipant updated: #{remap_scpe}"
    remap_scpf = SupportCaseParticipant.where("fax_id = #{duplicate}").update_all("fax_id = #{replacement}")
    Rails.logger.info "SupportCaseParticipant updated: #{remap_scpf}"

    # After remaping we can delete dupeplicates
    ContactPoint.find(duplicate).destroy
    Rails.logger.info "ContactPoint #{duplicate} destroyed"
  end
end