diff --git a/app/controllers/repp/v1/contacts_controller.rb b/app/controllers/repp/v1/contacts_controller.rb index 7b60d8b23..be8f14115 100644 --- a/app/controllers/repp/v1/contacts_controller.rb +++ b/app/controllers/repp/v1/contacts_controller.rb @@ -94,7 +94,6 @@ module Repp action = Actions::ContactUpdate.new(@contact, contact_params_with_address(required: false), contact_params[:legal_document], contact_ident_params(required: false), current_user) - unless action.call handle_errors(@contact) return diff --git a/app/interactions/actions/contact_create.rb b/app/interactions/actions/contact_create.rb index 39fb3212d..b9d1d6596 100644 --- a/app/interactions/actions/contact_create.rb +++ b/app/interactions/actions/contact_create.rb @@ -12,11 +12,12 @@ module Actions def call maybe_remove_address maybe_attach_legal_doc - validate_ident + maybe_validate_ident maybe_change_email # maybe_company_is_relevant commit - validate_contact + maybe_validate_phone_number + maybe_validate_contact end def maybe_change_email @@ -45,7 +46,7 @@ module Actions contact.country_code = nil end - def validate_ident + def maybe_validate_ident validate_ident_integrity validate_ident_birthday @@ -102,12 +103,16 @@ module Actions contact.save end - def validate_contact + def maybe_validate_contact return if @error || !contact.valid? [:regex, :mx].each do |m| contact.verify_email(check_level: m, single_email: true) end end + + def maybe_validate_phone_number + OrgRegistrantPhoneCheckerJob.perform_later(type: 'single', registrant_user_code: contact.code) + end end end diff --git a/app/interactions/actions/contact_update.rb b/app/interactions/actions/contact_update.rb index a37f81b60..6831c449b 100644 --- a/app/interactions/actions/contact_update.rb +++ b/app/interactions/actions/contact_update.rb @@ -18,7 +18,8 @@ module Actions maybe_change_email if new_attributes[:email].present? maybe_filtering_old_failed_records commit - validate_contact + maybe_validate_phone_number + maybe_validate_contact end def maybe_change_email @@ -125,7 +126,7 @@ module Actions updated end - def validate_contact + def maybe_validate_contact return if @error || !contact.valid? [:regex, :mx].each do |m| @@ -134,5 +135,9 @@ module Actions @contact.remove_force_delete_for_valid_contact end + + def maybe_validate_phone_number + OrgRegistrantPhoneCheckerJob.perform_later(type: 'single', registrant_user_code: contact.code) + end end end diff --git a/app/jobs/org_registrant_phone_checker_job.rb b/app/jobs/org_registrant_phone_checker_job.rb index 642690137..84456e992 100644 --- a/app/jobs/org_registrant_phone_checker_job.rb +++ b/app/jobs/org_registrant_phone_checker_job.rb @@ -1,7 +1,8 @@ class OrgRegistrantPhoneCheckerJob < ApplicationJob queue_as :default - def perform(type: 'bulk', registrant_user_code: nil, spam_delay: 1 ) + def perform(type: 'bulk', registrant_user_code: nil, spam_delay: 1) + puts '??? PERFROMED ???' case type when 'bulk' execute_bulk_checker(spam_delay) diff --git a/test/integration/repp/v1/contacts/create_test.rb b/test/integration/repp/v1/contacts/create_test.rb index e47b96265..bcb196f1a 100644 --- a/test/integration/repp/v1/contacts/create_test.rb +++ b/test/integration/repp/v1/contacts/create_test.rb @@ -253,4 +253,32 @@ class ReppV1ContactsCreateTest < ActionDispatch::IntegrationTest CompanyRegister::Client.define_singleton_method(:new, original_new_method) end + + def test_validates_phone_number_after_create + request_body = { + contact: { + name: 'Test Company', + phone: '+372.51111112', + email: 'test@company.com', + ident: { + ident_type: 'org', + ident_country_code: 'EE', + ident: '12345678', + }, + }, + } + + assert_enqueued_with(job: OrgRegistrantPhoneCheckerJob) do + post '/repp/v1/contacts', headers: @auth_headers, params: request_body + end + + assert_response :ok + json = JSON.parse(response.body, symbolize_names: true) + assert_equal 1000, json[:code] + assert_equal 'Command completed successfully', json[:message] + + contact = Contact.find_by(code: json[:data][:contact][:code]) + assert contact.present? + assert_equal '+372.51111112', contact.phone + end end diff --git a/test/integration/repp/v1/contacts/update_test.rb b/test/integration/repp/v1/contacts/update_test.rb index d51602c32..f078c4bf3 100644 --- a/test/integration/repp/v1/contacts/update_test.rb +++ b/test/integration/repp/v1/contacts/update_test.rb @@ -142,4 +142,31 @@ class ReppV1ContactsUpdateTest < ActionDispatch::IntegrationTest ENV["shunter_default_threshold"] = '10000' ENV["shunter_enabled"] = 'false' end + + def test_validates_phone_number_after_update + @contact.update!( + phone: '+372.555666777', + ident_type: 'org', + ident_country_code: 'EE', + ident: '12345678' + ) + + request_body = { + contact: { + phone: '+372.123456789' + } + } + + assert_enqueued_with(job: OrgRegistrantPhoneCheckerJob) do + put "/repp/v1/contacts/#{@contact.code}", headers: @auth_headers, params: request_body + end + + assert_response :ok + json = JSON.parse(response.body, symbolize_names: true) + assert_equal 1000, json[:code] + assert_equal 'Command completed successfully', json[:message] + + @contact.reload + assert_equal '+372.123456789', @contact.phone + end end