Refactored legal_document model

This commit is contained in:
Sergei Tsoganov 2022-11-28 13:44:04 +02:00
parent b366aa8d1d
commit c48c85d2ea

View file

@ -14,7 +14,7 @@ class LegalDocument < ApplicationRecord
belongs_to :documentable, polymorphic: true belongs_to :documentable, polymorphic: true
validate :val_body_length, if: ->(file) { file.path.blank? && Rails.env.production? } validate :val_body_length, if: ->(file) { file.path.blank? && (Rails.env.production? || Rails.env.test?) }
before_create :add_creator before_create :add_creator
before_save :save_to_filesystem, if: :body before_save :save_to_filesystem, if: :body
@ -24,7 +24,7 @@ class LegalDocument < ApplicationRecord
'2308' => [ '2308' => [
%i[body length_more_than], %i[body length_more_than],
%i[body length_less_than], %i[body length_less_than],
] ],
} }
end end
@ -41,12 +41,13 @@ class LegalDocument < ApplicationRecord
digest = Digest::SHA1.new.update(binary).to_s digest = Digest::SHA1.new.update(binary).to_s
loop do loop do
rand = SecureRandom.random_number.to_s.last(4) rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4 next if rand.to_i.zero? || rand.length < 4
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775) dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}" FileUtils.mkdir_p(dir, mode: 0775)
break unless File.file?(path) self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
end end
File.open(path, 'wb') { |f| f.write(binary) } unless Rails.env.test? File.open(path, 'wb') { |f| f.write(binary) } unless Rails.env.test?
@ -69,50 +70,57 @@ class LegalDocument < ApplicationRecord
start = Time.zone.now.to_f start = Time.zone.now.to_f
Rails.logger.info '-----> Removing legal documents duplicates' Rails.logger.info '-----> Removing legal documents duplicates'
count = 0 count = 0
modified = Array.new modified = []
LegalDocument.where(documentable_type: "Domain").where.not(checksum: [nil, ""]).find_each do |orig_legal| LegalDocument.where(documentable_type: 'Domain')
.where.not(checksum: [nil, ''])
.find_each do |orig_legal|
next if modified.include?(orig_legal.checksum) next if modified.include?(orig_legal.checksum)
next if !File.exist?(orig_legal.path) next unless File.exist?(orig_legal.path)
modified.push(orig_legal.checksum) modified.push(orig_legal.checksum)
LegalDocument.where(documentable_type: "Domain", documentable_id: orig_legal.documentable_id). LegalDocument.where(documentable_type: 'Domain', documentable_id: orig_legal.documentable_id)
where(checksum: orig_legal.checksum). .where(checksum: orig_legal.checksum)
where.not(id: orig_legal.id).where.not(path: orig_legal.path).each do |new_legal| .where.not(id: orig_legal.id)
unless modified.include?(orig_legal.id) .where.not(path: orig_legal.path).each do |new_legal|
File.delete(new_legal.path) if File.exist?(new_legal.path) next if modified.include?(orig_legal.id)
new_legal.update(path: orig_legal.path)
count += 1 File.delete(new_legal.path) if File.exist?(new_legal.path)
Rails.logger.info "File #{new_legal.path} has been removed by Domain "\ new_legal.update(path: orig_legal.path)
"#{new_legal.documentable_id}. Document id: #{new_legal.id}" count += 1
end Rails.logger.info "File #{new_legal.path} has been removed by Domain "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
end end
contact_ids = Version::DomainVersion.where(item_id: orig_legal.documentable_id).distinct. contact_ids = Version::DomainVersion.where(item_id: orig_legal.documentable_id).distinct
pluck("object->>'registrant_id'", "object_changes->>'registrant_id'", .pluck("object->>'registrant_id'",
"children->>'tech_contacts'", "children->>'admin_contacts'").flatten.uniq "object_changes->>'registrant_id'",
contact_ids = contact_ids.map{|id| "children->>'tech_contacts'",
"children->>'admin_contacts'")
.flatten.uniq
contact_ids = contact_ids.map do |id|
case id case id
when Hash when Hash
id["id"] id['id']
when String when String
JSON.parse(id) rescue id.to_i JSON.parse(id) rescue id.to_i
else else
id id
end
}.flatten.compact.uniq
LegalDocument.where(documentable_type: "Contact", documentable_id: contact_ids).
where(checksum: orig_legal.checksum).where.not(path: orig_legal.path).each do |new_legal|
unless modified.include?(orig_legal.id)
File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
Rails.logger.info "File #{new_legal.path} has been removed by Contact "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
end end
end.flatten.compact.uniq
LegalDocument.where(documentable_type: 'Contact', documentable_id: contact_ids)
.where(checksum: orig_legal.checksum)
.where.not(path: orig_legal.path).each do |new_legal|
next if modified.include?(orig_legal.id)
File.delete(new_legal.path) if File.exist?(new_legal.path)
new_legal.update(path: orig_legal.path)
count += 1
Rails.logger.info "File #{new_legal.path} has been removed by Contact "\
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
end end
end end
Rails.logger.info "-----> Duplicates fixed for #{count} rows in #{(Time.zone.now.to_f - start).round(2)} seconds" Rails.logger.info "-----> Duplicates fixed for #{count} rows in #{(Time.zone.now.to_f - start).round(2)} seconds"
end end
end end