Registrant API: Simplify authorized contact search query

This commit is contained in:
Karl Erik Õunapuu 2021-01-21 15:39:21 +02:00
parent 1d1c336117
commit f07f03f0f9
No known key found for this signature in database
GPG key ID: C9DD647298A34764

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