mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 13:36:15 +02:00
Merge pull request #2315 from internetee/2307-bulk-change-poll-messages
Fixed notifications about automatic contact name update
This commit is contained in:
commit
95780ec84a
23 changed files with 329 additions and 221 deletions
|
@ -34,15 +34,15 @@ module Api
|
|||
end
|
||||
end
|
||||
|
||||
def do_need_update_contact
|
||||
result = current_registrant_user.do_need_update_contact?
|
||||
def do_need_update_contacts
|
||||
result = current_registrant_user.do_need_update_contacts?
|
||||
render json: { update_contacts: result[:result], counter: result[:counter] }
|
||||
end
|
||||
|
||||
def update_company_contacts
|
||||
companies = current_registrant_user.update_company_contacts
|
||||
def update_contacts
|
||||
contacts = current_registrant_user.update_contacts
|
||||
|
||||
render json: { message: 'get it', companies: companies }
|
||||
render json: { message: 'get it', contacts: contacts }
|
||||
end
|
||||
|
||||
def update
|
||||
|
|
|
@ -12,7 +12,7 @@ class Ability
|
|||
@user.roles&.each { |role| send(role) }
|
||||
when 'ApiUser'
|
||||
@user.roles&.each { |role| send(role) }
|
||||
when 'RegistrantUser'
|
||||
when 'RegistrantUser'
|
||||
static_registrant
|
||||
end
|
||||
|
||||
|
|
|
@ -2,18 +2,40 @@ class Action < ApplicationRecord
|
|||
has_paper_trail versions: { class_name: 'Version::ActionVersion' }
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :contact
|
||||
belongs_to :contact, optional: true
|
||||
has_many :subactions, class_name: 'Action',
|
||||
foreign_key: 'bulk_action_id',
|
||||
inverse_of: :bulk_action,
|
||||
dependent: :destroy
|
||||
belongs_to :bulk_action, class_name: 'Action', optional: true
|
||||
|
||||
validates :operation, inclusion: { in: proc { |action| action.class.valid_operations } }
|
||||
|
||||
class << self
|
||||
def valid_operations
|
||||
%w[update]
|
||||
%w[update bulk_update]
|
||||
end
|
||||
end
|
||||
|
||||
def notification_key
|
||||
raise 'Action object is missing' unless contact
|
||||
raise 'Action object is missing' unless bulk_action? || contact
|
||||
|
||||
"contact_#{operation}".to_sym
|
||||
end
|
||||
|
||||
def bulk_action?
|
||||
!!subactions.exists?
|
||||
end
|
||||
|
||||
def to_non_available_contact_codes
|
||||
return [] unless bulk_action?
|
||||
|
||||
subactions.map do |a|
|
||||
{
|
||||
code: a.contact.code,
|
||||
avail: 0,
|
||||
reason: 'in use',
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
1
app/models/bulk_action.rb
Normal file
1
app/models/bulk_action.rb
Normal file
|
@ -0,0 +1 @@
|
|||
class BulkAction < Action; end
|
|
@ -30,6 +30,19 @@ class Contact < ApplicationRecord
|
|||
.where('success = false and verified_at IS NOT NULL')
|
||||
}
|
||||
|
||||
scope :with_different_company_name, (lambda do |company|
|
||||
where("ident = ? AND ident_country_code = 'EE' AND name != ?",
|
||||
company.registration_number,
|
||||
company.company_name)
|
||||
end)
|
||||
|
||||
scope :with_different_registrant_name, (lambda do |user|
|
||||
where('ident = ? AND ident_country_code = ? AND UPPER(name) != UPPER(?)',
|
||||
user.ident,
|
||||
user.country.alpha2,
|
||||
user.username)
|
||||
end)
|
||||
|
||||
NAME_REGEXP = /([\u00A1-\u00B3\u00B5-\u00BF\u0021-\u0026\u0028-\u002C\u003A-\u0040]|
|
||||
[\u005B-\u005F\u007B-\u007E\u2040-\u206F\u20A0-\u20BF\u2100-\u218F])/x
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ class Epp::Contact < Contact
|
|||
codes = codes.map { |c| c.include?(':') ? c : "#{reg}:#{c}" }
|
||||
|
||||
res = []
|
||||
codes.map { |c| c.include?(':') ? c : "#{reg}:#{c}" }.map { |c| c.strip.upcase }.each do |x|
|
||||
codes.map { |c| c.strip.upcase }.each do |x|
|
||||
c = find_by_epp_code(x)
|
||||
res << (c ? { code: c.code, avail: 0, reason: 'in use' } : { code: x, avail: 1 })
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ class Notification < ApplicationRecord
|
|||
include Versions # version/notification_version.rb
|
||||
|
||||
belongs_to :registrar
|
||||
belongs_to :action
|
||||
belongs_to :action, optional: true
|
||||
|
||||
scope :unread, -> { where(read: false) }
|
||||
|
||||
|
|
|
@ -26,13 +26,13 @@ class RegistrantUser < User
|
|||
[]
|
||||
end
|
||||
|
||||
def do_need_update_contact?
|
||||
return { result: false, counter: 0 } if companies.blank?
|
||||
|
||||
def do_need_update_contacts?
|
||||
counter = 0
|
||||
|
||||
counter += Contact.with_different_registrant_name(self).size
|
||||
|
||||
companies.each do |company|
|
||||
counter += Contact.where(ident: company.registration_number, ident_country_code: 'EE')&.
|
||||
reject { |contact| contact.name == company.company_name }.size
|
||||
counter += Contact.with_different_company_name(company).size
|
||||
end
|
||||
|
||||
return { result: true, counter: counter } if counter.positive?
|
||||
|
@ -40,40 +40,25 @@ class RegistrantUser < User
|
|||
{ result: false, counter: 0 }
|
||||
end
|
||||
|
||||
def update_company_contacts
|
||||
return [] if companies.blank?
|
||||
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
def update_contacts
|
||||
user = self
|
||||
contacts = []
|
||||
contacts.concat(Contact.with_different_registrant_name(user).each do |c|
|
||||
c.write_attribute(:name, user.username)
|
||||
end)
|
||||
companies.each do |company|
|
||||
contacts = Contact.where(ident: company.registration_number, ident_country_code: 'EE')
|
||||
|
||||
next if contacts.blank?
|
||||
|
||||
contacts.each do |contact|
|
||||
next if company.company_name == contact.name
|
||||
|
||||
update_company_name(contact: contact, company: company)
|
||||
end
|
||||
contacts.concat(Contact.with_different_company_name(company).each do |c|
|
||||
c.write_attribute(:name, company.company_name)
|
||||
end)
|
||||
end
|
||||
|
||||
companies
|
||||
end
|
||||
|
||||
def update_company_name(contact:, company:)
|
||||
old_contact_name = contact.name
|
||||
contact.name = company.company_name
|
||||
|
||||
contact.save(validate: false)
|
||||
|
||||
notify_registrar_data_updated(company_name: company.company_name,
|
||||
old_contact_name: old_contact_name,
|
||||
contact: contact)
|
||||
end
|
||||
|
||||
def notify_registrar_data_updated(company_name:, old_contact_name:, contact:)
|
||||
contact.registrar.notifications.create!(
|
||||
text: "Contact update: #{contact.id} name updated from #{old_contact_name} to #{company_name} by the registry"
|
||||
)
|
||||
return [] if contacts.blank?
|
||||
|
||||
group_and_bulk_update(contacts)
|
||||
contacts
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
|
||||
def contacts(representable: true)
|
||||
Contact.registrant_user_contacts(self, representable: representable)
|
||||
|
@ -111,17 +96,6 @@ class RegistrantUser < User
|
|||
username.split.second
|
||||
end
|
||||
|
||||
def update_related_contacts
|
||||
contacts = Contact.where(ident: ident, ident_country_code: country.alpha2)
|
||||
.where('UPPER(name) != UPPER(?)', username)
|
||||
|
||||
contacts.each do |contact|
|
||||
contact.update(name: username)
|
||||
action = actions.create!(contact: contact, operation: :update)
|
||||
contact.registrar.notify(action)
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
def find_or_create_by_api_data(user_data = {})
|
||||
return false unless user_data[:ident]
|
||||
|
@ -158,9 +132,27 @@ class RegistrantUser < User
|
|||
user = find_or_create_by(registrant_ident: "#{user_data[:country_code]}-#{user_data[:ident]}")
|
||||
user.username = "#{user_data[:first_name]} #{user_data[:last_name]}"
|
||||
user.save
|
||||
|
||||
user.update_related_contacts
|
||||
user
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def group_and_bulk_update(contacts)
|
||||
contacts.group_by(&:registrar_id).each do |registrar_id, reg_contacts|
|
||||
bulk_action, action = actions.create!(operation: :bulk_update) if reg_contacts.size > 1
|
||||
reg_contacts.each do |c|
|
||||
if c.save(validate: false)
|
||||
action = actions.create!(contact: c, operation: :update, bulk_action_id: bulk_action&.id)
|
||||
end
|
||||
end
|
||||
notify_registrar_contacts_updated(action: bulk_action || action,
|
||||
registrar_id: registrar_id)
|
||||
end
|
||||
end
|
||||
|
||||
def notify_registrar_contacts_updated(action:, registrar_id:)
|
||||
registrar = Registrar.find(registrar_id)
|
||||
registrar&.notify(action)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -218,8 +218,15 @@ class Registrar < ApplicationRecord
|
|||
end
|
||||
|
||||
def notify(action)
|
||||
text = I18n.t("notifications.texts.#{action.notification_key}", contact: action.contact.code)
|
||||
notifications.create!(text: text)
|
||||
text = I18n.t("notifications.texts.#{action.notification_key}", contact: action.contact&.code,
|
||||
count: action.subactions&.count)
|
||||
if action.bulk_action?
|
||||
notifications.create!(text: text, action_id: action.id,
|
||||
attached_obj_type: 'BulkAction',
|
||||
attached_obj_id: action.id)
|
||||
else
|
||||
notifications.create!(text: text)
|
||||
end
|
||||
end
|
||||
|
||||
def e_invoice_iban
|
||||
|
|
|
@ -5,15 +5,7 @@ xml.epp_head do
|
|||
end
|
||||
|
||||
xml.resData do
|
||||
xml.tag!('contact:chkData', 'xmlns:contact' =>
|
||||
Xsd::Schema.filename(for_prefix: 'contact-ee', for_version: '1.1')) do
|
||||
@results.each do |result|
|
||||
xml.tag!('contact:cd') do
|
||||
xml.tag! "contact:id", result[:code], avail: result[:avail]
|
||||
xml.tag!('contact:reason', result[:reason]) unless result[:avail] == 1
|
||||
end
|
||||
end
|
||||
end
|
||||
xml << render('epp/contacts/partials/check', builder: xml, results: @results)
|
||||
end
|
||||
|
||||
render('epp/shared/trID', builder: xml)
|
||||
|
|
9
app/views/epp/contacts/partials/_check.xml.builder
Normal file
9
app/views/epp/contacts/partials/_check.xml.builder
Normal file
|
@ -0,0 +1,9 @@
|
|||
builder.tag!('contact:chkData', 'xmlns:contact' =>
|
||||
Xsd::Schema.filename(for_prefix: 'contact-ee', for_version: '1.1')) do
|
||||
results.each do |result|
|
||||
builder.tag!('contact:cd') do
|
||||
builder.tag! 'contact:id', result[:code], avail: result[:avail]
|
||||
# builder.tag!('contact:reason', result[:reason]) unless result[:avail] == 1
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +0,0 @@
|
|||
builder.extension do
|
||||
builder.tag!('changePoll:changeData',
|
||||
'xmlns:changePoll' => Xsd::Schema.filename(for_prefix: 'changePoll')) do
|
||||
builder.tag!('changePoll:operation', action.operation)
|
||||
builder.tag!('changePoll:date', action.created_at.utc.xmlschema)
|
||||
builder.tag!('changePoll:svTRID', action.id)
|
||||
builder.tag!('changePoll:who', action.user)
|
||||
end
|
||||
end
|
21
app/views/epp/poll/_extension.xml.builder
Normal file
21
app/views/epp/poll/_extension.xml.builder
Normal file
|
@ -0,0 +1,21 @@
|
|||
builder.extension do
|
||||
builder.tag!('changePoll:changeData',
|
||||
'xmlns:changePoll' => Xsd::Schema.filename(for_prefix: 'changePoll',
|
||||
for_version: '1.0')) do
|
||||
case type
|
||||
when 'action'
|
||||
builder.tag!('changePoll:operation', obj.operation)
|
||||
builder.tag!('changePoll:date', obj.created_at.utc.xmlschema)
|
||||
builder.tag!('changePoll:svTRID', obj.id)
|
||||
builder.tag!('changePoll:who', obj.user)
|
||||
if obj.bulk_action?
|
||||
builder.tag!(
|
||||
'changePoll:reason',
|
||||
'Auto-update according to official data'
|
||||
)
|
||||
end
|
||||
when 'state'
|
||||
builder.tag!('changePoll:operation', obj)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -9,27 +9,35 @@ xml.epp_head do
|
|||
xml.msg @notification.text
|
||||
end
|
||||
|
||||
if @notification.attached_obj_type == 'DomainTransfer' && @object
|
||||
xml.resData do
|
||||
xml << render('epp/domains/partials/transfer', builder: xml, dt: @object)
|
||||
if @object
|
||||
case @notification.attached_obj_type
|
||||
when 'DomainTransfer'
|
||||
xml.resData do
|
||||
xml << render('epp/domains/partials/transfer', builder: xml, dt: @object)
|
||||
end
|
||||
when 'BulkAction'
|
||||
xml.resData do
|
||||
xml << render(
|
||||
'epp/contacts/partials/check',
|
||||
builder: xml,
|
||||
results: @object.to_non_available_contact_codes
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if @notification.action&.contact || @notification.registry_lock?
|
||||
if @notification.action || @notification.registry_lock?
|
||||
if @notification.registry_lock?
|
||||
state = @notification.text.include?('unlocked') ? 'unlock' : 'lock'
|
||||
xml.extension do
|
||||
xml.tag!('changePoll:changeData',
|
||||
'xmlns:changePoll': Xsd::Schema.filename(for_prefix: 'changePoll')) do
|
||||
xml.tag!('changePoll:operation', state)
|
||||
end
|
||||
end
|
||||
render(partial: 'epp/poll/extension',
|
||||
locals: { builder: xml,
|
||||
obj: state,
|
||||
type: 'state' })
|
||||
else
|
||||
render(partial: 'epp/poll/action',
|
||||
locals: {
|
||||
builder: xml,
|
||||
action: @notification.action,
|
||||
})
|
||||
render(partial: 'epp/poll/extension',
|
||||
locals: { builder: xml,
|
||||
obj: @notification.action,
|
||||
type: 'action' })
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue