diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e8560d6..08f68a169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +21.01.2021 +* Registrant API: optimised contact linking [#1807](https://github.com/internetee/registry/pull/1807) + +20.01.2021 +* Fixed legaldoc assignment issue on registrant confirmation [#1806](https://github.com/internetee/registry/pull/1806) + 14.01.2021 * Fixed IDN and punycode support for REPP domain transfer_info request [#1801](https://github.com/internetee/registry/issues/1801) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index e11458ff8..ec59ed83f 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -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 diff --git a/app/models/contact.rb b/app/models/contact.rb index e30312b4a..7ae51992d 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -360,9 +360,11 @@ class Contact < ApplicationRecord @desc end + # Limits returned objects to 11 def related_domains - a = related_domain_descriptions - a.keys.map { |d| { name: d, id: a[d][:id], roles: a[d][:roles] } } + ids = DomainContact.select(:domain_id).where(contact_id: id).limit(11).map(&:domain_id).uniq + res = Domain.where(id: ids).or(Domain.where(registrant_id: id)).select(:name, :uuid).limit(11) + res.pluck(:name, :uuid).map { |name, id| { name: name, id: id } } end def status_notes_array=(notes) diff --git a/test/integration/api/registrant/registrant_api_contacts_test.rb b/test/integration/api/registrant/registrant_api_contacts_test.rb index 191222764..f3998a2e9 100644 --- a/test/integration/api/registrant/registrant_api_contacts_test.rb +++ b/test/integration/api/registrant/registrant_api_contacts_test.rb @@ -57,6 +57,15 @@ class RegistrantApiContactsTest < ApplicationIntegrationTest assert_equal({ errors: [base: ['Not authorized']] }, json_body) end + def test_gets_contact_domain_links_when_requested + get "/api/v1/registrant/contacts/#{@contact.uuid}?links=true", headers: @auth_headers + + expected_links = @contact.domains.uniq.map { |d| { name: d.name, id: d.uuid }} + assert_response :ok + response_json = JSON.parse(response.body, symbolize_names: true) + + assert_empty expected_links - response_json[:links] + end private def auth_token