diff --git a/.gitignore b/.gitignore index 3f23f1277..1c4a85f46 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /config/application.yml /config/environments/development.rb /config/deploy.rb +/config/master.key /.idea # Do not commit one. Instead, download the latest from https://github.com/internetee/style-guide. diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 30096ab8a..8e8b46631 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -34,6 +34,17 @@ module Api end end + def do_need_update_contact + result = current_registrant_user.do_need_update_contact? + render json: { update_contacts: result[:result], counter: result[:counter] } + end + + def update_company_contacts + companies = current_registrant_user.update_company_contacts + + render json: { message: 'get it', companies: companies } + end + def update logger.debug 'Received update request' logger.debug params diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 5fe508125..80b8ecab9 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -20,16 +20,27 @@ class RegistrantUser < User def companies(company_register = CompanyRegister::Client.new) return [] if ident.include?('-') - companies = company_register.representation_rights(citizen_personal_code: ident, - citizen_country_code: country.alpha3) - - companies = update_contacts_before_receive(companies) - companies + company_register.representation_rights(citizen_personal_code: ident, + citizen_country_code: country.alpha3) rescue CompanyRegister::NotAvailableError - return [] + [] end - def update_contacts_before_receive(companies) + def do_need_update_contact? + return { result: false, counter: 0 } if companies.blank? + + counter = 0 + companies.each do |company| + counter += Contact.where(ident: company.registration_number, ident_country_code: 'EE')&. + reject { |contact| contact.name == company.company_name }.size + end + + return { result: true, counter: counter } if counter.positive? + + { result: false, counter: 0 } + end + + def update_company_contacts return [] if companies.blank? companies.each do |company| diff --git a/config/routes.rb b/config/routes.rb index 66debd4b4..79807729a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -128,7 +128,10 @@ Rails.application.routes.draw do resources :domains, only: %i[index show], param: :uuid do resource :registry_lock, only: %i[create destroy] end - resources :contacts, only: %i[index show update], param: :uuid + resources :contacts, only: %i[index show update], param: :uuid do + get 'do_need_update_contact', to: 'contacts#do_need_update_contact', as: :do_need_update_contact + post 'update_company_contacts', to: 'contacts#update_company_contacts', as: :update_company_contacts + end resources :companies, only: %i[index] end diff --git a/test/models/registrant_user_test.rb b/test/models/registrant_user_test.rb index 987e80c03..2b1d6a880 100644 --- a/test/models/registrant_user_test.rb +++ b/test/models/registrant_user_test.rb @@ -41,10 +41,8 @@ class RegistrantUserTest < ActiveSupport::TestCase company = Company.new(org.ident, "ace") - company_register = Minitest::Mock.new - company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', - citizen_country_code: 'USA' }]) - @user.companies(company_register) + Spy.on(@user, :companies).and_return([company]) + @user.update_company_contacts org.reload assert_equal org.name, company.company_name @@ -63,6 +61,39 @@ class RegistrantUserTest < ActiveSupport::TestCase company_register.verify end + def test_should_return_zero_count_of_companies + assert_equal 'US-1234', @user.registrant_ident + org = contacts(:acme_ltd) + org.ident_country_code = 'EE' + org.save(validate: false) + org.reload + + company_one = Company.new(org.ident, 'Acme Ltd') + + Spy.on(@user, :companies).and_return([company_one]) + response = @user.do_need_update_contact? + org.reload + + assert_equal response[:counter], 0 + end + + def test_should_return_count_of_contact_which_should_be_updated + assert_equal 'US-1234', @user.registrant_ident + org = contacts(:acme_ltd) + org.ident_country_code = 'EE' + org.save(validate: false) + org.reload + + company_one = Company.new(org.ident, 'ace') + company_two = Company.new(org.ident, 'acer') + + Spy.on(@user, :companies).and_return([company_one, company_two]) + response = @user.do_need_update_contact? + org.reload + + assert_equal response[:counter], 2 + end + def test_returns_contacts Contact.stub(:registrant_user_contacts, %w(john jane)) do assert_equal %w(john jane), @user.contacts