Extract company register

Closes #1079, #916, #1077
This commit is contained in:
Artur Beljajev 2019-02-09 16:35:01 +02:00
parent 8c4e6f1656
commit 83f8a9fb6a
44 changed files with 530 additions and 610 deletions

View file

@ -30,15 +30,6 @@ module Api
header.gsub(pattern, '') if header&.match(pattern)
end
def associated_domains(user)
country_code, ident = user.registrant_ident.split('-')
BusinessRegistryCache.fetch_associated_domains(ident, country_code)
rescue Soap::Arireg::NotAvailableError => error
Rails.logger.fatal("[EXCEPTION] #{error}")
user.domains
end
def authenticate
decryptor = AuthTokenDecryptor.create_with_defaults(bearer_token)
decryptor.decrypt_token

View file

@ -4,8 +4,6 @@ module Api
module V1
module Registrant
class ContactsController < ::Api::V1::Registrant::BaseController
before_action :set_contacts_pool
def index
limit = params[:limit] || 200
offset = params[:offset] || 0
@ -20,8 +18,8 @@ module Api
status: :bad_request) && return
end
@contacts = @contacts_pool.limit(limit).offset(offset)
serialized_contacts = @contacts.map do |item|
contacts = current_user_contacts.limit(limit).offset(offset)
serialized_contacts = contacts.map do |item|
serializer = Serializers::RegistrantApi::Contact.new(item)
serializer.to_json
end
@ -30,7 +28,7 @@ module Api
end
def show
@contact = @contacts_pool.find_by(uuid: params[:uuid])
@contact = current_user_contacts.find_by(uuid: params[:uuid])
if @contact
render json: @contact
@ -40,7 +38,7 @@ module Api
end
def update
contact = @contacts_pool.find_by!(uuid: params[:uuid])
contact = current_user_contacts.find_by!(uuid: params[:uuid])
contact.name = params[:name] if params[:name].present?
contact.email = params[:email] if params[:email].present?
contact.phone = params[:phone] if params[:phone].present?
@ -97,22 +95,10 @@ module Api
private
def set_contacts_pool
country_code, ident = current_registrant_user.registrant_ident.to_s.split '-'
associated_domain_ids = begin
BusinessRegistryCache.fetch_by_ident_and_cc(ident, country_code).associated_domain_ids
end
available_contacts_ids = begin
DomainContact.where(domain_id: associated_domain_ids).pluck(:contact_id) |
Domain.where(id: associated_domain_ids).pluck(:registrant_id)
end
@contacts_pool = Contact.where(id: available_contacts_ids)
rescue Soap::Arireg::NotAvailableError => error
Rails.logger.fatal("[EXCEPTION] #{error}")
render json: { errors: [{ base: ['Business Registry not available'] }] },
status: :service_unavailable and return
def current_user_contacts
current_registrant_user.contacts
rescue CompanyRegister::NotAvailableError
current_registrant_user.direct_contacts
end
end
end

View file

@ -18,7 +18,7 @@ module Api
status: :bad_request) && return
end
@domains = associated_domains(current_registrant_user).limit(limit).offset(offset)
@domains = current_user_domains.limit(limit).offset(offset)
serialized_domains = @domains.map do |item|
serializer = Serializers::RegistrantApi::Domain.new(item)
@ -29,8 +29,7 @@ module Api
end
def show
domain_pool = associated_domains(current_registrant_user)
@domain = domain_pool.find_by(uuid: params[:uuid])
@domain = current_user_domains.find_by(uuid: params[:uuid])
if @domain
serializer = Serializers::RegistrantApi::Domain.new(@domain)
@ -39,6 +38,14 @@ module Api
render json: { errors: [{ base: ['Domain not found'] }] }, status: :not_found
end
end
private
def current_user_domains
current_registrant_user.domains
rescue CompanyRegister::NotAvailableError
current_registrant_user.direct_domains
end
end
end
end

View file

@ -1,21 +1,19 @@
class Registrant::ContactsController < RegistrantController
helper_method :domain_ids
helper_method :domain
helper_method :fax_enabled?
skip_authorization_check only: %i[edit update]
def show
@contact = Contact.where(id: contacts).find_by(id: params[:id])
@contact = current_user_contacts.find(params[:id])
authorize! :read, @contact
end
def edit
@contact = Contact.where(id: contacts).find(params[:id])
@contact = current_user_contacts.find(params[:id])
end
def update
@contact = Contact.where(id: contacts).find(params[:id])
@contact = current_user_contacts.find(params[:id])
@contact.attributes = contact_params
response = update_contact_via_api(@contact.uuid)
updated = response.is_a?(Net::HTTPSuccess)
@ -31,38 +29,10 @@ class Registrant::ContactsController < RegistrantController
private
def contacts
begin
DomainContact.where(domain_id: domain_ids).pluck(:contact_id) | Domain.where(id: domain_ids).pluck(:registrant_id)
rescue Soap::Arireg::NotAvailableError => error
flash[:notice] = I18n.t(error.json[:message])
Rails.logger.fatal("[EXCEPTION] #{error.to_s}")
[]
end
end
def domain_ids
@domain_ids ||= begin
ident_cc, ident = current_registrant_user.registrant_ident.to_s.split '-'
BusinessRegistryCache.fetch_by_ident_and_cc(ident, ident_cc).associated_domain_ids
end
end
def domain
current_user_domains.find(params[:domain_id])
end
def current_user_domains
ident_cc, ident = current_registrant_user.registrant_ident.split '-'
begin
BusinessRegistryCache.fetch_associated_domains ident, ident_cc
rescue Soap::Arireg::NotAvailableError => error
flash[:notice] = I18n.t(error.json[:message])
Rails.logger.fatal("[EXCEPTION] #{error.to_s}")
current_registrant_user.domains
end
end
def contact_params
permitted = %i[
name

View file

@ -1,22 +1,24 @@
class Registrant::DomainsController < RegistrantController
def index
authorize! :view, :registrant_domains
params[:q] ||= {}
normalize_search_parameters do
@q = domains.search(params[:q])
@q = current_user_domains.search(params[:q])
@domains = @q.result.page(params[:page])
end
@domains = @domains.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?
end
def show
@domain = domains.find(params[:id])
@domain = current_user_domains.find(params[:id])
authorize! :read, @domain
end
def domain_verification_url
authorize! :view, :registrant_domains
dom = domains.find(params[:id])
dom = current_user_domains.find(params[:id])
if (dom.statuses.include?(DomainStatus::PENDING_UPDATE) || dom.statuses.include?(DomainStatus::PENDING_DELETE_CONFIRMATION)) &&
dom.pending_json.present?
@ -34,7 +36,7 @@ class Registrant::DomainsController < RegistrantController
authorize! :view, :registrant_domains
params[:q] ||= {}
normalize_search_parameters do
@q = domains.search(params[:q])
@q = current_user_domains.search(params[:q])
@domains = @q
end
@ -49,21 +51,6 @@ class Registrant::DomainsController < RegistrantController
private
def set_domain
@domain = domains.find(params[:id])
end
def domains
ident_cc, ident = current_registrant_user.registrant_ident.split '-'
begin
BusinessRegistryCache.fetch_associated_domains ident, ident_cc
rescue Soap::Arireg::NotAvailableError => error
flash[:notice] = I18n.t(error.json[:message])
Rails.logger.fatal("[EXCEPTION] #{error.to_s}")
current_registrant_user.domains
end
end
def normalize_search_parameters
ca_cache = params[:q][:valid_to_lteq]
begin

View file

@ -19,4 +19,18 @@ class RegistrantController < ApplicationController
def user_for_paper_trail
current_registrant_user.present? ? current_registrant_user.id_role_username : 'anonymous'
end
def current_user_contacts
current_registrant_user.contacts
rescue CompanyRegister::NotAvailableError
flash.now[:notice] = t('registrant.company_register_unavailable')
current_registrant_user.direct_contacts
end
def current_user_domains
current_registrant_user.domains
rescue CompanyRegister::NotAvailableError
flash.now[:notice] = t('registrant.company_register_unavailable')
current_registrant_user.direct_domains
end
end