Skip to content

Commit

Permalink
refactor to align with existing validation pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tycol7 committed Oct 7, 2024
1 parent 0e1503c commit f65aeb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ def shared_form_validation(form_number)
base = form_number == '2122' ? 'serviceOrganization' : 'representative'
poa_code = form_attributes.dig(base, 'poaCode')

@claims_api_forms_validation_errors = validate_form_2122_and_2122a_submission_values(user_profile) || []
@claims_api_forms_validation_errors = validate_form_2122_and_2122a_submission_values(
user_profile:, veteran_participant_id: target_veteran.participant_id, poa_code:, base:
)

validate_json_schema(form_number.upcase)
@rep_id = validate_registration_number!(base, poa_code)

dependent_claimant_errors = validate_dependent_claimant(veteran_participant_id: target_veteran.participant_id,
user_profile:, poa_code:, base:)
@claims_api_forms_validation_errors.concat(dependent_claimant_errors) if dependent_claimant_errors.any?

add_claimant_data_to_form if user_profile

if @claims_api_forms_validation_errors.any?
if @claims_api_forms_validation_errors
raise ::ClaimsApi::Common::Exceptions::Lighthouse::JsonFormValidationError,
@claims_api_forms_validation_errors
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def request_representative
target_veteran

poa_code = form_attributes.dig('poa', 'poaCode')
@claims_api_forms_validation_errors = validate_form_2122_and_2122a_submission_values(user_profile)
@claims_api_forms_validation_errors = validate_form_2122_and_2122a_submission_values(user_profile:)

validate_json_schema(FORM_NUMBER)
validate_accredited_representative(form_attributes.dig('poa', 'registrationNumber'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@
module ClaimsApi
module V2
module PowerOfAttorneyValidation
def validate_form_2122_and_2122a_submission_values(user_profile)
def validate_form_2122_and_2122a_submission_values(user_profile:, veteran_participant_id: nil, poa_code: nil,
base: nil)
validate_claimant_fields(user_profile)
if [veteran_participant_id, user_profile, poa_code, base].all?(&:present?)
validate_dependent_claimant(veteran_participant_id:, user_profile:, poa_code:, base:)
end

# collect errors and pass back to the controller
raise_error_collection if @errors
end

def validate_dependent_claimant(veteran_participant_id:, user_profile:, poa_code:, base:)
return [] unless feature_enabled_and_claimant_present?

errors = []

service = build_dependent_claimant_verification_service(veteran_participant_id:, user_profile:, poa_code:)

validate_claimant(service:, errors:, base:)

errors
end

private

def feature_enabled_and_claimant_present?
Flipper.enabled?(:lighthouse_claims_api_poa_dependent_claimants) && form_attributes['claimant'].present?
end

def validate_dependent_claimant(veteran_participant_id:, user_profile:, poa_code:, base:)
return nil unless feature_enabled_and_claimant_present?

service = build_dependent_claimant_verification_service(veteran_participant_id:, user_profile:,
poa_code:)

validate_claimant(service:, base:)
end

def build_dependent_claimant_verification_service(veteran_participant_id:, user_profile:, poa_code:)
claimant = user_profile.profile

Expand All @@ -39,34 +41,27 @@ def build_dependent_claimant_verification_service(veteran_participant_id:, user_
poa_code:)
end

def validate_claimant(service:, errors:, base:)
validate_poa_code(service:, errors:, base:)
validate_dependent(service:, errors:)
def validate_claimant(service:, base:)
validate_poa_code(service:, base:)
validate_dependent(service:)
end

def validate_poa_code(service:, errors:, base:)
def validate_poa_code(service:, base:)
service.validate_poa_code_exists!
rescue ::Common::Exceptions::UnprocessableEntity
errors << {
collect_error_messages(
source: "/#{base}/poaCode",
detail: ClaimsApi::DependentClaimantVerificationService::POA_CODE_NOT_FOUND_ERROR_MESSAGE
}
)
end

def validate_dependent(service:, errors:)
def validate_dependent(service:)
service.validate_dependent_by_participant_id!
rescue ::Common::Exceptions::UnprocessableEntity
errors << {
collect_error_messages(
source: '/claimant/claimantId',
detail: ClaimsApi::DependentClaimantVerificationService::CLAIMANT_NOT_A_DEPENDENT_ERROR_MESSAGE
}
end

def build_claimant_data(service:)
{
participant_id: service.claimant_participant_id,
ssn: service.claimant_ssn
}
)
end

def validate_claimant_fields(user_profile)
Expand Down

0 comments on commit f65aeb4

Please sign in to comment.