mirror of
https://github.com/internetee/registry.git
synced 2025-06-13 08:04:45 +02:00
Merge pull request #1750 from internetee/registrant-api-fetch-improvements
Registrant API: Include total domain count
This commit is contained in:
commit
0d09327493
7 changed files with 51 additions and 26 deletions
|
@ -19,15 +19,16 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
contacts = current_user_contacts.limit(limit).offset(offset)
|
contacts = current_user_contacts.limit(limit).offset(offset)
|
||||||
serialized_contacts = contacts.collect { |contact| serialize_contact(contact) }
|
serialized_contacts = contacts.collect { |contact| serialize_contact(contact, false) }
|
||||||
render json: serialized_contacts
|
render json: serialized_contacts
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
contact = current_user_contacts.find_by(uuid: params[:uuid])
|
contact = current_user_contacts.find_by(uuid: params[:uuid])
|
||||||
|
links = params[:links] == 'true'
|
||||||
|
|
||||||
if contact
|
if contact
|
||||||
render json: serialize_contact(contact)
|
render json: serialize_contact(contact, links)
|
||||||
else
|
else
|
||||||
render json: { errors: [{ base: ['Contact not found'] }] }, status: :not_found
|
render json: { errors: [{ base: ['Contact not found'] }] }, status: :not_found
|
||||||
end
|
end
|
||||||
|
@ -85,7 +86,7 @@ module Api
|
||||||
contact.registrar.notify(action)
|
contact.registrar.notify(action)
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: serialize_contact(contact)
|
render json: serialize_contact(contact, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -96,8 +97,8 @@ module Api
|
||||||
current_registrant_user.direct_contacts
|
current_registrant_user.direct_contacts
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_contact(contact)
|
def serialize_contact(contact, links)
|
||||||
Serializers::RegistrantApi::Contact.new(contact).to_json
|
Serializers::RegistrantApi::Contact.new(contact, links).to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,7 @@ module Api
|
||||||
def index
|
def index
|
||||||
limit = params[:limit] || 200
|
limit = params[:limit] || 200
|
||||||
offset = params[:offset] || 0
|
offset = params[:offset] || 0
|
||||||
|
simple = params[:simple] == 'true' || false
|
||||||
|
|
||||||
if limit.to_i > 200 || limit.to_i < 1
|
if limit.to_i > 200 || limit.to_i < 1
|
||||||
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
|
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
|
||||||
|
@ -18,21 +19,20 @@ module Api
|
||||||
status: :bad_request) && return
|
status: :bad_request) && return
|
||||||
end
|
end
|
||||||
|
|
||||||
@domains = current_user_domains.limit(limit).offset(offset)
|
domains = current_user_domains
|
||||||
|
serialized_domains = domains.limit(limit).offset(offset).map do |item|
|
||||||
serialized_domains = @domains.map do |item|
|
serializer = Serializers::RegistrantApi::Domain.new(item, simplify: simple)
|
||||||
serializer = Serializers::RegistrantApi::Domain.new(item)
|
|
||||||
serializer.to_json
|
serializer.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: serialized_domains
|
render json: { count: domains.count, domains: serialized_domains }
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@domain = current_user_domains.find_by(uuid: params[:uuid])
|
@domain = current_user_domains.find_by(uuid: params[:uuid])
|
||||||
|
|
||||||
if @domain
|
if @domain
|
||||||
serializer = Serializers::RegistrantApi::Domain.new(@domain)
|
serializer = Serializers::RegistrantApi::Domain.new(@domain, simplify: false)
|
||||||
render json: serializer.to_json
|
render json: serializer.to_json
|
||||||
else
|
else
|
||||||
render json: { errors: [{ base: ['Domain not found'] }] }, status: :not_found
|
render json: { errors: [{ base: ['Domain not found'] }] }, status: :not_found
|
||||||
|
|
|
@ -347,19 +347,24 @@ class Contact < ApplicationRecord
|
||||||
@desc = {}
|
@desc = {}
|
||||||
|
|
||||||
registrant_domains.each do |dom|
|
registrant_domains.each do |dom|
|
||||||
@desc[dom.name] ||= []
|
@desc[dom.name] ||= { id: dom.uuid, roles: [] }
|
||||||
@desc[dom.name] << :registrant
|
@desc[dom.name][:roles] << :registrant
|
||||||
end
|
end
|
||||||
|
|
||||||
domain_contacts.each do |dc|
|
domain_contacts.each do |dc|
|
||||||
@desc[dc.domain.name] ||= []
|
@desc[dc.domain.name] ||= { id: dc.domain.uuid, roles: [] }
|
||||||
@desc[dc.domain.name] << dc.name.downcase.to_sym
|
@desc[dc.domain.name][:roles] << dc.name.downcase.to_sym
|
||||||
@desc[dc.domain.name] = @desc[dc.domain.name].compact
|
@desc[dc.domain.name] = @desc[dc.domain.name].compact
|
||||||
end
|
end
|
||||||
|
|
||||||
@desc
|
@desc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def related_domains
|
||||||
|
a = related_domain_descriptions
|
||||||
|
a.keys.map { |d| { name: d, id: a[d][:id], roles: a[d][:roles] } }
|
||||||
|
end
|
||||||
|
|
||||||
def status_notes_array=(notes)
|
def status_notes_array=(notes)
|
||||||
self.status_notes = {}
|
self.status_notes = {}
|
||||||
notes ||= []
|
notes ||= []
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
module Serializers
|
module Serializers
|
||||||
module RegistrantApi
|
module RegistrantApi
|
||||||
class Contact
|
class Contact
|
||||||
attr_reader :contact
|
attr_reader :contact, :links
|
||||||
|
|
||||||
def initialize(contact)
|
def initialize(contact, links)
|
||||||
@contact = contact
|
@contact = contact
|
||||||
|
@links = links
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json
|
def to_json(_obj = nil)
|
||||||
{
|
obj = {
|
||||||
id: contact.uuid,
|
id: contact.uuid,
|
||||||
name: contact.name,
|
name: contact.name,
|
||||||
code: contact.code,
|
code: contact.code,
|
||||||
|
@ -31,6 +32,10 @@ module Serializers
|
||||||
statuses: contact.statuses,
|
statuses: contact.statuses,
|
||||||
disclosed_attributes: contact.disclosed_attributes,
|
disclosed_attributes: contact.disclosed_attributes,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj[:links] = contact.related_domains if @links
|
||||||
|
|
||||||
|
obj
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,11 +3,14 @@ module Serializers
|
||||||
class Domain
|
class Domain
|
||||||
attr_reader :domain
|
attr_reader :domain
|
||||||
|
|
||||||
def initialize(domain)
|
def initialize(domain, simplify: false)
|
||||||
@domain = domain
|
@domain = domain
|
||||||
|
@simplify = simplify
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json
|
def to_json(_obj = nil)
|
||||||
|
return simple_object if @simplify
|
||||||
|
|
||||||
{
|
{
|
||||||
id: domain.uuid,
|
id: domain.uuid,
|
||||||
name: domain.name,
|
name: domain.name,
|
||||||
|
@ -49,6 +52,17 @@ module Serializers
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def simple_object
|
||||||
|
{
|
||||||
|
id: domain.uuid, name: domain.name, registered_at: domain.registered_at,
|
||||||
|
valid_to: domain.valid_to, outzone_at: domain.outzone_at, statuses: domain.statuses,
|
||||||
|
registrant_verification_asked_at: domain.registrant_verification_asked_at,
|
||||||
|
registrar: { name: domain.registrar.name, website: domain.registrar.website },
|
||||||
|
registrant: { name: domain.registrant.name, id: domain.registrant.uuid,
|
||||||
|
phone: domain.registrant.phone, email: domain.registrant.email }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def dnssec_keys
|
def dnssec_keys
|
||||||
domain.dnskeys.map do |key|
|
domain.dnskeys.map do |key|
|
||||||
"#{key.flags} #{key.protocol} #{key.alg} #{key.public_key}"
|
"#{key.flags} #{key.protocol} #{key.alg} #{key.public_key}"
|
||||||
|
|
|
@ -50,10 +50,10 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest
|
||||||
assert_equal(200, response.status)
|
assert_equal(200, response.status)
|
||||||
|
|
||||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||||
array_of_domain_names = response_json.map { |x| x[:name] }
|
array_of_domain_names = response_json[:domains].map { |x| x[:name] }
|
||||||
assert(array_of_domain_names.include?('hospital.test'))
|
assert(array_of_domain_names.include?('hospital.test'))
|
||||||
|
|
||||||
array_of_domain_registrars = response_json.map { |x| x[:registrar] }
|
array_of_domain_registrars = response_json[:domains].map { |x| x[:registrar] }
|
||||||
assert(array_of_domain_registrars.include?({name: 'Good Names', website: nil}))
|
assert(array_of_domain_registrars.include?({name: 'Good Names', website: nil}))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -63,12 +63,12 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest
|
||||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
|
||||||
assert_equal(200, response.status)
|
assert_equal(200, response.status)
|
||||||
assert_equal(2, response_json.count)
|
assert_equal(2, response_json[:domains].count)
|
||||||
|
|
||||||
get '/api/v1/registrant/domains', headers: @auth_headers
|
get '/api/v1/registrant/domains', headers: @auth_headers
|
||||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
|
||||||
assert_equal(4, response_json.count)
|
assert_equal(4, response_json[:domains].count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_root_does_not_accept_limit_higher_than_200
|
def test_root_does_not_accept_limit_higher_than_200
|
||||||
|
|
|
@ -4,7 +4,7 @@ require 'serializers/registrant_api/contact'
|
||||||
class SerializersRegistrantApiContactTest < ActiveSupport::TestCase
|
class SerializersRegistrantApiContactTest < ActiveSupport::TestCase
|
||||||
def setup
|
def setup
|
||||||
@contact = contacts(:william)
|
@contact = contacts(:william)
|
||||||
@serializer = Serializers::RegistrantApi::Contact.new(@contact)
|
@serializer = Serializers::RegistrantApi::Contact.new(@contact, false)
|
||||||
@json = @serializer.to_json
|
@json = @serializer.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue