Modifications for updating business and private contacts

This commit is contained in:
Sergei Tsõganov 2022-03-29 10:39:26 +03:00
parent 4afd32ebff
commit eb64b3aca4
10 changed files with 159 additions and 163 deletions

View file

@ -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

View file

@ -32,7 +32,7 @@ class Action < ApplicationRecord
subactions.map do |a|
{
code: a.contact&.code,
code: a.contact.code,
avail: 0,
reason: 'in use',
}

View file

@ -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

View file

@ -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,39 +40,35 @@ class RegistrantUser < User
{ result: false, counter: 0 }
end
def update_company_contacts
return [] if companies.blank?
def update_contacts
user = self
contacts = []
contacts.concat Contact.with_different_registrant_name(user)
.each{ |c| c.write_attribute(:name, user.username) }
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{ |c| c.write_attribute(:name, company.company_name) }
end
return [] if contacts.blank?
companies
grouped_contacts = contacts.group_by(&:registrar_id)
grouped_contacts.each do |registrar_id, contacts|
bulk_action, action = actions.create!(operation: :bulk_update) if contacts.size > 1
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
contacts
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"
)
def notify_registrar_contacts_updated(action:, registrar_id:)
registrar = Registrar.find(registrar_id)
registrar.notify(action) if registrar
end
def contacts(representable: true)
@ -111,24 +107,6 @@ class RegistrantUser < User
username.split.second
end
# rubocop:disable Metrics/MethodLength
def update_related_contacts
grouped_contacts = Contact.where(ident: ident, ident_country_code: country.alpha2)
.where('UPPER(name) != UPPER(?)', username)
.includes(:registrar)
.group_by(&:registrar)
grouped_contacts.each do |registrar, contacts|
bulk_action, action = actions.create!(operation: :bulk_update) if contacts.size > 1
contacts.each do |c|
if c.update(name: username)
action = actions.create!(contact: c, operation: :update, bulk_action_id: bulk_action&.id)
end
end
registrar.notify(bulk_action || action)
end
end
# rubocop:enable Metrics/MethodLength
class << self
def find_or_create_by_api_data(user_data = {})
return false unless user_data[:ident]
@ -165,8 +143,6 @@ 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

View file

@ -1,10 +0,0 @@
builder.extension do
builder.tag!('changePoll:changeData',
'xmlns:changePoll' => Xsd::Schema.filename(for_prefix: 'changePoll', for_version: '1.0')) 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)
builder.tag!('changePoll:reason', 'Auto-update according to official data') if action.bulk_action?
end
end

View file

@ -0,0 +1,18 @@
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

View file

@ -29,18 +29,15 @@ xml.epp_head do
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