mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Refactored legal_document model
This commit is contained in:
parent
b366aa8d1d
commit
c48c85d2ea
1 changed files with 50 additions and 42 deletions
|
@ -14,7 +14,7 @@ class LegalDocument < ApplicationRecord
|
|||
|
||||
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_save :save_to_filesystem, if: :body
|
||||
|
@ -24,7 +24,7 @@ class LegalDocument < ApplicationRecord
|
|||
'2308' => [
|
||||
%i[body length_more_than],
|
||||
%i[body length_less_than],
|
||||
]
|
||||
],
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -41,12 +41,13 @@ class LegalDocument < ApplicationRecord
|
|||
digest = Digest::SHA1.new.update(binary).to_s
|
||||
|
||||
loop do
|
||||
rand = SecureRandom.random_number.to_s.last(4)
|
||||
next if rand.to_i == 0 || rand.length < 4
|
||||
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
|
||||
FileUtils.mkdir_p(dir, mode: 0775)
|
||||
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
|
||||
break unless File.file?(path)
|
||||
rand = SecureRandom.random_number.to_s.last(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)
|
||||
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
|
||||
break unless File.file?(path)
|
||||
end
|
||||
|
||||
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
|
||||
Rails.logger.info '-----> Removing legal documents duplicates'
|
||||
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 !File.exist?(orig_legal.path)
|
||||
next unless File.exist?(orig_legal.path)
|
||||
|
||||
modified.push(orig_legal.checksum)
|
||||
|
||||
LegalDocument.where(documentable_type: "Domain", documentable_id: orig_legal.documentable_id).
|
||||
where(checksum: orig_legal.checksum).
|
||||
where.not(id: orig_legal.id).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 Domain "\
|
||||
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
|
||||
end
|
||||
LegalDocument.where(documentable_type: 'Domain', documentable_id: orig_legal.documentable_id)
|
||||
.where(checksum: orig_legal.checksum)
|
||||
.where.not(id: orig_legal.id)
|
||||
.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 Domain "\
|
||||
"#{new_legal.documentable_id}. Document id: #{new_legal.id}"
|
||||
end
|
||||
|
||||
contact_ids = Version::DomainVersion.where(item_id: orig_legal.documentable_id).distinct.
|
||||
pluck("object->>'registrant_id'", "object_changes->>'registrant_id'",
|
||||
"children->>'tech_contacts'", "children->>'admin_contacts'").flatten.uniq
|
||||
contact_ids = contact_ids.map{|id|
|
||||
contact_ids = Version::DomainVersion.where(item_id: orig_legal.documentable_id).distinct
|
||||
.pluck("object->>'registrant_id'",
|
||||
"object_changes->>'registrant_id'",
|
||||
"children->>'tech_contacts'",
|
||||
"children->>'admin_contacts'")
|
||||
.flatten.uniq
|
||||
contact_ids = contact_ids.map do |id|
|
||||
case id
|
||||
when Hash
|
||||
id["id"]
|
||||
when String
|
||||
JSON.parse(id) rescue id.to_i
|
||||
else
|
||||
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}"
|
||||
when Hash
|
||||
id['id']
|
||||
when String
|
||||
JSON.parse(id) rescue id.to_i
|
||||
else
|
||||
id
|
||||
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
|
||||
Rails.logger.info "-----> Duplicates fixed for #{count} rows in #{(Time.zone.now.to_f - start).round(2)} seconds"
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue