Fixed codeclimate errors

This commit is contained in:
Sergei Tsõganov 2022-06-06 16:12:16 +03:00
parent a5ffce290d
commit 7c570e2916
12 changed files with 114 additions and 103 deletions

View file

@ -1,6 +1,6 @@
module Repp
module V1
class AccountController < BaseController
class AccountController < BaseController # rubocop:disable Metrics/ClassLength
load_and_authorize_resource
api :get, '/repp/v1/account'

View file

@ -9,7 +9,7 @@ module Repp
desc 'Get all existing contacts'
def index
authorize! :check, Epp::Contact
records = current_user.registrar.contacts.order(created_at: :desc)
records = current_user.registrar.contacts
q = records.ransack(search_params)
q.sorts = 'created_at desc' if q.sorts.empty?
@ -19,8 +19,7 @@ module Repp
.includes(:domain_contacts, :registrant_domains, :registrar)
render_success(data: { contacts: serialized_contacts(limited_contacts),
count: contacts.count,
statuses: Contact::STATUSES,
count: contacts.count, statuses: Contact::STATUSES,
ident_types: Contact::Ident.types })
end
@ -156,7 +155,7 @@ module Repp
end
def serialized_contacts(contacts)
return contacts.map {|c| c.code } unless index_params[:details] == 'true'
return contacts.map(&code) unless index_params[:details] == 'true'
address_processing = Contact.address_processing?
contacts.map do |c|

View file

@ -51,14 +51,11 @@ module Repp
def select_renewable_domains
@epp_errors ||= ActiveModel::Errors.new(self)
if bulk_renew_params[:domains].instance_of?(Array)
@domains = bulk_renew_domains
@epp_errors.add(:epp_errors, msg: 'Domains cannot be empty', code: '2005') if @domains.empty?
else
@epp_errors.add(:epp_errors, msg: 'Domains attribute must be an array', code: '2005')
@domains = bulk_renew_domains
if @domains.empty?
@epp_errors.add(:epp_errors, msg: 'Domains cannot be empty',
code: '2005')
end
return handle_errors if @epp_errors.any?
end
@ -77,14 +74,18 @@ module Repp
def bulk_renew_domains
@epp_errors ||= ActiveModel::Errors.new(self)
domains = []
bulk_renew_params[:domains].each do |idn|
domain = Epp::Domain.find_by(name: idn)
domains << domain if domain
next if domain
if bulk_renew_params[:domains].instance_of?(Array)
bulk_renew_params[:domains].each do |idn|
domain = Epp::Domain.find_by(name: idn)
domains << domain if domain
next if domain
@epp_errors.add(:epp_errors,
msg: "Object does not exist: #{idn}",
code: '2304')
@epp_errors.add(:epp_errors,
msg: "Object does not exist: #{idn}",
code: '2304')
end
else
@epp_errors.add(:epp_errors, msg: 'Domains attribute must be an array', code: '2005')
end
domains

View file

@ -30,7 +30,7 @@ module Repp
api :GET, '/repp/v1/domains/:domain_name'
desc 'Get a specific domain'
def show
@domain = Epp::Domain.find_by_name(params[:id])
@domain = Epp::Domain.find_by(name: params[:id])
authorize! :info, @domain
sponsor = @domain.registrar == current_user.registrar
@ -251,34 +251,33 @@ module Repp
dup_params = domain_params.to_h.dup
return dup_params unless dup_params[:contacts]
new_contact_params = dup_params[:contacts].map do |c|
c.to_h.symbolize_keys
end
modify_contact_params(dup_params)
end
old_contact_params = @domain.domain_contacts.map do |c|
{ code: c.contact_code_cache, type: c.name.downcase }
def modify_contact_params(params)
new_contact_params = params[:contacts].map { |c| c.to_h.symbolize_keys }
old_contact_params = @domain.domain_contacts.includes(:contact).map do |c|
{ code: c.contact.code, type: c.name.downcase }
end
dup_params[:contacts] = (new_contact_params - old_contact_params).map { |c| c.merge(action: 'add') }
dup_params[:contacts].concat((old_contact_params - new_contact_params)
.map { |c| c.merge(action: 'rem') })
dup_params
params[:contacts] = (new_contact_params - old_contact_params).map do |c|
c.merge(action: 'add')
end
params[:contacts].concat((old_contact_params - new_contact_params)
.map { |c| c.merge(action: 'rem') })
params
end
def domain_params
params.require(:domain)
.permit(:name, :period, :period_unit, :registrar,
:transfer_code, :reserved_pw, :legal_document,
:registrant, legal_document: %i[body type],
registrant: [%i[code verified]],
dns_keys: [%i[id flags alg protocol public_key action]],
nameservers: [[:id, :hostname,
:action, { ipv4: [], ipv6: [] }]],
contacts: [%i[code type action]],
nameservers_attributes: [[:hostname, { ipv4: [], ipv6: [] }]],
admin_contacts: [], tech_contacts: [],
dnskeys_attributes: [%i[flags alg protocol public_key]],
delete: [:verified])
params.require(:domain).permit(:name, :period, :period_unit, :registrar, :transfer_code,
:reserved_pw, :legal_document, :registrant,
legal_document: %i[body type], registrant: [%i[code verified]],
dns_keys: [%i[id flags alg protocol public_key action]],
nameservers: [[:id, :hostname, :action, { ipv4: [], ipv6: [] }]],
contacts: [%i[code type action]],
nameservers_attributes: [[:hostname, { ipv4: [], ipv6: [] }]],
admin_contacts: [], tech_contacts: [],
dnskeys_attributes: [%i[flags alg protocol public_key]],
delete: [:verified])
end
end
end

View file

@ -115,4 +115,4 @@ module Repp
end
end
end
end
end

View file

@ -46,4 +46,4 @@ module Repp
end
end
end
end
end

View file

@ -11,33 +11,60 @@ module Repp
if can?(:manage, :poll)
user_notifications = user.unread_notifications
notification = user_notifications.order('created_at DESC').take
notifications_count = user_notifications.count
if notification&.attached_obj_type && notification&.attached_obj_id
begin
object = object_by_type(notification.attached_obj_type)
.find(notification.attached_obj_id)
rescue => e
# the data model might be inconsistent; or ...
# this could happen if the registrar does not dequeue messages, and then the domain was deleted
# SELECT messages.id, domains.name, messages.body FROM messages LEFT OUTER
# JOIN domains ON attached_obj_id::INTEGER = domains.id
# WHERE attached_obj_type = 'Epp::Domain' AND name IS NULL;
message = 'orphan message, domain deleted, registrar should dequeue: '
Rails.logger.error message + e.to_s
end
end
end
data = serialize_data(registrar: registrar,
notification: notification,
notifications_count: notifications_count,
object: object)
render_success(data: serialize_data(registrar: registrar,
notification: notification,
notifications_count: user_notifications&.count,
object: notification_object(notification)))
end
render_success(data: data)
def serialized_domain_transfer(object)
{
name: object.domain_name, trStatus: object.status,
reID: object.new_registrar.code,
reDate: object.transfer_requested_at.try(:iso8601),
acID: object.old_registrar.code,
acDate: object.transferred_at.try(:iso8601) || object.wait_until.try(:iso8601),
exDate: object.domain_valid_to.iso8601
}
end
def serialized_contact_update_action(object)
{
contacts: object.to_non_available_contact_codes,
operation: object.operation,
opDate: object.created_at.utc.xmlschema,
svTrid: object.id,
who: object.user.username,
reason: 'Auto-update according to official data',
}
end
private
# rubocop:disable Style/RescueStandardError
def notification_object(notification)
return unless notification
return unless notification.attached_obj_type || notification.attached_obj_id
begin
object_by_type(notification.attached_obj_type)
.find(notification.attached_obj_id)
rescue => e
# the data model might be inconsistent; or ...
# this could happen if the registrar does not dequeue messages,
# and then the domain was deleted
# SELECT messages.id, domains.name, messages.body FROM messages LEFT OUTER
# JOIN domains ON attached_obj_id::INTEGER = domains.id
# WHERE attached_obj_type = 'Epp::Domain' AND name IS NULL;
message = 'orphan message, domain deleted, registrar should dequeue: '
Rails.logger.error message + e.to_s
end
end
# rubocop:enable Style/RescueStandardError
def object_by_type(object_type)
Object.const_get(object_type)
rescue NameError
@ -83,29 +110,9 @@ module Repp
def serialized_object(object, obj_type)
return unless object
case obj_type
when 'DomainTransfer'
{
name: object.domain_name,
trStatus: object.status,
reID: object.new_registrar.code,
reDate: object.transfer_requested_at.try(:iso8601),
acID: object.old_registrar.code,
acDate: object.transferred_at.try(:iso8601) || object.wait_until.try(:iso8601),
exDate: object.domain_valid_to.iso8601,
}
when 'ContactUpdateAction'
{
contacts: object.to_non_available_contact_codes,
operation: object.operation,
opDate: object.created_at.utc.xmlschema,
svTrid: object.id,
who: object.user.username,
reason: 'Auto-update according to official data',
}
end
try("serialized_#{obj_type.underscore}", object)
end
end
end
end
end
end

View file

@ -6,7 +6,7 @@ class AdminDomainContact < DomainContact
skipped_domains = []
admin_contacts = where(contact: current_contact)
admin_contacts.includes(:domain).each do |admin_contact|
admin_contacts.includes(:domain).find_each do |admin_contact|
if admin_contact.domain.bulk_update_prohibited?
skipped_domains << admin_contact.domain.name
next

View file

@ -5,7 +5,7 @@ class TechDomainContact < DomainContact
skipped_domains = []
tech_contacts = where(contact: current_contact)
tech_contacts.includes(:domain).each do |tech_contact|
tech_contacts.includes(:domain).find_each do |tech_contact|
if irreplaceable?(tech_contact)
skipped_domains << tech_contact.domain.name
next

View file

@ -10,13 +10,15 @@ module Serializers
@simplify = options[:simplify] || false
end
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def to_json(obj = contact)
return simple_object if @simplify
json = { id: obj.uuid, code: obj.code, name: obj.name, ident: ident,
email: obj.email, phone: obj.phone, created_at: obj.created_at,
auth_info: obj.auth_info, statuses: statuses,
disclosed_attributes: obj.disclosed_attributes, registrar: registrar }
json = { id: obj.uuid, code: obj.code, name: obj.name, ident: ident, phone: obj.phone,
created_at: obj.created_at, auth_info: obj.auth_info, email: obj.email,
statuses: statuses, disclosed_attributes: obj.disclosed_attributes,
registrar: registrar }
json[:address] = address if @show_address
if @domain_params
json[:domains] = domains
@ -24,6 +26,8 @@ module Serializers
end
json
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
def registrar
contact.registrar.as_json(only: %i[name website])

View file

@ -9,6 +9,7 @@ module Serializers
@simplify = simplify
end
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def to_json(obj = domain)
return simple_object if @simplify
@ -26,6 +27,7 @@ module Serializers
json[:transfer_code] = obj.auth_info if @sponsored
json
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
def contacts

View file

@ -8,6 +8,8 @@ module Serializers
@simplify = simplify
end
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def to_json(obj = invoice)
return simple_object if @simplify
@ -29,16 +31,11 @@ module Serializers
def seller
{
name: invoice.seller_name,
reg_no: invoice.seller_reg_no,
iban: invoice.seller_iban,
bank: invoice.seller_bank,
swift: invoice.seller_swift,
vat_no: invoice.seller_vat_no,
address: invoice.seller_address,
country: invoice.seller_country.name,
phone: invoice.seller_phone,
url: invoice.seller_url,
name: invoice.seller_name, reg_no: invoice.seller_reg_no,
iban: invoice.seller_iban, bank: invoice.seller_bank,
swift: invoice.seller_swift, vat_no: invoice.seller_vat_no,
address: invoice.seller_address, country: invoice.seller_country.name,
phone: invoice.seller_phone, url: invoice.seller_url,
email: invoice.seller_email,
contact_name: invoice.seller_contact_name,
}
@ -80,6 +77,8 @@ module Serializers
recipient: invoice.buyer.billing_email,
}
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
end
end
end