Merge pull request #1807 from internetee/remove-registrant-api-contact-links

Registrant API: Optimize contact link(s) query
This commit is contained in:
Timo Võhmar 2021-01-21 21:10:04 +02:00 committed by GitHub
commit ae6e1d7ff1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 3 deletions

View file

@ -24,7 +24,7 @@ module Api
end
def show
contact = current_user_contacts.find_by(uuid: params[:uuid])
contact = representable_contact(params[:uuid])
links = params[:links] == 'true'
if contact
@ -91,6 +91,22 @@ module Api
private
def representable_contact(uuid)
country = current_registrant_user.country.alpha2
contact = Contact.find_by(uuid: uuid, ident: current_registrant_user.ident,
ident_type: 'priv', ident_country_code: country)
return contact if contact
Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes,
ident_country_code: country)
rescue CompanyRegister::NotAvailableError
nil
end
def company_codes
current_registrant_user.companies.collect(&:registration_number)
end
def current_user_contacts
current_registrant_user.contacts(representable: false)
rescue CompanyRegister::NotAvailableError