Add phone number validation for Estonian organization contacts

- Add OrgRegistrantPhoneCheckerJob execution for contact creation and update
- Rename validate_* methods to maybe_validate_* for consistency
- Add tests for phone number validation in REPP API
- Fix method naming and add proper job arguments

This ensures phone numbers are validated against company register data
for Estonian organization contacts after creation or update.
This commit is contained in:
oleghasjanov 2025-01-30 12:49:20 +02:00
parent 429e2644d6
commit 803276d8c8
6 changed files with 73 additions and 8 deletions

View file

@ -94,7 +94,6 @@ module Repp
action = Actions::ContactUpdate.new(@contact, contact_params_with_address(required: false), action = Actions::ContactUpdate.new(@contact, contact_params_with_address(required: false),
contact_params[:legal_document], contact_params[:legal_document],
contact_ident_params(required: false), current_user) contact_ident_params(required: false), current_user)
unless action.call unless action.call
handle_errors(@contact) handle_errors(@contact)
return return

View file

@ -12,11 +12,12 @@ module Actions
def call def call
maybe_remove_address maybe_remove_address
maybe_attach_legal_doc maybe_attach_legal_doc
validate_ident maybe_validate_ident
maybe_change_email maybe_change_email
# maybe_company_is_relevant # maybe_company_is_relevant
commit commit
validate_contact maybe_validate_phone_number
maybe_validate_contact
end end
def maybe_change_email def maybe_change_email
@ -45,7 +46,7 @@ module Actions
contact.country_code = nil contact.country_code = nil
end end
def validate_ident def maybe_validate_ident
validate_ident_integrity validate_ident_integrity
validate_ident_birthday validate_ident_birthday
@ -102,12 +103,16 @@ module Actions
contact.save contact.save
end end
def validate_contact def maybe_validate_contact
return if @error || !contact.valid? return if @error || !contact.valid?
[:regex, :mx].each do |m| [:regex, :mx].each do |m|
contact.verify_email(check_level: m, single_email: true) contact.verify_email(check_level: m, single_email: true)
end end
end end
def maybe_validate_phone_number
OrgRegistrantPhoneCheckerJob.perform_later(type: 'single', registrant_user_code: contact.code)
end
end end
end end

View file

@ -18,7 +18,8 @@ module Actions
maybe_change_email if new_attributes[:email].present? maybe_change_email if new_attributes[:email].present?
maybe_filtering_old_failed_records maybe_filtering_old_failed_records
commit commit
validate_contact maybe_validate_phone_number
maybe_validate_contact
end end
def maybe_change_email def maybe_change_email
@ -125,7 +126,7 @@ module Actions
updated updated
end end
def validate_contact def maybe_validate_contact
return if @error || !contact.valid? return if @error || !contact.valid?
[:regex, :mx].each do |m| [:regex, :mx].each do |m|
@ -134,5 +135,9 @@ module Actions
@contact.remove_force_delete_for_valid_contact @contact.remove_force_delete_for_valid_contact
end end
def maybe_validate_phone_number
OrgRegistrantPhoneCheckerJob.perform_later(type: 'single', registrant_user_code: contact.code)
end
end end
end end

View file

@ -1,7 +1,8 @@
class OrgRegistrantPhoneCheckerJob < ApplicationJob class OrgRegistrantPhoneCheckerJob < ApplicationJob
queue_as :default 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 case type
when 'bulk' when 'bulk'
execute_bulk_checker(spam_delay) execute_bulk_checker(spam_delay)

View file

@ -253,4 +253,32 @@ class ReppV1ContactsCreateTest < ActionDispatch::IntegrationTest
CompanyRegister::Client.define_singleton_method(:new, original_new_method) CompanyRegister::Client.define_singleton_method(:new, original_new_method)
end 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 end

View file

@ -142,4 +142,31 @@ class ReppV1ContactsUpdateTest < ActionDispatch::IntegrationTest
ENV["shunter_default_threshold"] = '10000' ENV["shunter_default_threshold"] = '10000'
ENV["shunter_enabled"] = 'false' ENV["shunter_enabled"] = 'false'
end 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 end