Skip to content

Commit

Permalink
Create Lighthouse upload polling record with API response
Browse files Browse the repository at this point in the history
  • Loading branch information
NB28VT committed Sep 5, 2024
1 parent 8320785 commit d788168
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ def upload_bdd_instructions
# as a "kill switch" in the event there are problems with the ApiProviderFactory implementation, so we can
# revert back to using the EVSS::DocumentsService directly here
if Flipper.enabled?(:disability_compensation_use_api_provider_for_bdd_instructions)
upload_via_api_provider
api_response = upload_via_api_provider

if @api_upload_provider.is_a?(LighthouseSupplementalDocumentUploadProvider)
create_lighthouse_polling_record(api_response)
end

api_upload_provider.log_upload_success(STATSD_KEY_PREFIX)
else
EVSS::DocumentsService.new(submission.auth_headers).upload(file_body, document_data)
Expand All @@ -144,6 +149,24 @@ def api_upload_provider
@api_upload_provider ||= EVSS::DisabilityCompensationForm::UploadBddInstructions.api_upload_provider(submission)
end

# Creates a Lighthouse526DocumentUpload record, where we save
# a unique 'request ID' Lighthouse provides us in the API response after we upload a document.
# We use this ID in the Form526DocumentUploadPollingJob chron job to check the status of the document
# after Lighthouse has received it.
#
# @param api_response [Faraday::Response] the response from the Lighthouse Benefits Documents API upload endpoint
def create_lighthouse_polling_record(api_response)
response_body = api_response.body['data']

if response_body['success'] == true && response_body['requestId']
Lighthouse526DocumentUpload.create!(
form526_submission_id: @submission_id,
document_type: Lighthouse526DocumentUpload::BDD_INSTRUCTIONS_DOCUMENT_TYPE,
lighthouse_document_request_id: response_body['requestId']
)
end
end

def retryable_error_handler(error)
super(error)
raise error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,23 @@
end

context 'when the ApiProviderFactory::FEATURE_TOGGLE_UPLOAD_BDD_INSTRUCTIONS feature flag is enabled' do
let(:faraday_response) { instance_double(Faraday::Response) }
let(:lighthouse_request_id) { Faker::Number.number(digits: 8) }

before do
Flipper.enable(ApiProviderFactory::FEATURE_TOGGLE_UPLOAD_BDD_INSTRUCTIONS)

allow_any_instance_of(LighthouseSupplementalDocumentUploadProvider).to receive(:submit_upload_document)
.and_return(faraday_response)

allow(faraday_response).to receive(:body).and_return(
{
'data' => {
'success' => true,
'requestId' => lighthouse_request_id
}
}
)
end

it 'uploads the document via the LighthouseSupplementalDocumentUploadProvider' do
Expand All @@ -88,15 +103,27 @@
end

it 'logs an upload success via the upload provider' do
# Stub API call
allow(BenefitsDocuments::Form526::UploadSupplementalDocumentService).to receive(:call)

expect_any_instance_of(LighthouseSupplementalDocumentUploadProvider)
.to receive(:log_upload_success).with('worker.evss.submit_form526_bdd_instructions')

subject.perform_async(submission.id)
described_class.drain
end

it 'creates a pending Lighthouse526DocumentUpload record for the submission so we can poll Lighthouse later' do
upload_attributes = {
aasm_state: 'pending',
form526_submission_id: submission.id,
document_type: Lighthouse526DocumentUpload::BDD_INSTRUCTIONS_DOCUMENT_TYPE,
lighthouse_document_request_id: lighthouse_request_id

}

expect do
subject.perform_async(submission.id)
described_class.drain
end.to change { Lighthouse526DocumentUpload.where(**upload_attributes).count }.by(1)
end
end

context 'when the ApiProviderFactory::FEATURE_TOGGLE_UPLOAD_BDD_INSTRUCTIONS feature flag is disabled' do
Expand Down Expand Up @@ -128,6 +155,17 @@
subject.perform_async(submission.id)
described_class.drain
end

# We don't create these records when uploading to EVSS, since they are only used
# to poll Lighthouse for the status of the document after Lighthouse receives it
it 'does not create a Lighthouse526DocumentUpload record' do
allow_any_instance_of(EVSSSupplementalDocumentUploadProvider).to receive(:submit_upload_document)

expect do
subject.perform_async(submission.id)
described_class.drain
end.not_to change(Lighthouse526DocumentUpload, :count)
end
end
end
end
Expand Down

0 comments on commit d788168

Please sign in to comment.