diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index f7e85c5af..74a8ad0e5 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -20,8 +20,48 @@ class RegistrantUser < User def companies(company_register = CompanyRegister::Client.new) return [] if ident.include?('-') - company_register.representation_rights(citizen_personal_code: ident, - citizen_country_code: country.alpha3) + companies = company_register.representation_rights(citizen_personal_code: ident, + citizen_country_code: country.alpha3) + + companies = update_contacts_before_receive(companies) + companies + end + + def update_contacts_before_receive(companies) + return if companies.blank? + + companies.each do |company| + contacts = Contact.where(ident: company.registration_number, ident_country_code: 'EE') + + next if contacts.blank? + + contacts.each do |contact| + next if company.company_name == contact.name + + update_company_name(contact: contact, company: company) + end + end + + return companies + rescue CompanyRegister::NotAvailableError + nil + end + + def update_company_name(contact:, company:) + old_contact_name = contact.name + contact.name = company.company_name + + contact.save(validate: false) + + notify_registrar_data_updated(company_name: company.company_name, + old_contact_name: old_contact_name, + contact: contact) + end + + def notify_registrar_data_updated(company_name:, old_contact_name:, contact:) + contact.registrar.notifications.create!( + text: "Contact update: #{contact.id} name updated from #{old_contact_name} to #{company_name} by the registry" + ) end def contacts(representable: true) diff --git a/test/models/registrant_user_test.rb b/test/models/registrant_user_test.rb index c61f095f2..987e80c03 100644 --- a/test/models/registrant_user_test.rb +++ b/test/models/registrant_user_test.rb @@ -1,6 +1,8 @@ require 'test_helper' class RegistrantUserTest < ActiveSupport::TestCase + Company = Struct.new(:registration_number, :company_name) + def setup super @@ -30,14 +32,34 @@ class RegistrantUserTest < ActiveSupport::TestCase assert_equal Country.new('US'), user.country end + def test_should_update_org_contact_if_data_from_business_registry_dismatch + assert_equal 'US-1234', @user.registrant_ident + org = contacts(:acme_ltd) + org.ident_country_code = 'EE' + org.save(validate: false) + org.reload + + 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) + org.reload + + assert_equal org.name, company.company_name + end + def test_queries_company_register_for_associated_companies assert_equal 'US-1234', @user.registrant_ident + company = Company.new("acme", "ace") + company_register = Minitest::Mock.new - company_register.expect(:representation_rights, %w[acme ace], [{ citizen_personal_code: '1234', + company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', citizen_country_code: 'USA' }]) - assert_equal %w[acme ace], @user.companies(company_register) + assert_equal [company], @user.companies(company_register) company_register.verify end