Skip to content

Commit

Permalink
updated company register status job
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed May 8, 2024
1 parent a447d71 commit 546898c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 34 deletions.
2 changes: 2 additions & 0 deletions app/interactions/domains/force_delete/set_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def force_delete_fast_track
expire_warning_period_days +
redemption_grace_period_days
domain.force_delete_start = Time.zone.today + 1.day

domain.status_notes[DomainStatus::FORCE_DELETE] = "Company no: #{domain.registrant.ident}" if reason == 'invalid_company'
end

def force_delete_soft
Expand Down
108 changes: 89 additions & 19 deletions app/jobs/company_register_status_job.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,97 @@
require 'zip'

class CompanyRegisterStatusJob < ApplicationJob
queue_as :default

def perform(days_interval = 14, spam_time_delay = 0.2, batch_size = 100, force_delete = false)
sampling_registrant_contact(days_interval).find_in_batches(batch_size: batch_size) do |contacts|
contacts.each do |contact|
# avoid spamming company register
sleep spam_time_delay
FILENAME = 'ettevotja_rekvisiidid__lihtandmed.csv.zip'
UNZIP_FILENAME = 'ettevotja_rekvisiidid__lihtandmed.csv'
DESTINATION = 'lib/tasks/data/'

company_status = contact.return_company_status
contact.update!(company_register_status: company_status, checked_company_at: Time.zone.now)
def perform(days_interval = 14, spam_time_delay = 0.2, batch_size = 100, download_open_data_file_url='https://avaandmed.ariregister.rik.ee/sites/default/files/avaandmed/ettevotja_rekvisiidid__lihtandmed.csv.zip')

ContactInformMailer.company_liquidation(contact: contact).deliver_now if company_status == Contact::LIQUIDATED
download_open_data_file(download_open_data_file_url, DESTINATION + FILENAME)
unzip_file(FILENAME, DESTINATION)

next unless [Contact::BANKRUPT, Contact::DELETED, nil].include? company_status
codes_in_csv = collect_company_codes(DESTINATION + UNZIP_FILENAME)

if force_delete
schedule_force_delete(contact)
sampling_registrant_contact(days_interval).find_in_batches(batch_size: batch_size) do |contacts|
contacts.each do |contact|
if codes_in_csv.include?(contact.ident)
proceed_company_status(contact, spam_time_delay)
else
generate_alert_list(contact, company_status)
schedule_force_delete(contact)
end
end
end

remove_temp_file(DESTINATION + UNZIP_FILENAME)
end

private

def proceed_company_status(contact, spam_time_delay)
# avoid spamming company register
sleep spam_time_delay

company_status = contact.return_company_status
contact.update!(company_register_status: company_status, checked_company_at: Time.zone.now)

puts company_status
case company_status
when Contact::REGISTERED
lift_force_delete(contact) if check_for_force_delete(contact)
when Contact::LIQUIDATED
ContactInformMailer.company_liquidation(contact: contact).deliver_now
when Contact::BANKRUPT || Contact::DELETED
schedule_force_delete(contact)
end
end

def collect_company_codes(open_data_file_path)
codes_in_csv = []
CSV.foreach(open_data_file_path, headers: true, col_sep: ';', quote_char: '"', liberal_parsing: true) do |row|
codes_in_csv << row['ariregistri_kood']
end

codes_in_csv
end

def download_open_data_file(url, filename)
uri = URI(url)

Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new(uri)
response = http.request(request)

if response.code == '200'
File.open(filename, 'wb') do |file|
file.write(response.body)
end
else
puts "Failed to download file: #{response.code} #{response.message}"
end
end

puts "File saved as #{filename}"
end

def unzip_file(filename, destination)
::Zip::File.open(destination + filename) do |zip_file|
zip_file.each do |entry|
entry.extract(File.join(destination, entry.name)) { true }
end
end

puts "Archive invoke to #{destination}"
end

def sampling_registrant_contact(days_interval)
Registrant.where(ident_type: 'org')
Registrant.where(ident_type: 'org', ident_country_code: 'EE')
.where('(company_register_status IS NULL) OR
(company_register_status = ? AND (checked_company_at IS NULL OR checked_company_at <= ?)) OR
(company_register_status = ? AND (checked_company_at IS NULL OR checked_company_at <= ?))',
Contact::REGISTERED, days_interval.days.ago, Contact::LIQUIDATED, 1.day.ago)
end

def generate_alert_list(contact, company_status)
File.open(Rails.root.join('contact_companies_alert_list.txt'), 'a') do |f|
f.puts "#{contact.name} - #{contact.ident} - #{company_status}"
end
end

def schedule_force_delete(contact)
contact.domains.each do |domain|
Expand All @@ -49,4 +103,20 @@ def schedule_force_delete(contact)
)
end
end

def check_for_force_delete(contact)
contact.domains.any? do |domain|
domain.schedule_force_delete? && domain.status_notes[DomainStatus::FORCE_DELETE].include?("Company no: #{contact.ident}")
end
end

def lift_force_delete(contact)
contact.domains.each do |domain|
domain.lift_force_delete
end
end

def remove_temp_file(distination)
FileUtils.rm(distination) if File.exist?(distination)
end
end
8 changes: 0 additions & 8 deletions app/models/contact/company_register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ def return_company_details
[]
end

def return_entries_and_rulings
return unless org?

company_register.entries_and_rulings(start_at: '2019-01-18T11:57:00', ends_at: '2019-01-19T11:57:00')
rescue CompanyRegister::NotAvailableError
[]
end

def company_register
@company_register ||= CompanyRegister::Client.new
end
Expand Down
13 changes: 6 additions & 7 deletions lib/tasks/company_status.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ require 'optparse'
require 'rake_option_parser_boilerplate'


namespace :companies do
# bundle exec rake companies:check_all -- --open_data_file_path=lib/tasks/data/ettevotja_rekvisiidid__lihtandmed.csv --missing_companies_output_path=lib/tasks/data/missing_companies_in_business_registry.csv --deleted_companies_output_path=lib/tasks/data/deleted_companies_from_business_registry.csv --download_path=https://avaandmed.ariregister.rik.ee/sites/default/files/avaandmed/ettevotja_rekvisiidid__lihtandmed.csv.zip
namespace :company_status do
# bundle exec rake company_status:check_for_exists -- --open_data_file_path=lib/tasks/data/ettevotja_rekvisiidid__lihtandmed.csv --missing_companies_output_path=lib/tasks/data/missing_companies_in_business_registry.csv --deleted_companies_output_path=lib/tasks/data/deleted_companies_from_business_registry.csv --download_path=https://avaandmed.ariregister.rik.ee/sites/default/files/avaandmed/ettevotja_rekvisiidid__lihtandmed.csv.zip
desc 'Get Estonian companies status from Business Registry.'

DELETED_FROM_REGISTRY_STATUS = 'K'
FILENAME = 'opendata_business_registry.csv.zip'
DESTINATION = 'lib/tasks/data/'

task :check_all => :environment do
task :check_for_exists => :environment do
options = initialize_rake_task

open_data_file_path = options[:open_data_file_path]
Expand All @@ -26,9 +27,7 @@ namespace :companies do
puts "*** Run 1 step. Downloading fresh open data file. ***"

download_open_data_file(download_path, FILENAME)

destination = 'lib/tasks/data/'
unzip_dile(FILENAME, destination)
unzip_file(FILENAME, DESTINATION)

# Remove old file
remove_old_file(output_file_path)
Expand Down Expand Up @@ -81,7 +80,7 @@ namespace :companies do
end


def unzip_dile(filename, destination)
def unzip_file(filename, destination)
Zip::File.open(filename) do |zip_file|
zip_file.each do |entry|
entry.extract(File.join(destination, entry.name)) { true }
Expand Down

0 comments on commit 546898c

Please sign in to comment.