From c1bba784d6450948c4f809bf805051b749372de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Wed, 28 Oct 2020 16:35:35 +0200 Subject: [PATCH 1/6] Update contact(s) name based on eIdentity data from Registrant API --- app/models/registrant_user.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index c0addb5cd..40f48f69c 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -2,6 +2,7 @@ class RegistrantUser < User attr_accessor :idc_data devise :trackable, :timeoutable + after_save :update_related_contacts def ability @ability ||= Ability.new(self) @@ -54,6 +55,12 @@ class RegistrantUser < User username.split.second end + def update_related_contacts + cc, idcode = registrant_ident.split('-') + contacts = Contact.where(ident: idcode, country_code: cc).where('name != ?', username) + contacts.each { |c| c.update(name: username) } + end + class << self def find_or_create_by_api_data(user_data = {}) return false unless user_data[:ident] From 7b258f8e798ada9d681177ba6e0d1c9e60abbae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Wed, 28 Oct 2020 17:05:48 +0200 Subject: [PATCH 2/6] Don't force RegistrantUser name to be uppercase --- app/models/registrant_user.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 40f48f69c..cb8212e17 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -57,7 +57,9 @@ class RegistrantUser < User def update_related_contacts cc, idcode = registrant_ident.split('-') - contacts = Contact.where(ident: idcode, country_code: cc).where('name != ?', username) + contacts = Contact.where(ident: idcode, country_code: cc) + .where('UPPER(name) != UPPER(?)', username) + contacts.each { |c| c.update(name: username) } end @@ -67,8 +69,8 @@ class RegistrantUser < User return false unless user_data[:first_name] return false unless user_data[:last_name] - user_data.each_value { |v| v.upcase! if v.is_a?(String) } user_data[:country_code] ||= 'EE' + %i[ident country_code].each { |f| user_data[f].upcase! if user_data[f].is_a?(String) } find_or_create_by_user_data(user_data) end From e1d2fb45d57be67ae924cf47767cc31b253d7624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 29 Oct 2020 11:11:39 +0200 Subject: [PATCH 3/6] Fix tests --- app/models/registrant_user.rb | 5 ++-- .../registrant_user_creation_test.rb | 23 +++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index cb8212e17..420446582 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -2,7 +2,6 @@ class RegistrantUser < User attr_accessor :idc_data devise :trackable, :timeoutable - after_save :update_related_contacts def ability @ability ||= Ability.new(self) @@ -56,8 +55,7 @@ class RegistrantUser < User end def update_related_contacts - cc, idcode = registrant_ident.split('-') - contacts = Contact.where(ident: idcode, country_code: cc) + contacts = Contact.where(ident: ident, country_code: country_code) .where('UPPER(name) != UPPER(?)', username) contacts.each { |c| c.update(name: username) } @@ -100,6 +98,7 @@ class RegistrantUser < User user.username = "#{user_data[:first_name]} #{user_data[:last_name]}" user.save + user.update_related_contacts user end end diff --git a/test/models/registrant_user/registrant_user_creation_test.rb b/test/models/registrant_user/registrant_user_creation_test.rb index 5ed680795..9fff4ca02 100644 --- a/test/models/registrant_user/registrant_user_creation_test.rb +++ b/test/models/registrant_user/registrant_user_creation_test.rb @@ -14,7 +14,7 @@ class RegistrantUserCreationTest < ActiveSupport::TestCase assert_equal('JOHN SMITH', user.username) end - def test_find_or_create_by_api_data_creates_a_user_after_upcasing_input + def test_find_or_create_by_api_data_creates_a_user_with_original_name user_data = { ident: '37710100070', first_name: 'John', @@ -24,6 +24,25 @@ class RegistrantUserCreationTest < ActiveSupport::TestCase RegistrantUser.find_or_create_by_api_data(user_data) user = User.find_by(registrant_ident: 'EE-37710100070') - assert_equal('JOHN SMITH', user.username) + assert_equal('John Smith', user.username) + end + + def test_updates_related_contacts_name_if_differs_from_e_identity + contact = contacts(:john) + contact.update(ident: '39708290276', ident_country_code: 'EE') + + user_data = { + ident: '39708290276', + first_name: 'John', + last_name: 'Doe' + } + + RegistrantUser.find_or_create_by_api_data(user_data) + + user = User.find_by(registrant_ident: 'EE-39708290276') + assert_equal('John Doe', user.username) + + contact.reload + assert_equal user.username, contact.name end end From 2f259982da9e1d22a80305c37e80fd529bf59ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 30 Oct 2020 11:51:20 +0200 Subject: [PATCH 4/6] Find related contacts via ident and ident_country_code --- app/models/registrant_user.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 420446582..f1e0e1c2d 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -55,7 +55,8 @@ class RegistrantUser < User end def update_related_contacts - contacts = Contact.where(ident: ident, country_code: country_code) + cc, idcode = registrant_ident.split('-') + contacts = Contact.where(ident: idcode, ident_country_code: cc) .where('UPPER(name) != UPPER(?)', username) contacts.each { |c| c.update(name: username) } From 89154c4fe200482caa1f868802b8bdf642b13cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 30 Oct 2020 11:56:07 +0200 Subject: [PATCH 5/6] Use predefined methods to determine country and ident --- app/models/registrant_user.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index f1e0e1c2d..089292917 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -55,8 +55,7 @@ class RegistrantUser < User end def update_related_contacts - cc, idcode = registrant_ident.split('-') - contacts = Contact.where(ident: idcode, ident_country_code: cc) + contacts = Contact.where(ident: ident, ident_country_code: country.alpha2) .where('UPPER(name) != UPPER(?)', username) contacts.each { |c| c.update(name: username) } From 3444c4e021e007ad75e384f64cb5c0922ec39cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 3 Nov 2020 16:14:01 +0200 Subject: [PATCH 6/6] Add registrar poll message after updateing registrant contact(s) names --- app/models/registrant_user.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 089292917..aa41ccff8 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -58,7 +58,11 @@ class RegistrantUser < User contacts = Contact.where(ident: ident, ident_country_code: country.alpha2) .where('UPPER(name) != UPPER(?)', username) - contacts.each { |c| c.update(name: username) } + contacts.each do |contact| + contact.update(name: username) + action = actions.create!(contact: contact, operation: :update) + contact.registrar.notify(action) + end end class << self