diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 0e4ca81c2..30096ab8a 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -104,7 +104,6 @@ module Api Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) - rescue CompanyRegister::NotAvailableError nil end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index a907d98e5..7a6e6dbe4 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -21,29 +21,54 @@ class RegistrantUser < User return [] if ident.include?('-') companies = company_register.representation_rights(citizen_personal_code: ident, - citizen_country_code: country.alpha3) - update_contacts_before_receive(companies) + citizen_country_code: country.alpha3) + + p "++++++++++++++++++++" + log("+++++++++++++") + log(companies) + log("+++++++++++++") + p companies + p "+++++++++++++++++++++" + companies = update_contacts_before_receive(companies) companies end + def log(msg) + @log ||= Logger.new($stdout) + @log.info(msg) + end + def update_contacts_before_receive(companies) return if companies.blank? companies.each do |c| - contact = Contact.find_by(ident: c.registration_number, ident_country_code: "EE") - break if contact.blank? + # contact = Contact.find_by(ident: c.registration_number, ident_country_code: 'EE') + contacts = Contact.where(ident: c.registration_number, ident_country_code: 'EE') - unless c.company_name == contact.name - old_contact_name = contact.name - contact.update(name: c.company_name) - notify_registrar_data_updated(company_name: c.company_name, old_contact_name: old_contact_name, contact: contact) + break if contacts.blank? + + contacts.each do |co| + next if c.company_name == co.name + + update_company_name(contact: co, company: c) 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: "#{old_contact_name} was changed to #{company_name}" diff --git a/test/models/registrant_user_test.rb b/test/models/registrant_user_test.rb index 47adcda68..987e80c03 100644 --- a/test/models/registrant_user_test.rb +++ b/test/models/registrant_user_test.rb @@ -44,10 +44,10 @@ class RegistrantUserTest < ActiveSupport::TestCase company_register = Minitest::Mock.new company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', citizen_country_code: 'USA' }]) - assert_equal [company], @user.companies(company_register) - company_register.verify - + @user.companies(company_register) org.reload + + assert_equal org.name, company.company_name end def test_queries_company_register_for_associated_companies