REPP Domains: Allow create/read/update for domain-scoped contacts

This commit is contained in:
Karl Erik Õunapuu 2021-01-18 17:23:31 +02:00
parent 9dcd40ee3b
commit fda58e9a41
No known key found for this signature in database
GPG key ID: C9DD647298A34764
3 changed files with 62 additions and 1 deletions

View file

@ -4,6 +4,53 @@ module Repp
class ContactsController < BaseController
before_action :set_current_contact, only: [:update]
before_action :set_new_contact, only: [:update]
before_action :set_domain, only: %i[index create destroy]
api :GET, '/repp/v1/domains/:domain_name/contacts'
desc "View domain's admin and tech contacts"
def index
admin_contacts = @domain.admin_domain_contacts.pluck(:contact_code_cache)
tech_contacts = @domain.tech_domain_contacts.pluck(:contact_code_cache)
data = { admin_contacts: admin_contacts, tech_contacts: tech_contacts }
render_success(data: data)
end
api :POST, '/repp/v1/domains/:domain_name/contacts'
desc "Link new contact(s) to domain"
param :contacts, Array, required: true, desc: 'Array of new linked contacts' do
param :code, String, required: true, desc: 'Contact code'
param :type, String, required: true, desc: 'Role of contact (admin/tech)'
end
def create
contact_create_params[:contacts].each { |c| c[:action] = 'add' }
action = Actions::DomainUpdate.new(@domain, contact_create_params, current_user)
unless action.call
handle_errors(@domain)
return
end
render_success(data: { domain: { name: @domain.name } })
end
api :DELETE, '/repp/v1/domains/:domain_name/contacts'
desc "Remove contact(s) from domain"
param :contacts, Array, required: true, desc: 'Array of new linked contacts' do
param :code, String, required: true, desc: 'Contact code'
param :type, String, required: true, desc: 'Role of contact (admin/tech)'
end
def destroy
contact_create_params[:contacts].each { |c| c[:action] = 'rem' }
action = Actions::DomainUpdate.new(@domain, contact_create_params, current_user)
unless action.call
handle_errors(@domain)
return
end
render_success(data: { domain: { name: @domain.name } })
end
def set_current_contact
@current_contact = current_user.registrar.contacts.find_by!(
@ -32,6 +79,18 @@ module Repp
private
def set_domain
registrar = current_user.registrar
@domain = Epp::Domain.find_by(registrar: registrar, name: params[:domain_id])
@domain ||= Epp::Domain.find_by!(registrar: registrar, name_puny: params[:domain_id])
@domain
end
def contact_create_params
params.permit!
end
def contact_params
params.require(%i[current_contact_id new_contact_id])
params.permit(:current_contact_id, :new_contact_id)