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

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