Merge pull request #2309 from internetee/confirmation-dialog-box-for-update-companies-contacts

business registry contacts - endpoints for send status of contacts and receive confirmation
This commit is contained in:
Timo Võhmar 2022-03-25 13:48:20 +02:00 committed by GitHub
commit 9519c573a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 12 deletions

1
.gitignore vendored
View file

@ -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.

View file

@ -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

View file

@ -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|

View file

@ -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

View file

@ -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