mirror of
https://github.com/internetee/registry.git
synced 2025-07-30 22:46:22 +02:00
Updated REPP API for new registrar portal
This commit is contained in:
parent
e17b21436d
commit
a5ffce290d
61 changed files with 1269 additions and 408 deletions
|
@ -3,23 +3,61 @@ module Repp
|
|||
module V1
|
||||
class ContactsController < BaseController # rubocop:disable Metrics/ClassLength
|
||||
before_action :find_contact, only: %i[show update destroy]
|
||||
skip_around_action :log_request, only: :search
|
||||
|
||||
api :get, '/repp/v1/contacts'
|
||||
desc 'Get all existing contacts'
|
||||
def index
|
||||
record_count = current_user.registrar.contacts.count
|
||||
contacts = showable_contacts(params[:details], params[:limit] || 200,
|
||||
params[:offset] || 0)
|
||||
@response = { contacts: contacts, total_number_of_records: record_count }
|
||||
render(json: @response, status: :ok)
|
||||
authorize! :check, Epp::Contact
|
||||
records = current_user.registrar.contacts.order(created_at: :desc)
|
||||
|
||||
q = records.ransack(search_params)
|
||||
q.sorts = 'created_at desc' if q.sorts.empty?
|
||||
contacts = q.result(distinct: true)
|
||||
|
||||
limited_contacts = contacts.limit(limit).offset(offset)
|
||||
.includes(:domain_contacts, :registrant_domains, :registrar)
|
||||
|
||||
render_success(data: { contacts: serialized_contacts(limited_contacts),
|
||||
count: contacts.count,
|
||||
statuses: Contact::STATUSES,
|
||||
ident_types: Contact::Ident.types })
|
||||
end
|
||||
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
api :get, '/repp/v1/contacts/search(/:id)'
|
||||
desc 'Search all existing contacts by optional id or query param'
|
||||
def search
|
||||
scope = current_user.registrar.contacts
|
||||
if params[:query]
|
||||
escaped_str = ActiveRecord::Base.connection.quote_string params[:query]
|
||||
scope = scope.where("name ilike '%#{escaped_str}%' OR code ilike '%#{escaped_str}%'
|
||||
OR ident ilike '%#{escaped_str}%'")
|
||||
elsif params[:id]
|
||||
scope = scope.where(code: params[:id])
|
||||
end
|
||||
|
||||
render_success(data: scope.limit(10)
|
||||
.map do |c|
|
||||
{ value: c.code,
|
||||
label: "#{c.code} #{c.name}",
|
||||
selected: scope.size == 1 }
|
||||
end)
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
|
||||
api :get, '/repp/v1/contacts/:contact_code'
|
||||
desc 'Get a specific contact'
|
||||
def show
|
||||
serializer = ::Serializers::Repp::Contact.new(@contact,
|
||||
show_address: Contact.address_processing?)
|
||||
render_success(data: serializer.to_json)
|
||||
authorize! :check, Epp::Contact
|
||||
|
||||
simple = params[:simple] == 'true' || false
|
||||
serializer = Serializers::Repp::Contact.new(@contact,
|
||||
show_address: Contact.address_processing?,
|
||||
domain_params: domain_filter_params,
|
||||
simplify: simple)
|
||||
|
||||
render_success(data: { contact: serializer.to_json })
|
||||
end
|
||||
|
||||
api :get, '/repp/v1/contacts/check/:contact_code'
|
||||
|
@ -35,7 +73,7 @@ module Repp
|
|||
desc 'Create a new contact'
|
||||
def create
|
||||
@contact = Epp::Contact.new(contact_params_with_address, current_user.registrar, epp: false)
|
||||
action = Actions::ContactCreate.new(@contact, params[:legal_document],
|
||||
action = Actions::ContactCreate.new(@contact, contact_params[:legal_document],
|
||||
contact_ident_params)
|
||||
|
||||
unless action.call
|
||||
|
@ -50,7 +88,7 @@ module Repp
|
|||
desc 'Update existing contact'
|
||||
def update
|
||||
action = Actions::ContactUpdate.new(@contact, contact_params_with_address(required: false),
|
||||
params[:legal_document],
|
||||
contact_params[:legal_document],
|
||||
contact_ident_params(required: false), current_user)
|
||||
|
||||
unless action.call
|
||||
|
@ -73,29 +111,71 @@ module Repp
|
|||
render_success
|
||||
end
|
||||
|
||||
def contact_addr_present?
|
||||
return false unless contact_addr_params.key?(:addr)
|
||||
private
|
||||
|
||||
contact_addr_params[:addr].keys.any?
|
||||
def index_params
|
||||
params.permit(:id, :limit, :offset, :details, :q, :simple,
|
||||
:page, :per_page, :domain_filter,
|
||||
domain_filter: [],
|
||||
q: %i[s name_matches code_eq ident_matches ident_type_eq
|
||||
email_matches country_code_eq types_contains_array
|
||||
updated_at_gteq created_at_gteq created_at_lteq
|
||||
statuses_contains_array] + [s: []])
|
||||
end
|
||||
|
||||
def search_params
|
||||
index_params.fetch(:q, {})
|
||||
end
|
||||
|
||||
def domain_filter_params
|
||||
filter_params = index_params.slice(:id, :page, :per_page, :domain_filter).to_h
|
||||
filter_params.merge!({ sort: hashify(index_params[:q].fetch(:s)) }) if index_params[:q]
|
||||
filter_params
|
||||
end
|
||||
|
||||
def hashify(sort)
|
||||
return unless sort
|
||||
|
||||
sort_hash = {}
|
||||
if sort.is_a?(Array)
|
||||
sort.each do |s|
|
||||
sort_hash.merge!(Hash[*s.split(' ')])
|
||||
end
|
||||
else
|
||||
sort_hash.merge!(Hash[*sort.split(' ')])
|
||||
end
|
||||
sort_hash
|
||||
end
|
||||
|
||||
def limit
|
||||
index_params[:limit] || 200
|
||||
end
|
||||
|
||||
def offset
|
||||
index_params[:offset] || 0
|
||||
end
|
||||
|
||||
def serialized_contacts(contacts)
|
||||
return contacts.map {|c| c.code } unless index_params[:details] == 'true'
|
||||
|
||||
address_processing = Contact.address_processing?
|
||||
contacts.map do |c|
|
||||
Serializers::Repp::Contact.new(c, show_address: address_processing).to_json
|
||||
end
|
||||
end
|
||||
|
||||
def contact_addr_present?
|
||||
return false unless contact_addr_params
|
||||
|
||||
contact_addr_params.keys.any?
|
||||
end
|
||||
|
||||
def create_update_success_body
|
||||
{ code: opt_addr? ? 1100 : nil, data: { contact: { id: @contact.code } },
|
||||
{ code: opt_addr? ? 1100 : nil,
|
||||
data: { contact: { code: @contact.code } },
|
||||
message: opt_addr? ? I18n.t('epp.contacts.completed_without_address') : nil }
|
||||
end
|
||||
|
||||
def showable_contacts(details, limit, offset)
|
||||
contacts = current_user.registrar.contacts.limit(limit).offset(offset)
|
||||
|
||||
return contacts.pluck(:code) unless details
|
||||
|
||||
contacts.map do |contact|
|
||||
serializer = ::Serializers::Repp::Contact.new(contact,
|
||||
show_address: Contact.address_processing?)
|
||||
serializer.to_json
|
||||
end
|
||||
end
|
||||
|
||||
def opt_addr?
|
||||
!Contact.address_processing? && contact_addr_present?
|
||||
end
|
||||
|
@ -106,36 +186,36 @@ module Repp
|
|||
end
|
||||
|
||||
def contact_params_with_address(required: true)
|
||||
return contact_create_params(required: required) unless contact_addr_params.key?(:addr)
|
||||
return contact_create_params(required: required) unless contact_addr_present?
|
||||
|
||||
addr = {}
|
||||
contact_addr_params[:addr].each_key { |k| addr[k] = contact_addr_params[:addr][k] }
|
||||
contact_create_params(required: required).merge(addr)
|
||||
contact_create_params(required: required).merge(contact_addr_params)
|
||||
end
|
||||
|
||||
def contact_create_params(required: true)
|
||||
params.require(:contact).require(%i[name email phone]) if required
|
||||
params.require(:contact).permit(:name, :email, :phone, :id)
|
||||
create_params = %i[name email phone]
|
||||
contact_params.require(create_params) if required
|
||||
contact_params.slice(*create_params)
|
||||
end
|
||||
|
||||
def contact_ident_params(required: true)
|
||||
if required
|
||||
params.require(:contact).require(:ident).require(%i[ident ident_type ident_country_code])
|
||||
params.require(:contact).require(:ident).permit(:ident, :ident_type, :ident_country_code)
|
||||
else
|
||||
params.permit(contact: { ident: %i[ident ident_type ident_country_code] })
|
||||
end
|
||||
|
||||
params[:contact][:ident]
|
||||
ident_params = %i[ident ident_type ident_country_code]
|
||||
contact_params.require(:ident).require(ident_params) if required
|
||||
contact_params[:ident].to_h
|
||||
end
|
||||
|
||||
def contact_addr_params
|
||||
if Contact.address_processing?
|
||||
params.require(:contact).require(:addr).require(%i[country_code city street zip])
|
||||
params.require(:contact).require(:addr).permit(:country_code, :city, :street, :zip)
|
||||
else
|
||||
params.require(:contact).permit(addr: %i[country_code city street zip])
|
||||
end
|
||||
return contact_params[:addr] unless Contact.address_processing?
|
||||
|
||||
addr_params = %i[country_code city street zip]
|
||||
contact_params.require(:addr).require(addr_params)
|
||||
contact_params[:addr]
|
||||
end
|
||||
|
||||
def contact_params
|
||||
params.require(:contact).permit(:name, :email, :phone, :legal_document,
|
||||
legal_document: %i[body type],
|
||||
ident: [%i[ident ident_type ident_country_code]],
|
||||
addr: [%i[country_code city street zip state]])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue