refactoring

This commit is contained in:
olegphenomenon 2021-10-27 15:46:30 +03:00
parent e9d7b40136
commit ca231620f1
3 changed files with 36 additions and 12 deletions

View file

@ -104,7 +104,6 @@ module Api
Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes,
ident_country_code: country) ident_country_code: country)
rescue CompanyRegister::NotAvailableError rescue CompanyRegister::NotAvailableError
nil nil
end end

View file

@ -21,29 +21,54 @@ class RegistrantUser < User
return [] if ident.include?('-') return [] if ident.include?('-')
companies = company_register.representation_rights(citizen_personal_code: ident, companies = company_register.representation_rights(citizen_personal_code: ident,
citizen_country_code: country.alpha3) citizen_country_code: country.alpha3)
update_contacts_before_receive(companies)
p "++++++++++++++++++++"
log("+++++++++++++")
log(companies)
log("+++++++++++++")
p companies
p "+++++++++++++++++++++"
companies = update_contacts_before_receive(companies)
companies companies
end end
def log(msg)
@log ||= Logger.new($stdout)
@log.info(msg)
end
def update_contacts_before_receive(companies) def update_contacts_before_receive(companies)
return if companies.blank? return if companies.blank?
companies.each do |c| companies.each do |c|
contact = Contact.find_by(ident: c.registration_number, ident_country_code: "EE") # contact = Contact.find_by(ident: c.registration_number, ident_country_code: 'EE')
break if contact.blank? contacts = Contact.where(ident: c.registration_number, ident_country_code: 'EE')
unless c.company_name == contact.name break if contacts.blank?
old_contact_name = contact.name
contact.update(name: c.company_name) contacts.each do |co|
notify_registrar_data_updated(company_name: c.company_name, old_contact_name: old_contact_name, contact: contact) next if c.company_name == co.name
update_company_name(contact: co, company: c)
end end
end end
return companies
rescue CompanyRegister::NotAvailableError rescue CompanyRegister::NotAvailableError
nil nil
end 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:) def notify_registrar_data_updated(company_name:, old_contact_name:, contact:)
contact.registrar.notifications.create!( contact.registrar.notifications.create!(
text: "#{old_contact_name} was changed to #{company_name}" text: "#{old_contact_name} was changed to #{company_name}"

View file

@ -44,10 +44,10 @@ class RegistrantUserTest < ActiveSupport::TestCase
company_register = Minitest::Mock.new company_register = Minitest::Mock.new
company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234',
citizen_country_code: 'USA' }]) citizen_country_code: 'USA' }])
assert_equal [company], @user.companies(company_register) @user.companies(company_register)
company_register.verify
org.reload org.reload
assert_equal org.name, company.company_name
end end
def test_queries_company_register_for_associated_companies def test_queries_company_register_for_associated_companies