Skip to content

Commit 47a8ff0

Browse files
authored
Merge pull request #2883 from internetee/soap-fault-exception-handler-from-business-registry
added SOAPFaule exception handler from company registry
2 parents 7ca073c + 99f6968 commit 47a8ff0

7 files changed

Lines changed: 516 additions & 108 deletions

File tree

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ gem 'omniauth-tara', github: 'internetee/omniauth-tara'
6262
# gem 'omniauth-tara', path: 'vendor/gems/omniauth-tara'
6363

6464
gem 'airbrake'
65-
gem 'company_register', github: 'internetee/company_register', branch: :master
65+
gem 'company_register', github: 'internetee/company_register', branch: 'master'
6666
gem 'directo', github: 'internetee/directo', branch: 'master'
6767
gem 'domain_name'
6868
gem 'dry-struct'

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GIT
22
remote: https://github.com/internetee/company_register.git
3-
revision: 2c31da54c57db13324161eeb8db7e9f81af81987
3+
revision: d83597c5cc06c1909637e56d937a5b3bcb6286c6
44
branch: master
55
specs:
66
company_register (0.1.0)

app/jobs/company_register_status_job.rb

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
require 'zip'
2+
require 'csv'
23

34
class CompanyRegisterStatusJob < ApplicationJob
45
PAYMENT_STATEMENT_BUSINESS_REGISTRY_REASON = 'Kustutamiskanne dokumentide hoidjata'
56

7+
CHECK_INTERVAL_REGISTERED = 1.year
8+
CHECK_INTERVAL_LIQUIDATED_BANKRUPT = 1.month
9+
CHECK_INTERVAL_DELETED = 1.day
10+
611
REGISTRY_STATUSES = {
712
Contact::REGISTERED => 'registered',
813
Contact::LIQUIDATED => 'liquidated',
914
Contact::BANKRUPT => 'bankrupt',
10-
Contact::DELETED => 'deleted'
11-
}
15+
Contact::DELETED => 'deleted',
16+
Contact::NOT_FOUND => 'not_found'
17+
}.freeze
1218

1319
queue_as :default
1420

15-
def perform(days_interval = 14, spam_time_delay = 1, batch_size = 100)
16-
sampling_registrant_contact(days_interval).find_in_batches(batch_size: batch_size) do |contacts|
21+
def perform(spam_time_delay = 1, batch_size = 100)
22+
sampling_registrant_contacts.find_in_batches(batch_size: batch_size) do |contacts|
1723
contacts_to_check = contacts.reject { |contact| whitelisted_company?(contact) }
1824

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

3137
company_status = contact.return_company_status
38+
Rails.logger.info "Checking contact with id #{contact.id} and its ident: #{contact.ident}. His status from business registry is #{company_status}"
39+
40+
if company_status.blank?
41+
handle_missing_company(contact)
42+
return
43+
end
3244

3345
handle_company_statuses(contact, company_status)
34-
status = company_status.blank? ? Contact::DELETED : company_status
46+
update_validation_company_status(contact: contact, status: company_status)
47+
rescue CompanyRegister::SOAPFaultError => e
48+
Rails.logger.error("SOAPFaultError for contact #{contact.id} (#{contact.ident}): #{e.message}. Skipping contact.")
49+
end
50+
3551

36-
update_validation_company_status(contact:contact , status: status)
52+
def handle_missing_company(contact)
53+
Rails.logger.info("Contact #{contact.id} (#{contact.ident}) not found in business registry. Scheduling force delete.")
54+
schedule_force_delete(contact, nil, nil)
55+
update_validation_company_status(contact: contact, status: Contact::NOT_FOUND)
3756
end
3857

3958
def handle_company_statuses(contact, company_status)
40-
return if company_status == Contact::BANKRUPT
41-
4259
case company_status
4360
when Contact::REGISTERED
4461
lift_force_delete(contact) if check_for_force_delete(contact)
4562
when Contact::LIQUIDATED
4663
send_email_for_liquidation(contact)
47-
else
64+
when Contact::BANKRUPT
65+
# Bankrupt companies are tracked but no action needed
66+
Rails.logger.info("Contact #{contact.id} is bankrupt. No action required.")
67+
when Contact::DELETED
4868
delete_process(contact, company_status)
4969
end
5070
end
5171

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

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

58-
def sampling_registrant_contact(days_interval)
79+
def sampling_registrant_contacts
5980
Contact.joins(:registrant_domains)
6081
.where(ident_type: 'org', ident_country_code: 'EE')
61-
.where(
62-
"(company_register_status IS NULL OR checked_company_at IS NULL) OR
63-
(company_register_status = ? AND checked_company_at < ?) OR
64-
company_register_status IN (?)",
65-
Contact::REGISTERED, days_interval.days.ago, [Contact::LIQUIDATED, Contact::BANKRUPT, Contact::DELETED]
66-
)
82+
.where(sampling_conditions)
6783
.distinct
6884
end
6985

86+
def sampling_conditions
87+
cutoff_registered = CHECK_INTERVAL_REGISTERED.ago.to_date + 1.day
88+
cutoff_liquidated_bankrupt = CHECK_INTERVAL_LIQUIDATED_BANKRUPT.ago.to_date + 1.day
89+
cutoff_deleted = CHECK_INTERVAL_DELETED.ago.to_date + 1.day
90+
91+
<<-SQL.squish
92+
(company_register_status IS NULL OR checked_company_at IS NULL)
93+
OR (company_register_status = '#{Contact::REGISTERED}' AND checked_company_at < '#{cutoff_registered}')
94+
OR (company_register_status IN ('#{Contact::LIQUIDATED}', '#{Contact::BANKRUPT}') AND checked_company_at < '#{cutoff_liquidated_bankrupt}')
95+
OR (company_register_status = '#{Contact::DELETED}' AND checked_company_at < '#{cutoff_deleted}')
96+
SQL
97+
end
98+
7099
def update_validation_company_status(contact:, status:)
71-
contact.update(company_register_status: status, checked_company_at: Time.zone.now)
100+
contact.update(company_register_status: status, checked_company_at: Date.current)
72101
end
73102

74103
def schedule_force_delete(contact, company_status, kandeliik_tekstina)
@@ -99,6 +128,7 @@ def check_for_force_delete(contact)
99128
end
100129

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

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

130162
private
@@ -159,7 +191,12 @@ def whitelisted_companies
159191
end
160192

161193
def whitelisted_company?(contact)
162-
whitelisted_companies.include?(contact.ident)
194+
if whitelisted_companies.include?(contact.ident)
195+
Rails.logger.info("Contact #{contact.id} (#{contact.ident}) is whitelisted. Skipping company status check.")
196+
return true
197+
end
198+
199+
false
163200
end
164201

165202
def company_status_notes(company_status)

app/models/contact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def modifies_address?
540540

541541
def update_related_whois_records
542542
# not doing anything if no real changes
543-
ignored_columns = %w[updated_at created_at statuses status_notes]
543+
ignored_columns = %w[updated_at created_at statuses status_notes checked_company_at company_register_status]
544544

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

app/models/contact/company_register.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Contact::CompanyRegister
55
LIQUIDATED = 'L'.freeze
66
BANKRUPT = 'N'.freeze
77
DELETED = 'K'.freeze
8+
NOT_FOUND = 'X'.freeze
89

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

2327
def return_company_details
2428
return unless org?
25-
29+
2630
company_register.company_details(registration_number: ident.to_s)
31+
rescue CompanyRegister::SOAPFaultError => e
32+
Rails.logger.error("SOAP Fault getting company details for #{ident}: #{e.message}")
33+
raise e
2734
rescue CompanyRegister::NotAvailableError
2835
[]
2936
end

lib/tasks/company_status.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ namespace :company_status do
168168
if resp.empty?
169169
put_company_to_missing_file(contact: contact, path: missing_companies_in_business_registry_path)
170170
puts "Company: #{contact.name} with ident: #{contact.ident} and ID: #{contact.id} is missing in registry, company id: #{contact.id}"
171-
soft_delete_company(contact) if soft_delete_enable
171+
# soft_delete_company(contact) if soft_delete_enable
172172
else
173173
status = resp.first.status.upcase
174174
kandeliik_type = resp.first.kandeliik.last.last.kandeliik

0 commit comments

Comments
 (0)