Created
February 15, 2019 16:32
-
-
Save pecha7x/db48d83e713d532608721b4831f4d3f0 to your computer and use it in GitHub Desktop.
t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sql = | |
<<-SQL | |
(select person_id from abc_duplicates | |
LEFT JOIN people ON abc_duplicates.person_id = people.id | |
where abc_duplicates.created_at > now()-interval'48 hours' | |
and people.company_id in (select id from companies where group_id=99)) | |
SQL | |
results = ActiveRecord::Base.connection.exec_query(sql) | |
ids = results.map{|r| r['person_id']} | |
incorrect_merged_ids = [] | |
correct_merged_ids = [] | |
need_check_manually = [] | |
Person.where(id: ids).each do |person| | |
chngs = person.audits.where(action: 'update').where("created_at > now()-interval'48 hours' AND audited_changes ~ 'first_name:|email:|phone_mobile:'") | |
if chngs.size == 0 | |
correct_merged_ids << person.id | |
elsif chngs.size > 1 | |
need_check_manually << person.id | |
else | |
changes = chngs.last.audited_changes.slice('email', 'phone_mobile', 'first_name', 'last_name') | |
keys = changes.keys | |
if keys.include?('last_name') | |
incorrect_merged_ids << person.id | |
else | |
if person.last_name.blank? | |
need_check_manually << person.id | |
end | |
end | |
end | |
end;nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment