Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ gem 'omniauth-tara', github: 'internetee/omniauth-tara'
# gem 'omniauth-tara', path: 'vendor/gems/omniauth-tara'

gem 'airbrake'
gem 'company_register', github: 'internetee/company_register', branch: :master
gem 'company_register', github: 'internetee/company_register', branch: 'master'
gem 'directo', github: 'internetee/directo', branch: 'master'
gem 'domain_name'
gem 'dry-struct'
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/internetee/company_register.git
revision: 2c31da54c57db13324161eeb8db7e9f81af81987
revision: d83597c5cc06c1909637e56d937a5b3bcb6286c6
branch: master
specs:
company_register (0.1.0)
Expand Down
73 changes: 55 additions & 18 deletions app/jobs/company_register_status_job.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
require 'zip'
require 'csv'

class CompanyRegisterStatusJob < ApplicationJob
PAYMENT_STATEMENT_BUSINESS_REGISTRY_REASON = 'Kustutamiskanne dokumentide hoidjata'

CHECK_INTERVAL_REGISTERED = 1.year
CHECK_INTERVAL_LIQUIDATED_BANKRUPT = 1.month
CHECK_INTERVAL_DELETED = 1.day

REGISTRY_STATUSES = {
Contact::REGISTERED => 'registered',
Contact::LIQUIDATED => 'liquidated',
Contact::BANKRUPT => 'bankrupt',
Contact::DELETED => 'deleted'
}
Contact::DELETED => 'deleted',
Contact::NOT_FOUND => 'not_found'
}.freeze

queue_as :default

def perform(days_interval = 14, spam_time_delay = 1, batch_size = 100)
sampling_registrant_contact(days_interval).find_in_batches(batch_size: batch_size) do |contacts|
def perform(spam_time_delay = 1, batch_size = 100)
sampling_registrant_contacts.find_in_batches(batch_size: batch_size) do |contacts|
contacts_to_check = contacts.reject { |contact| whitelisted_company?(contact) }

contacts_to_check.each do |contact|
Expand All @@ -29,46 +35,69 @@ def proceed_company_status(contact, spam_time_delay)
sleep spam_time_delay

company_status = contact.return_company_status
Rails.logger.info "Checking contact with id #{contact.id} and its ident: #{contact.ident}. His status from business registry is #{company_status}"

if company_status.blank?
handle_missing_company(contact)
return
end

handle_company_statuses(contact, company_status)
status = company_status.blank? ? Contact::DELETED : company_status
update_validation_company_status(contact: contact, status: company_status)
rescue CompanyRegister::SOAPFaultError => e
Rails.logger.error("SOAPFaultError for contact #{contact.id} (#{contact.ident}): #{e.message}. Skipping contact.")
end


update_validation_company_status(contact:contact , status: status)
def handle_missing_company(contact)
Rails.logger.info("Contact #{contact.id} (#{contact.ident}) not found in business registry. Scheduling force delete.")
schedule_force_delete(contact, nil, nil)
update_validation_company_status(contact: contact, status: Contact::NOT_FOUND)
end

def handle_company_statuses(contact, company_status)
return if company_status == Contact::BANKRUPT

case company_status
when Contact::REGISTERED
lift_force_delete(contact) if check_for_force_delete(contact)
when Contact::LIQUIDATED
send_email_for_liquidation(contact)
else
when Contact::BANKRUPT
# Bankrupt companies are tracked but no action needed
Rails.logger.info("Contact #{contact.id} is bankrupt. No action required.")
when Contact::DELETED
delete_process(contact, company_status)
end
end

def send_email_for_liquidation(contact)
return if contact.company_register_status == Contact::LIQUIDATED

Rails.logger.info("Sending email for liquidation for contact #{contact.id}")
ContactInformMailer.company_liquidation(contact: contact).deliver_now
end

def sampling_registrant_contact(days_interval)
def sampling_registrant_contacts
Contact.joins(:registrant_domains)
.where(ident_type: 'org', ident_country_code: 'EE')
.where(
"(company_register_status IS NULL OR checked_company_at IS NULL) OR
(company_register_status = ? AND checked_company_at < ?) OR
company_register_status IN (?)",
Contact::REGISTERED, days_interval.days.ago, [Contact::LIQUIDATED, Contact::BANKRUPT, Contact::DELETED]
)
.where(sampling_conditions)
.distinct
end

def sampling_conditions
cutoff_registered = CHECK_INTERVAL_REGISTERED.ago.to_date + 1.day
cutoff_liquidated_bankrupt = CHECK_INTERVAL_LIQUIDATED_BANKRUPT.ago.to_date + 1.day
cutoff_deleted = CHECK_INTERVAL_DELETED.ago.to_date + 1.day

<<-SQL.squish
(company_register_status IS NULL OR checked_company_at IS NULL)
OR (company_register_status = '#{Contact::REGISTERED}' AND checked_company_at < '#{cutoff_registered}')
OR (company_register_status IN ('#{Contact::LIQUIDATED}', '#{Contact::BANKRUPT}') AND checked_company_at < '#{cutoff_liquidated_bankrupt}')
OR (company_register_status = '#{Contact::DELETED}' AND checked_company_at < '#{cutoff_deleted}')
SQL
end

def update_validation_company_status(contact:, status:)
contact.update(company_register_status: status, checked_company_at: Time.zone.now)
contact.update(company_register_status: status, checked_company_at: Date.current)
end

def schedule_force_delete(contact, company_status, kandeliik_tekstina)
Expand Down Expand Up @@ -99,6 +128,7 @@ def check_for_force_delete(contact)
end

def lift_force_delete(contact)
Rails.logger.info("Lifting force delete for contact #{contact.id}")
contact.registrant_domains.each do |domain|
next unless domain.force_delete_scheduled?

Expand All @@ -125,6 +155,8 @@ def delete_process(contact, company_status)
else
schedule_force_delete(contact, company_status, kandeliik_tekstina)
end
rescue CompanyRegister::SOAPFaultError => e
Rails.logger.error("Error getting company details for #{contact.ident}: #{e.message}")
end

private
Expand Down Expand Up @@ -159,7 +191,12 @@ def whitelisted_companies
end

def whitelisted_company?(contact)
whitelisted_companies.include?(contact.ident)
if whitelisted_companies.include?(contact.ident)
Rails.logger.info("Contact #{contact.id} (#{contact.ident}) is whitelisted. Skipping company status check.")
return true
end

false
end

def company_status_notes(company_status)
Expand Down
2 changes: 1 addition & 1 deletion app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def modifies_address?

def update_related_whois_records
# not doing anything if no real changes
ignored_columns = %w[updated_at created_at statuses status_notes]
ignored_columns = %w[updated_at created_at statuses status_notes checked_company_at company_register_status]

return if (previous_changes.keys - ignored_columns).empty?

Expand Down
9 changes: 8 additions & 1 deletion app/models/contact/company_register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Contact::CompanyRegister
LIQUIDATED = 'L'.freeze
BANKRUPT = 'N'.freeze
DELETED = 'K'.freeze
NOT_FOUND = 'X'.freeze

def return_company_status
return if return_company_data.blank?
Expand All @@ -18,12 +19,18 @@ def return_company_data
company_register.simple_data(registration_number: ident.to_s)
rescue CompanyRegister::NotAvailableError
[]
rescue CompanyRegister::SOAPFaultError => e
Rails.logger.error("SOAP Fault getting company data for #{ident}: #{e.message}")
raise e
end

def return_company_details
return unless org?

company_register.company_details(registration_number: ident.to_s)
rescue CompanyRegister::SOAPFaultError => e
Rails.logger.error("SOAP Fault getting company details for #{ident}: #{e.message}")
raise e
rescue CompanyRegister::NotAvailableError
[]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/company_status.rake
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ namespace :company_status do
if resp.empty?
put_company_to_missing_file(contact: contact, path: missing_companies_in_business_registry_path)
puts "Company: #{contact.name} with ident: #{contact.ident} and ID: #{contact.id} is missing in registry, company id: #{contact.id}"
soft_delete_company(contact) if soft_delete_enable
# soft_delete_company(contact) if soft_delete_enable
else
status = resp.first.status.upcase
kandeliik_type = resp.first.kandeliik.last.last.kandeliik
Expand Down
Loading
Loading