Skip to content

Commit

Permalink
Registrant API: Return associated domains for contact query
Browse files Browse the repository at this point in the history
  • Loading branch information
karlerikounapuu committed Dec 4, 2020
1 parent 15e090c commit 15d2ffe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
11 changes: 6 additions & 5 deletions app/controllers/api/v1/registrant/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ def index
end

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
end

def show
contact = current_user_contacts.find_by(uuid: params[:uuid])
links = params[:links] == 'true'

if contact
render json: serialize_contact(contact)
render json: serialize_contact(contact, links)
else
render json: { errors: [{ base: ['Contact not found'] }] }, status: :not_found
end
Expand Down Expand Up @@ -85,7 +86,7 @@ def update
contact.registrar.notify(action)
end

render json: serialize_contact(contact)
render json: serialize_contact(contact, false)
end

private
Expand All @@ -96,8 +97,8 @@ def current_user_contacts
current_registrant_user.direct_contacts
end

def serialize_contact(contact)
Serializers::RegistrantApi::Contact.new(contact).to_json
def serialize_contact(contact, links)
Serializers::RegistrantApi::Contact.new(contact, links).to_json
end
end
end
Expand Down
13 changes: 9 additions & 4 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,24 @@ def related_domain_descriptions
@desc = {}

registrant_domains.each do |dom|
@desc[dom.name] ||= []
@desc[dom.name] << :registrant
@desc[dom.name] ||= { id: dom.uuid, roles: [] }
@desc[dom.name][:roles] << :registrant
end

domain_contacts.each do |dc|
@desc[dc.domain.name] ||= []
@desc[dc.domain.name] << dc.name.downcase.to_sym
@desc[dc.domain.name] ||= { id: dc.domain.uuid, roles: [] }
@desc[dc.domain.name][:roles] << dc.name.downcase.to_sym
@desc[dc.domain.name] = @desc[dc.domain.name].compact
end

@desc
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)
self.status_notes = {}
notes ||= []
Expand Down
13 changes: 9 additions & 4 deletions lib/serializers/registrant_api/contact.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Serializers
module RegistrantApi
class Contact
attr_reader :contact
attr_reader :contact, :links

def initialize(contact)
def initialize(contact, links)
@contact = contact
@links = links
end

def to_json
{
def to_json(_obj = nil)
obj = {
id: contact.uuid,
name: contact.name,
code: contact.code,
Expand All @@ -31,6 +32,10 @@ def to_json
statuses: contact.statuses,
disclosed_attributes: contact.disclosed_attributes,
}

obj[:links] = contact.related_domains if @links

obj
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/serializers/registrant_api/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def simple_object
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 }
registrant: { name: domain.registrant.name, id: domain.registrant.uuid,
phone: domain.registrant.phone, email: domain.registrant.email }
}
end

Expand Down

0 comments on commit 15d2ffe

Please sign in to comment.