From 303150315d567d6f7debc9e96473a02d33157549 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 25 Oct 2021 14:36:16 +0300 Subject: [PATCH 1/9] added logger for display company register --- app/controllers/api/v1/registrant/contacts_controller.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 30096ab8a..2dda1619c 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -102,8 +102,16 @@ module Api ident_type: 'priv', ident_country_code: country) return contact if contact + company_register = CompanyRegister::Client.new + res = company_register.representation_rights(citizen_personal_code: company_codes, + citizen_country_code: current_registrant_user.country.alpha3) + p "++++++++++++++++" + p res + p "+++++++++++++++++" + Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) + rescue CompanyRegister::NotAvailableError nil end From 5e056ed954045e595b8a32ffd3c4a98743b64c9c Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 25 Oct 2021 15:15:28 +0300 Subject: [PATCH 2/9] added another breakpoints --- .../api/v1/registrant/contacts_controller.rb | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 2dda1619c..e1ed02181 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -102,12 +102,46 @@ module Api ident_type: 'priv', ident_country_code: country) return contact if contact - company_register = CompanyRegister::Client.new - res = company_register.representation_rights(citizen_personal_code: company_codes, - citizen_country_code: current_registrant_user.country.alpha3) - p "++++++++++++++++" - p res + # company_register = CompanyRegister::Client.new + # res = company_register.representation_rights(citizen_personal_code: company_codes, + # citizen_country_code: current_registrant_user.country.alpha3) + # p "++++++++++++++++" + # p res + # p "+++++++++++++++++" + # res.each do |r| + # unless contact.name == r.company_ame + # contact.update(name: r.company_name) + # end + # p "+++++++++++++++++" + p "+++++++++++++++++" + p current_registrant_user.companies + p "+++++++++++++++++" + p "+++++++++++++++++" + p "+++++++++++++++++" + p company_codes + p "+++++++++++++++++" + p "+++++++++++++++++" + p "+++++++++++++++++" + + + current_registrant_user.companies.each do |i| + c = Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, + ident_country_code: country) + + p "+++++++++++++++++" + p "+++++++++++++++++" + p c.org_name + p "+++++++++++++++++" + p "+++++++++++++++++" + p i.company_name + p "+++++++++++++++++" + p "+++++++++++++++++" + + unless c.org_name == i.company_name + c.update(org_name: company_name) + end + end Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) From fccac285acf74ea26e588d38be73f2bc2eefd47c Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 25 Oct 2021 15:26:17 +0300 Subject: [PATCH 3/9] added another breakpoints --- .../api/v1/registrant/contacts_controller.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index e1ed02181..9cd6f0a1c 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -126,21 +126,22 @@ module Api current_registrant_user.companies.each do |i| - c = Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, - ident_country_code: country) + # c = Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, + # ident_country_code: country) p "+++++++++++++++++" p "+++++++++++++++++" - p c.org_name + # p c.org_name p "+++++++++++++++++" p "+++++++++++++++++" + p i.companies p i.company_name p "+++++++++++++++++" p "+++++++++++++++++" - unless c.org_name == i.company_name - c.update(org_name: company_name) - end + # unless c.org_name == i.company_name + # c.update(org_name: company_name) + # end end Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, From b0ca5ddec084288057ca423b832824e83f2463a6 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 25 Oct 2021 15:35:00 +0300 Subject: [PATCH 4/9] added another breakpoints --- .../api/v1/registrant/contacts_controller.rb | 42 ------------------- app/models/registrant_user.rb | 3 +- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 9cd6f0a1c..0e4ca81c2 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -102,48 +102,6 @@ module Api ident_type: 'priv', ident_country_code: country) return contact if contact - # company_register = CompanyRegister::Client.new - # res = company_register.representation_rights(citizen_personal_code: company_codes, - # citizen_country_code: current_registrant_user.country.alpha3) - # p "++++++++++++++++" - # p res - # p "+++++++++++++++++" - # res.each do |r| - # unless contact.name == r.company_ame - # contact.update(name: r.company_name) - # end - # - p "+++++++++++++++++" - p "+++++++++++++++++" - p current_registrant_user.companies - p "+++++++++++++++++" - p "+++++++++++++++++" - p "+++++++++++++++++" - p company_codes - p "+++++++++++++++++" - p "+++++++++++++++++" - p "+++++++++++++++++" - - - current_registrant_user.companies.each do |i| - # c = Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, - # ident_country_code: country) - - p "+++++++++++++++++" - p "+++++++++++++++++" - # p c.org_name - p "+++++++++++++++++" - p "+++++++++++++++++" - p i.companies - p i.company_name - p "+++++++++++++++++" - p "+++++++++++++++++" - - # unless c.org_name == i.company_name - # c.update(org_name: company_name) - # end - end - Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index f7e85c5af..c174f06ad 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -20,7 +20,8 @@ class RegistrantUser < User def companies(company_register = CompanyRegister::Client.new) return [] if ident.include?('-') - company_register.representation_rights(citizen_personal_code: ident, + p "++++++++++++++++++++++++++++++++++++++++" + p company_register.representation_rights(citizen_personal_code: ident, citizen_country_code: country.alpha3) end From b0ed6167f59fb4088c32068b3d6e0ab04b8076d2 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 25 Oct 2021 16:15:10 +0300 Subject: [PATCH 5/9] added updated method --- .../api/v1/registrant/contacts_controller.rb | 16 ++++++++++++++++ app/models/registrant_user.rb | 3 +-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 0e4ca81c2..b8e9d2b43 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -102,6 +102,7 @@ module Api ident_type: 'priv', ident_country_code: country) return contact if contact + update_contacts_before_receive Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) @@ -109,6 +110,21 @@ module Api nil end + def update_contacts_before_receive + company_register = CompanyRegister::Client.new + companies = company_register.representation_rights(citizen_personal_code: current_registrant_user.ident, + citizen_country_code: current_registrant_user.country.alpha3) + + companies.each do |c| + contact = Contact.find_by(ident: c.registration_number) + break if contact.blank? + contact.update(name: c.company_name) unless c.company_name == contact.name + end + + rescue CompanyRegister::NotAvailableError + nil + end + def company_codes current_registrant_user.companies.collect(&:registration_number) end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index c174f06ad..f7e85c5af 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -20,8 +20,7 @@ class RegistrantUser < User def companies(company_register = CompanyRegister::Client.new) return [] if ident.include?('-') - p "++++++++++++++++++++++++++++++++++++++++" - p company_register.representation_rights(citizen_personal_code: ident, + company_register.representation_rights(citizen_personal_code: ident, citizen_country_code: country.alpha3) end From e9d7b401367cac48fb8f9f94e7cfc231b2394e9b Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Wed, 27 Oct 2021 15:27:41 +0300 Subject: [PATCH 6/9] added tests --- .../api/v1/registrant/contacts_controller.rb | 16 ----------- app/models/registrant_user.rb | 28 ++++++++++++++++++- test/models/registrant_user_test.rb | 26 +++++++++++++++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index b8e9d2b43..0e4ca81c2 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -102,7 +102,6 @@ module Api ident_type: 'priv', ident_country_code: country) return contact if contact - update_contacts_before_receive Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) @@ -110,21 +109,6 @@ module Api nil end - def update_contacts_before_receive - company_register = CompanyRegister::Client.new - companies = company_register.representation_rights(citizen_personal_code: current_registrant_user.ident, - citizen_country_code: current_registrant_user.country.alpha3) - - companies.each do |c| - contact = Contact.find_by(ident: c.registration_number) - break if contact.blank? - contact.update(name: c.company_name) unless c.company_name == contact.name - end - - rescue CompanyRegister::NotAvailableError - nil - end - def company_codes current_registrant_user.companies.collect(&:registration_number) end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index f7e85c5af..a907d98e5 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -20,8 +20,34 @@ class RegistrantUser < User def companies(company_register = CompanyRegister::Client.new) return [] if ident.include?('-') - company_register.representation_rights(citizen_personal_code: ident, + companies = company_register.representation_rights(citizen_personal_code: ident, citizen_country_code: country.alpha3) + update_contacts_before_receive(companies) + companies + end + + def update_contacts_before_receive(companies) + return if companies.blank? + + companies.each do |c| + contact = Contact.find_by(ident: c.registration_number, ident_country_code: "EE") + break if contact.blank? + + unless c.company_name == contact.name + old_contact_name = contact.name + contact.update(name: c.company_name) + notify_registrar_data_updated(company_name: c.company_name, old_contact_name: old_contact_name, contact: contact) + end + end + + rescue CompanyRegister::NotAvailableError + nil + end + + def notify_registrar_data_updated(company_name:, old_contact_name:, contact:) + contact.registrar.notifications.create!( + text: "#{old_contact_name} was changed to #{company_name}" + ) end def contacts(representable: true) diff --git a/test/models/registrant_user_test.rb b/test/models/registrant_user_test.rb index c61f095f2..47adcda68 100644 --- a/test/models/registrant_user_test.rb +++ b/test/models/registrant_user_test.rb @@ -1,6 +1,8 @@ require 'test_helper' class RegistrantUserTest < ActiveSupport::TestCase + Company = Struct.new(:registration_number, :company_name) + def setup super @@ -30,14 +32,34 @@ class RegistrantUserTest < ActiveSupport::TestCase assert_equal Country.new('US'), user.country end + def test_should_update_org_contact_if_data_from_business_registry_dismatch + assert_equal 'US-1234', @user.registrant_ident + org = contacts(:acme_ltd) + org.ident_country_code = 'EE' + org.save(validate: false) + org.reload + + company = Company.new(org.ident, "ace") + + company_register = Minitest::Mock.new + company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', + citizen_country_code: 'USA' }]) + assert_equal [company], @user.companies(company_register) + company_register.verify + + org.reload + end + def test_queries_company_register_for_associated_companies assert_equal 'US-1234', @user.registrant_ident + company = Company.new("acme", "ace") + company_register = Minitest::Mock.new - company_register.expect(:representation_rights, %w[acme ace], [{ citizen_personal_code: '1234', + company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', citizen_country_code: 'USA' }]) - assert_equal %w[acme ace], @user.companies(company_register) + assert_equal [company], @user.companies(company_register) company_register.verify end From ca231620f1cbca51b1369b1f0d2ec9b61a0f14f6 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Wed, 27 Oct 2021 15:46:30 +0300 Subject: [PATCH 7/9] refactoring --- .../api/v1/registrant/contacts_controller.rb | 1 - app/models/registrant_user.rb | 41 +++++++++++++++---- test/models/registrant_user_test.rb | 6 +-- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 0e4ca81c2..30096ab8a 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -104,7 +104,6 @@ module Api Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) - rescue CompanyRegister::NotAvailableError nil end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index a907d98e5..7a6e6dbe4 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -21,29 +21,54 @@ class RegistrantUser < User return [] if ident.include?('-') companies = company_register.representation_rights(citizen_personal_code: ident, - citizen_country_code: country.alpha3) - update_contacts_before_receive(companies) + citizen_country_code: country.alpha3) + + p "++++++++++++++++++++" + log("+++++++++++++") + log(companies) + log("+++++++++++++") + p companies + p "+++++++++++++++++++++" + companies = update_contacts_before_receive(companies) companies end + def log(msg) + @log ||= Logger.new($stdout) + @log.info(msg) + end + def update_contacts_before_receive(companies) return if companies.blank? companies.each do |c| - contact = Contact.find_by(ident: c.registration_number, ident_country_code: "EE") - break if contact.blank? + # contact = Contact.find_by(ident: c.registration_number, ident_country_code: 'EE') + contacts = Contact.where(ident: c.registration_number, ident_country_code: 'EE') - unless c.company_name == contact.name - old_contact_name = contact.name - contact.update(name: c.company_name) - notify_registrar_data_updated(company_name: c.company_name, old_contact_name: old_contact_name, contact: contact) + break if contacts.blank? + + contacts.each do |co| + next if c.company_name == co.name + + update_company_name(contact: co, company: c) end end + return companies rescue CompanyRegister::NotAvailableError nil 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: "#{old_contact_name} was changed to #{company_name}" diff --git a/test/models/registrant_user_test.rb b/test/models/registrant_user_test.rb index 47adcda68..987e80c03 100644 --- a/test/models/registrant_user_test.rb +++ b/test/models/registrant_user_test.rb @@ -44,10 +44,10 @@ class RegistrantUserTest < ActiveSupport::TestCase company_register = Minitest::Mock.new company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', citizen_country_code: 'USA' }]) - assert_equal [company], @user.companies(company_register) - company_register.verify - + @user.companies(company_register) org.reload + + assert_equal org.name, company.company_name end def test_queries_company_register_for_associated_companies From 042d8d5543e4cc79123417fcd40c5cf2311308a5 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 8 Nov 2021 10:39:10 +0200 Subject: [PATCH 8/9] set breakpoint --- app/models/registrant_user.rb | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 7a6e6dbe4..736fe538e 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -23,34 +23,22 @@ class RegistrantUser < User companies = company_register.representation_rights(citizen_personal_code: ident, citizen_country_code: country.alpha3) - p "++++++++++++++++++++" - log("+++++++++++++") - log(companies) - log("+++++++++++++") - p companies - p "+++++++++++++++++++++" companies = update_contacts_before_receive(companies) companies end - def log(msg) - @log ||= Logger.new($stdout) - @log.info(msg) - end - def update_contacts_before_receive(companies) return if companies.blank? - companies.each do |c| - # contact = Contact.find_by(ident: c.registration_number, ident_country_code: 'EE') - contacts = Contact.where(ident: c.registration_number, ident_country_code: 'EE') + companies.each do |company| + contacts = Contact.where(ident: company.registration_number, ident_country_code: 'EE') - break if contacts.blank? + next if contacts.blank? - contacts.each do |co| - next if c.company_name == co.name + contacts.each do |contact| + next if company.company_name == contact.name - update_company_name(contact: co, company: c) + update_company_name(contact: contact, company: company) end end @@ -62,6 +50,7 @@ class RegistrantUser < User 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, From cddba821147ed6d068ba654782e6c74e64960f23 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 8 Nov 2021 15:59:19 +0200 Subject: [PATCH 9/9] updated registrar notify --- app/models/registrant_user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 736fe538e..74a8ad0e5 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -60,7 +60,7 @@ class RegistrantUser < User def notify_registrar_data_updated(company_name:, old_contact_name:, contact:) contact.registrar.notifications.create!( - text: "#{old_contact_name} was changed to #{company_name}" + text: "Contact update: #{contact.id} name updated from #{old_contact_name} to #{company_name} by the registry" ) end