Merge remote-tracking branch 'origin/master' into 269-dispute-list

This commit is contained in:
Karl Erik Õunapuu 2020-05-21 17:43:57 +03:00
commit 7ef15a5bf1
15 changed files with 448 additions and 36 deletions

View file

@ -3,9 +3,10 @@ class Registrant::ContactsController < RegistrantController
helper_method :fax_enabled?
helper_method :domain_filter_params
skip_authorization_check only: %i[edit update]
before_action :set_contact, only: [:show]
def show
@contact = current_user_contacts.find(params[:id])
@requester_contact = Contact.find_by(ident: current_registrant_user.ident).id
authorize! :read, @contact
end
@ -30,6 +31,13 @@ class Registrant::ContactsController < RegistrantController
private
def set_contact
id = params[:id]
contact = domain.contacts.find_by(id: id) || current_user_contacts.find_by(id: id)
contact ||= Contact.find_by(id: id, ident: domain.registrant.ident)
@contact = contact
end
def domain
current_user_domains.find(params[:domain_id])
end

View file

@ -0,0 +1,23 @@
module Repp
module V1
class AuctionsController < ActionController::API
def index
auctions = Auction.started
render json: { count: auctions.count,
auctions: auctions_to_json(auctions) }
end
private
def auctions_to_json(auctions)
auctions.map do |e|
{
domain_name: e.domain,
punycode_domain_name: SimpleIDN.to_ascii(e.domain),
}
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Repp
module V1
class RetainedDomainsController < ActionController::API
def index
domains = RetainedDomains.new(query_params)
render json: { count: domains.count, domains: domains.to_jsonable }
end
def query_params
params.permit(:type)
end
end
end
end

View file

@ -415,45 +415,66 @@ class Contact < ApplicationRecord
# if total is smaller than needed, the load more
# we also need to sort by valid_to
# todo: extract to drapper. Then we can remove Domain#roles
def all_domains(page: nil, per: nil, params:)
# compose filter sql
filter_sql = case params[:domain_filter]
when "Registrant".freeze
%Q{select id from domains where registrant_id=#{id}}
when AdminDomainContact.to_s, TechDomainContact.to_s
%Q{select domain_id from domain_contacts where contact_id=#{id} AND type='#{params[:domain_filter]}'}
else
%Q{select domain_id from domain_contacts where contact_id=#{id} UNION select id from domains where registrant_id=#{id}}
end
def all_domains(page: nil, per: nil, params:, requester: nil)
filter_sql = qualified_domain_ids(params[:domain_filter])
# get sorting rules
sorts = params.fetch(:sort, {}).first || []
sort = Domain.column_names.include?(sorts.first) ? sorts.first : "valid_to"
order = {"asc"=>"desc", "desc"=>"asc"}[sorts.second] || "desc"
sort = %w[name registrar_name valid_to].include?(sorts.first) ? sorts.first : 'valid_to'
order = %w[asc desc].include?(sorts.second) ? sorts.second : 'desc'
# fetch domains
domains = Domain.where("domains.id IN (#{filter_sql})")
domains = qualified_domain_name_list(requester, filter_sql)
domains = domains.includes(:registrar).page(page).per(per)
if sorts.first == "registrar_name".freeze
# using small rails hack to generate outer join
domains = domains.includes(:registrar).where.not(registrars: {id: nil}).order("registrars.name #{order} NULLS LAST")
else
domains = domains.order("#{sort} #{order} NULLS LAST")
end
# using small rails hack to generate outer join
domains = if sorts.first == 'registrar_name'.freeze
domains.includes(:registrar).where.not(registrars: { id: nil })
.order("registrars.name #{order} NULLS LAST")
else
domains.order("#{sort} #{order} NULLS LAST")
end
# adding roles. Need here to make faster sqls
domain_c = Hash.new([])
registrant_domains.where(id: domains.map(&:id)).each{|d| domain_c[d.id] |= ["Registrant".freeze] }
DomainContact.where(contact_id: id, domain_id: domains.map(&:id)).each{|d| domain_c[d.domain_id] |= [d.type] }
domains.each{|d| d.roles = domain_c[d.id].uniq}
registrant_domains.where(id: domains.map(&:id)).each do |d|
domain_c[d.id] |= ['Registrant'.freeze]
end
DomainContact.where(contact_id: id, domain_id: domains.map(&:id)).each do |d|
domain_c[d.domain_id] |= [d.type]
end
domains.each { |d| d.roles = domain_c[d.id].uniq }
domains
end
def qualified_domain_name_list(requester, filter_sql)
return Domain.where('domains.id IN (?)', filter_sql) if requester.nil?
requester = Contact.find_by(id: requester)
registrant_user = RegistrantUser.find_or_initialize_by(registrant_ident:
"#{requester.ident_country_code}-#{requester.ident}")
begin
registrant_user.domains.where('domains.id IN (?)', filter_sql)
rescue CompanyRegister::NotAvailableError
registrant_user.direct_domains.where('domains.id IN (?)', filter_sql)
end
end
def qualified_domain_ids(domain_filter)
registrant_ids = registrant_domains.pluck(:id)
return registrant_ids if domain_filter == 'Registrant'
if %w[AdminDomainContact TechDomainContact].include? domain_filter
DomainContact.select('domain_id').where(contact_id: id, type: domain_filter)
else
(DomainContact.select('domain_id').where(contact_id: id).pluck(:domain_id) +
registrant_ids).uniq
end
end
def update_prohibited?
(statuses & [
CLIENT_UPDATE_PROHIBITED,

View file

@ -98,4 +98,4 @@ class RegistrantUser < User
user
end
end
end
end

View file

@ -0,0 +1,69 @@
# Hiding the queries behind its own class will allow us to include disputed or
# auctioned domains without meddling up with controller logic.
class RetainedDomains
RESERVED = 'reserved'.freeze
BLOCKED = 'blocked'.freeze
attr_reader :domains,
:type
def initialize(params)
@type = establish_type(params)
@domains = gather_domains
end
delegate :count, to: :domains
def to_jsonable
domains.map { |el| domain_to_jsonable(el) }
end
private
def establish_type(params)
type = params[:type]
case type
when RESERVED then :reserved
when BLOCKED then :blocked
else :all
end
end
def gather_domains
domains = blocked_domains.to_a.union(reserved_domains.to_a)
domains.sort_by(&:name)
end
def blocked_domains
if %i[all blocked].include?(type)
BlockedDomain.order(name: :desc).all
else
[]
end
end
def reserved_domains
if %i[all reserved].include?(type)
ReservedDomain.order(name: :desc).all
else
[]
end
end
def domain_to_jsonable(domain)
status = case domain
when ReservedDomain then RESERVED
when BlockedDomain then BLOCKED
end
punycode = SimpleIDN.to_ascii(domain.name)
{
name: domain.name,
status: status,
punycode_name: punycode,
}
end
end

View file

@ -1,5 +1,5 @@
<% domains = contact.all_domains(page: params[:domain_page], per: 20,
params: domain_filter_params.to_h) %>
params: domain_filter_params.to_h, requester: @requester_contact) %>
<div class="panel panel-default">
<div class="panel-heading">