Merge pull request #2315 from internetee/2307-bulk-change-poll-messages

Fixed notifications about automatic contact name update
This commit is contained in:
Timo Võhmar 2022-04-05 18:56:47 +03:00 committed by GitHub
commit 95780ec84a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 329 additions and 221 deletions

View file

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

View file

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

View file

@ -0,0 +1 @@
class BulkAction < Action; end

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

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

View file

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

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

View file

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