diff --git a/Gemfile b/Gemfile index f968fecc4..c4e916929 100644 --- a/Gemfile +++ b/Gemfile @@ -69,7 +69,7 @@ gem 'redis' gem 'sidekiq', '~> 7.0' gem 'company_register', github: 'internetee/company_register', - branch: '4-check-for-company-existence' + branch: 'issues-with-upcoming-data' gem 'domain_name' gem 'e_invoice', github: 'internetee/e_invoice', branch: :master gem 'haml', '~> 6.0' @@ -108,3 +108,6 @@ gem 'pg_query', '>= 0.9.0' # token gem 'jwt' gem 'net-ftp' + +# https://stackoverflow.com/questions/79360526/uninitialized-constant-activesupportloggerthreadsafelevellogger-nameerror +gem 'concurrent-ruby', '1.3.4' diff --git a/Gemfile.lock b/Gemfile.lock index 876ed72b7..ace0966c2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/internetee/company_register.git - revision: 6465d5c49478b9de5a5fa009cb6b8123b3956dd1 - branch: 4-check-for-company-existence + revision: 1e91fec78212d7e549a1c2362c011761a447bbcd + branch: issues-with-upcoming-data specs: company_register (0.1.0) activesupport @@ -170,7 +170,7 @@ GEM aws-eventstream (~> 1, >= 1.0.2) base64 (0.2.0) bcrypt (3.1.16) - bigdecimal (3.1.8) + bigdecimal (3.1.9) bindata (2.5.0) bootsnap (1.17.1) msgpack (~> 1.2) @@ -270,7 +270,7 @@ GEM mutex_m nkf rack (>= 2.0, < 4) - i18n (1.14.6) + i18n (1.14.7) concurrent-ruby (~> 1.0) i18n_data (0.13.0) isikukood (0.1.2) @@ -282,7 +282,7 @@ GEM thor (>= 0.14, < 2.0) jquery-ui-rails (6.0.1) railties (>= 3.2.16) - json (2.5.1) + json (2.9.1) json-jwt (1.16.6) activesupport (>= 4.2) aes_key_wrap @@ -304,12 +304,15 @@ GEM kaminari-core (= 1.2.1) kaminari-core (1.2.1) libxml-ruby (3.2.1) - logger (1.4.3) + logger (1.6.5) loofah (2.24.0) crass (~> 1.0.2) nokogiri (>= 1.12.0) - mail (2.7.1) + mail (2.8.1) mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp marcel (1.0.4) matrix (0.4.2) method_source (1.1.0) @@ -321,7 +324,7 @@ GEM rake mini_mime (1.1.5) mini_portile2 (2.8.8) - minitest (5.25.1) + minitest (5.25.4) minitest-stub_any_instance (1.0.3) monetize (1.9.4) money (~> 6.12) @@ -333,12 +336,17 @@ GEM money (~> 6.13.2) railties (>= 3.0) msgpack (1.7.2) - mutex_m (0.2.0) + mutex_m (0.3.0) net-ftp (0.3.7) net-protocol time net-http (0.6.0) uri + net-imap (0.4.18) + date + net-protocol + net-pop (0.1.2) + net-protocol net-protocol (0.1.3) timeout net-smtp (0.3.3) @@ -452,7 +460,7 @@ GEM http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 4.0) netrc (~> 0.8) - rexml (3.3.9) + rexml (3.4.0) rubyzip (2.3.2) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) @@ -534,7 +542,7 @@ GEM simpleidn warden (1.2.9) rack (>= 2.0.9) - wasabi (5.0.3) + wasabi (5.1.0) addressable faraday (>= 1.9, < 3) nokogiri (>= 1.13.9) @@ -571,6 +579,7 @@ DEPENDENCIES capybara (~> 3.40.0) coffee-rails (>= 5.0) company_register! + concurrent-ruby (= 1.3.4) countries data_migrate (~> 9.0) database_cleaner diff --git a/app/jobs/org_registrant_phone_checker_job.rb b/app/jobs/org_registrant_phone_checker_job.rb new file mode 100644 index 000000000..52d950823 --- /dev/null +++ b/app/jobs/org_registrant_phone_checker_job.rb @@ -0,0 +1,93 @@ +class OrgRegistrantPhoneCheckerJob < ApplicationJob + queue_as :default + + def perform(type: 'bulk', registrant_user_code: nil, spam_delay: 1 ) + case type + when 'bulk' + execute_bulk_checker(spam_delay) + when 'single' + execute_single_checker(registrant_user_code) + else + raise "Invalid type: #{type}. Allowed types: 'bulk', 'single'" + end + end + + def execute_bulk_checker(spam_delay) + log("Bulk checker started") + + Contact.where(ident_type: 'org', ident_country_code: 'EE').joins(:registrant_domains).each do |registrant_user| + is_phone_number_matching = check_the_registrant_phone_number(registrant_user) + + call_disclosure_action(is_phone_number_matching, registrant_user) + sleep(spam_delay) + end + + log("Bulk checker finished") + end + + def execute_single_checker(registrant_user_code) + registrant_user = RegistrantUser.find_by(code: registrant_user_code) + is_phone_number_matching = check_the_registrant_phone_number(registrant_user) + + call_disclosure_action(is_phone_number_matching, registrant_user) + end + + private + + def call_disclosure_action(is_phone_number_matching, contact) + if is_phone_number_matching + disclose_phone_number(contact) + log("Phone number disclosed for registrant user #{contact.code}. Phone number: #{contact.phone}") + elsif contact.disclosed_attributes.include?('phone') + log("Removing phone number from disclosed attributes for registrant user #{contact.code}. Phone number: #{contact.phone}") + contact.disclosed_attributes.delete('phone') + contact.save! + else + log("Phone number not disclosed for registrant user #{contact.code}. Phone number: #{contact.phone}") + end + end + + def log(message) + Rails.logger.info(message) + end + + def disclose_phone_number(contact) + contact.disclosed_attributes << 'phone' + contact.save! + end + + def company_register + @company_register ||= CompanyRegister::Client.new + end + + def check_the_registrant_phone_number(registrant_user) + phone_numbers = fetch_phone_number_from_company_register(registrant_user.ident) + + # TODO: implement phone number check + # If phone number is matching with registrant phone number, then return return true, else return false + + phone_numbers.any? do |phone_number| + puts '---- business registry phone number ----' + puts phone_number + puts '---- registrant phone number ----' + puts registrant_user.phone + puts '---- format phone number ----' + puts format_phone_number(phone_number) + puts '---- format registrant phone number ----' + puts format_phone_number(registrant_user.phone) + + format_phone_number(phone_number) == format_phone_number(registrant_user.phone) + end + end + + def format_phone_number(phone_number) + # phone number from business registry is "+3725655662" + # we need to format it to "+3725655662" + phone_number.gsub(/\D/, '') + end + + def fetch_phone_number_from_company_register(company_code) + data = company_register.company_details(registration_number: company_code.to_s) + data[0].phone_numbers + end +end