11require 'zip'
2+ require 'csv'
23
34class 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 )
0 commit comments