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),
contact_params[:legal_document],
contact_ident_params(required: false), current_user)
unless action.call
handle_errors(@contact)
return

View file

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

View file

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

View file

@ -2,6 +2,7 @@ class OrgRegistrantPhoneCheckerJob < ApplicationJob
queue_as :default
def perform(type: 'bulk', registrant_user_code: nil, spam_delay: 1)
puts '??? PERFROMED ???'
case type
when 'bulk'
execute_bulk_checker(spam_delay)

View file

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

View file

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