mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 06:34:46 +02:00
Merge pull request #1527 from internetee/fix-registrant-contact-view
Registrant: Allow to view other contacts of domain
This commit is contained in:
commit
3d30469db9
5 changed files with 76 additions and 28 deletions
|
@ -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,
|
||||
|
|
|
@ -98,4 +98,4 @@ class RegistrantUser < User
|
|||
user
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue