-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PowerOfAttorneyFormSubmissionJob (#101919)
- Loading branch information
Showing
9 changed files
with
316 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/sidekiq/accredited_representative_portal/power_of_attorney_form_submission_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'sentry_logging' | ||
|
||
module AccreditedRepresentativePortal | ||
class PowerOfAttorneyFormSubmissionJob | ||
include Sidekiq::Job | ||
include SentryLogging | ||
|
||
sidekiq_options retry: 2 | ||
|
||
attr_reader :response | ||
|
||
def perform(poa_form_submission_id) | ||
@id = poa_form_submission_id | ||
service = BenefitsClaims::Service.new(poa_form_submission.power_of_attorney_request.claimant.icn) | ||
@response = service.get_2122_submission(poa_form_submission.service_id) | ||
poa_form_submission.update( | ||
status: (non_error_response? ? :succeeded : :failed), | ||
service_response: response.to_json, | ||
status_updated_at: DateTime.current, | ||
error_message: error_data.to_json | ||
) | ||
rescue => e | ||
handle_errors(e, poa_form_submission) | ||
end | ||
|
||
sidekiq_retries_exhausted do |job, _ex| | ||
poa_form_submission_id = job['args'].first | ||
poa_form_submission = PowerOfAttorneyFormSubmission.find(poa_form_submission_id) | ||
poa_form_submission.update(status: :failed, status_updated_at: DateTime.current) | ||
end | ||
|
||
def handle_errors(e, poa_form_submission) | ||
log_exception_to_sentry(e) | ||
poa_form_submission.update(error_message: e.message, status: :enqueue_failed) | ||
raise e | ||
end | ||
|
||
def non_error_response? | ||
response.dig('data', 'attributes', 'status') != 'errored' | ||
end | ||
|
||
def error_data | ||
response.dig('data', 'attributes', 'errors') | ||
end | ||
|
||
def poa_form_submission | ||
@poa_form_submission ||= PowerOfAttorneyFormSubmission.find(@id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
modules/accredited_representative_portal/spec/factories/power_of_attorney_form_submission.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :power_of_attorney_form_submission, | ||
class: 'AccreditedRepresentativePortal::PowerOfAttorneyFormSubmission' do | ||
association :power_of_attorney_request | ||
service_id { SecureRandom.uuid } | ||
service_response { '{}' } | ||
status { :enqueue_succeeded } | ||
status_updated_at { DateTime.now } | ||
end | ||
end |
45 changes: 45 additions & 0 deletions
45
...credited_representative_portal/spec/fixtures/power_of_attorney_form_submission/error.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"data": { | ||
"id": "a4dd5bb0-d47b-4ca9-9afc-d7ce6a820985", | ||
"type": "claimsApiPowerOfAttorneys", | ||
"attributes": { | ||
"dateRequestAccepted": "2025-01-16", | ||
"previousPoa": "-", | ||
"representative": { | ||
"veteran": { | ||
"address": { | ||
"addressLine1": "123", | ||
"addressLine2": "2a", | ||
"city": "city", | ||
"countryCode": "US", | ||
"stateCode": "OR", | ||
"zipCode": "12345", | ||
"zipCodeSuffix": "6789" | ||
} | ||
}, | ||
"representative": { | ||
"poaCode": "067", | ||
"registrationNumber": "999999999999", | ||
"type": "ATTORNEY", | ||
"address": { | ||
"addressLine1": "123", | ||
"addressLine2": "2a", | ||
"city": "city", | ||
"countryCode": "US", | ||
"stateCode": "OR", | ||
"zipCode": "12345", | ||
"zipCodeSuffix": "6789" | ||
} | ||
} | ||
}, | ||
"status": "errored", | ||
"errors": [ | ||
{ | ||
"title": "some error", | ||
"detail": "error detail", | ||
"code": "PDF_SUBMISSION" | ||
} | ||
] | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...edited_representative_portal/spec/fixtures/power_of_attorney_form_submission/success.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"data": { | ||
"id": "a4dd5bb0-d47b-4ca9-9afc-d7ce6a820985", | ||
"type": "claimsApiPowerOfAttorneys", | ||
"attributes": { | ||
"dateRequestAccepted": "2025-01-16", | ||
"previousPoa": "-", | ||
"representative": { | ||
"veteran": { | ||
"address": { | ||
"addressLine1": "123", | ||
"addressLine2": "2a", | ||
"city": "city", | ||
"countryCode": "US", | ||
"stateCode": "OR", | ||
"zipCode": "12345", | ||
"zipCodeSuffix": "6789" | ||
} | ||
}, | ||
"representative": { | ||
"poaCode": "067", | ||
"registrationNumber": "999999999999", | ||
"type": "ATTORNEY", | ||
"address": { | ||
"addressLine1": "123", | ||
"addressLine2": "2a", | ||
"city": "city", | ||
"countryCode": "US", | ||
"stateCode": "OR", | ||
"zipCode": "12345", | ||
"zipCodeSuffix": "6789" | ||
} | ||
} | ||
}, | ||
"status": "pending" | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
spec/sidekiq/accredited_representative_portal/power_of_attorney_form_submission_job_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'sidekiq/testing' | ||
|
||
RSpec.describe AccreditedRepresentativePortal::PowerOfAttorneyFormSubmissionJob, type: :job do | ||
subject { described_class.new } | ||
|
||
let(:poa_form_submission) do | ||
create(:power_of_attorney_form_submission, service_id: '29b7c214-4a61-425e-97f2-1a56de869524') | ||
end | ||
|
||
before do | ||
allow_any_instance_of(Auth::ClientCredentials::Service).to receive(:get_token).and_return('fake_access_token') | ||
poa_form_submission.power_of_attorney_request.claimant.update(icn: '123498767V234859') | ||
end | ||
|
||
describe '#perform' do | ||
context 'successful LH submission' do | ||
let(:service_response) do | ||
File.read('modules/accredited_representative_portal/spec' \ | ||
'/fixtures/power_of_attorney_form_submission/success.json') | ||
end | ||
|
||
context 'successful submission' do | ||
it 'updates the form submission as successful' do | ||
VCR.use_cassette('lighthouse/benefits_claims/power_of_attorney_status/200_response') do | ||
subject.perform(poa_form_submission.id) | ||
end | ||
poa_form_submission.reload | ||
expect(poa_form_submission.status).to eq 'succeeded' | ||
expect(JSON.parse(poa_form_submission.service_response)).to eq JSON.parse(service_response) | ||
expect(poa_form_submission.status_updated_at).not_to be_nil | ||
end | ||
|
||
context 'data shows status of errored' do | ||
let(:expected_error_message) do | ||
'[{"title":"some error","detail":"error detail","code":"PDF_SUBMISSION"}]' | ||
end | ||
let(:service_response) do | ||
File.read('modules/accredited_representative_portal/spec' \ | ||
'/fixtures/power_of_attorney_form_submission/error.json') | ||
end | ||
|
||
it 'updates the form submission as failed' do | ||
VCR.use_cassette('lighthouse/benefits_claims/power_of_attorney_status/200_errored_response') do | ||
subject.perform(poa_form_submission.id) | ||
end | ||
poa_form_submission.reload | ||
expect(poa_form_submission.status).to eq 'failed' | ||
expect(JSON.parse(poa_form_submission.service_response)).to eq JSON.parse(service_response) | ||
expect(poa_form_submission.error_message).to eq expected_error_message | ||
expect(poa_form_submission.status_updated_at).not_to be_nil | ||
end | ||
end | ||
|
||
context 'the job fails 3 times' do | ||
let(:lh_service) { double } | ||
|
||
it 'updates the status as failed' do | ||
subject.sidekiq_retries_exhausted_block.call({ 'args' => [poa_form_submission.id] }) | ||
poa_form_submission.reload | ||
expect(poa_form_submission.status).to eq('failed') | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'submission not found' do | ||
let(:rails_logger) { double } | ||
|
||
it 'updates the form submission and submits the error to sentry and logger' do | ||
Settings.sentry = OpenStruct.new(dsn: 'test') | ||
allow(Rails).to receive(:logger).and_return rails_logger | ||
poa_form_submission.update(service_id: '491b878a-d977-40b8-8de9-7ba302307a48') | ||
expect(Sentry).to receive(:capture_exception) | ||
expect(rails_logger).to receive(:send).with('error', 'Resource not found.') | ||
expect(rails_logger).to receive(:send) | ||
expect do | ||
VCR.use_cassette('lighthouse/benefits_claims/power_of_attorney_status/404_response') do | ||
subject.perform(poa_form_submission.id) | ||
end | ||
end.to raise_error(Common::Exceptions::ResourceNotFound) | ||
poa_form_submission.reload | ||
expect(poa_form_submission.error_message).to eq 'Resource not found' | ||
expect(poa_form_submission.status_updated_at).not_to be_nil | ||
end | ||
end | ||
end | ||
end |
77 changes: 77 additions & 0 deletions
77
...cr_cassettes/lighthouse/benefits_claims/power_of_attorney_status/200_errored_response.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
53 changes: 0 additions & 53 deletions
53
...upport/vcr_cassettes/lighthouse/benefits_claims/power_of_attorney_status/200_response.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.