Skip to content

Commit

Permalink
Import contact history data from FRED
Browse files Browse the repository at this point in the history
  • Loading branch information
yulgolem committed May 15, 2020
1 parent 13e76ff commit 2c581be
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
57 changes: 55 additions & 2 deletions app/jobs/copy_fred_history_job.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class CopyFredHistoryJob < Que::Job
include FredSqlUtils
attr_accessor :initial_contacts_history, :object_history, :domain_contacts
attr_accessor :initial_contacts_history, :object_history, :domain_contacts, :contacts_history

def run
process_domains
# process_contacts
process_contacts
end

def process_domain(domain)
Expand Down Expand Up @@ -62,8 +62,48 @@ def process_domains
end
end

def process_contact(contact)

new_history_array = []
history_entries = contacts_history.select { |entry| entry[:id] == contact.legacy_id }
history_entries.each do |entry|
recorded_at = entry[:update]
attrs = { recorded_at: recorded_at, object_id: contact.id }

already_exist = Audit::DomainHistory.find_by(attrs).present?
next if already_exist

attrs[:action] = 'UPDATE'
attrs[:old_value] = {}
attrs[:new_value] = {
id: contact.id,
fax: entry[:fax],
zip: entry[:postalcode],
city: entry[:city],
name: entry[:name],
email: entry[:email],
ident: entry[:ssn],
phone: entry[:telephone],
state: entry[:stateorprovince],
street: entry[:street1],
legacy_id: entry[:id],
country_code: entry[:country],
ident_country_code: entry[:country]
}
new_history_array << attrs
end

Audit::ContactHistory.transaction { Audit::ContactHistory.import new_history_array }
end

def process_contacts

Contact.without_ignored_columns do
fred_history_contacts = Contact.where.not(legacy_id: nil)
fred_history_contacts.find_each do |contact|
process_contact(contact)
end
end
end

def process_status(status)
Expand Down Expand Up @@ -142,5 +182,18 @@ def initial_contacts_request
SQL
result_entries(sql: sql)
end

def contacts_history
@contacts_history ||= contacts_history_request
end

def contacts_history_request
sql = <<~SQL.gsub(/\s+/, " ").strip
select ch.*, h.valid_from as update
from contact_history ch
join history h on ch.historyid = h.id
SQL
result_entries(sql: sql)
end
end

8 changes: 8 additions & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,12 @@ def registrant?
def deletable?
!linked?
end

def self.without_ignored_columns
been_ignored = ignored_columns
self.ignored_columns = []
yield

self.ignored_columns = been_ignored
end
end

0 comments on commit 2c581be

Please sign in to comment.