added tests

This commit is contained in:
olegphenomenon 2021-10-27 15:27:41 +03:00
parent b0ed6167f5
commit e9d7b40136
3 changed files with 51 additions and 19 deletions

View file

@ -102,7 +102,6 @@ module Api
ident_type: 'priv', ident_country_code: country)
return contact if contact
update_contacts_before_receive
Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes,
ident_country_code: country)
@ -110,21 +109,6 @@ module Api
nil
end
def update_contacts_before_receive
company_register = CompanyRegister::Client.new
companies = company_register.representation_rights(citizen_personal_code: current_registrant_user.ident,
citizen_country_code: current_registrant_user.country.alpha3)
companies.each do |c|
contact = Contact.find_by(ident: c.registration_number)
break if contact.blank?
contact.update(name: c.company_name) unless c.company_name == contact.name
end
rescue CompanyRegister::NotAvailableError
nil
end
def company_codes
current_registrant_user.companies.collect(&:registration_number)
end

View file

@ -20,8 +20,34 @@ class RegistrantUser < User
def companies(company_register = CompanyRegister::Client.new)
return [] if ident.include?('-')
company_register.representation_rights(citizen_personal_code: ident,
companies = company_register.representation_rights(citizen_personal_code: ident,
citizen_country_code: country.alpha3)
update_contacts_before_receive(companies)
companies
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?
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)
end
end
rescue CompanyRegister::NotAvailableError
nil
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}"
)
end
def contacts(representable: true)