Skip to content

Commit

Permalink
[API-42282] Improve error handling and logging for POA dependent assi…
Browse files Browse the repository at this point in the history
…gnment (#19541)

* add poa id to logs

* raise error if manage_ptcpnt_rlnshp fails

* raise more meaningful exceptions

* improve error handling re dependent claims

* add more detail to bgs error
  • Loading branch information
tycol7 authored Nov 25, 2024
1 parent 13e4739 commit 7f26785
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module ClaimsApi
class DependentClaimantPoaAssignmentService
def initialize(**options)
@poa_id = options[:poa_id]
@poa_code = options[:poa_code]
@veteran_participant_id = options[:veteran_participant_id]
@dependent_participant_id = options[:dependent_participant_id]
Expand All @@ -25,7 +26,7 @@ def assign_poa_to_dependent!

log(level: :error, detail: 'Failed to assign POA to dependent')

raise ::Common::Exceptions::FailedDependency
raise ::Common::Exceptions::ServiceError
end

private
Expand All @@ -36,7 +37,8 @@ def person_web_service
end

def log(level: :info, **rest)
ClaimsApi::Logger.log('dependent_claimant_poa_assignment_service', level:, poa_code: @poa_code, **rest)
ClaimsApi::Logger.log('dependent_claimant_poa_assignment_service', level:, poa_id: @poa_id, poa_code: @poa_code,
**rest)
end

def assign_poa_to_dependent_via_manage_ptcpnt_rlnshp?
Expand All @@ -51,19 +53,28 @@ def assign_poa_to_dependent_via_manage_ptcpnt_rlnshp?
return true
end

log(level: :warn,
detail: 'Something else went wrong with manage_ptcpnt_rlnshp. Falling back to update_benefit_claim.')

false
raise ::Common::Exceptions::ServiceError
rescue ::Common::Exceptions::ServiceError => e
if e.errors.first.detail == 'PtcpntIdA has open claims.'
log(detail: 'Dependent has open claims, continuing.')
else
log(level: :warn,
detail: 'Something else went wrong with manage_ptcpnt_rlnshp. Falling back to update_benefit_claim.')

return false
end

false
raise e
rescue => e
log(level: :error, detail: 'Something else went wrong with manage_ptcpnt_rlnshp.', error: error_details(e))

raise e
end

def error_details(e)
{
message: e.message,
detail: e.try(:detail),
details: e.try(:details),
errors: e.try(:errors)&.map(&:to_h)
}.compact
end

def iso_to_date(iso_date)
Expand Down Expand Up @@ -92,6 +103,12 @@ def assign_poa_to_dependent_via_update_benefit_claim?
first_open_claim = dependent_claims.find do |claim|
claim[:phase_type] != 'Complete' && claim[:ptcpnt_vet_id] == @veteran_participant_id
end
if first_open_claim.nil? || first_open_claim.blank?
log(detail: 'Dependent has no open claims.', statuses: dependent_claims.pluck(:phase_type).uniq)

raise ::Common::Exceptions::ServiceError
end

first_open_claim_details = claim_details(first_open_claim[:benefit_claim_id])

benefit_claim_update_input = build_benefit_claim_update_input(claim_details: first_open_claim_details)
Expand All @@ -111,12 +128,13 @@ def dependent_claims
local_bgs = ClaimsApi::LocalBGS.new(external_uid: @dependent_participant_id,
external_key: @dependent_participant_id)
res = local_bgs.find_benefit_claims_status_by_ptcpnt_id(@dependent_participant_id)
benefit_claims = Array.wrap(res&.dig(:benefit_claims_dto, :benefit_claim))

return res&.dig(:benefit_claims_dto, :benefit_claim) if res&.dig(:benefit_claims_dto, :benefit_claim).present?
return benefit_claims if benefit_claims.present? && benefit_claims.is_a?(Array) && benefit_claims.first.present?

log(level: :error, detail: 'Dependent claims not found in BGS')

raise ::Common::Exceptions::FailedDependency
raise ::Common::Exceptions::ResourceNotFound
end

def benefit_claim_web_service
Expand All @@ -136,7 +154,7 @@ def claim_details(claim_id)

log(level: :error, detail: 'Claim details not found in BGS', claim_id:)

raise ::Common::Exceptions::FailedDependency
raise ::Common::Exceptions::ResourceNotFound
end

def poa_participant_id
Expand All @@ -146,7 +164,7 @@ def poa_participant_id

log(level: :error, detail: 'POA code/participant ID combo not found in BGS')

raise ::Common::Exceptions::FailedDependency
raise ::Common::Exceptions::ResourceNotFound
end

def manage_ptcpnt_rlnshp_poa_success?(response)
Expand All @@ -162,7 +180,7 @@ def benefit_claim_type(pgm_type_cd)
else
log(level: :error, detail: 'Program type code not recognized', pgm_type_cd:)

raise ::Common::Exceptions::FailedDependency
raise ::Common::Exceptions::BadRequest
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def perform(poa_id)
poa = ClaimsApi::PowerOfAttorney.find(poa_id)

service = dependent_claimant_poa_assignment_service(
poa.id,
poa.form_data,
poa.auth_headers
)
Expand Down Expand Up @@ -42,8 +43,9 @@ def handle_error(poa, e)
raise e
end

def dependent_claimant_poa_assignment_service(data, auth_headers)
def dependent_claimant_poa_assignment_service(poa_id, data, auth_headers)
ClaimsApi::DependentClaimantPoaAssignmentService.new(
poa_id:,
poa_code: find_poa_code(data),
veteran_participant_id: auth_headers['va_eauth_pid'],
dependent_participant_id: auth_headers.dig('dependent', 'participant_id'),
Expand Down

0 comments on commit 7f26785

Please sign in to comment.