From d78816861fbdd6011fc48901ca0e85013675e4c5 Mon Sep 17 00:00:00 2001 From: Nathan Burgess Date: Thu, 5 Sep 2024 17:09:08 -0400 Subject: [PATCH] Create Lighthouse upload polling record with API response --- .../upload_bdd_instructions.rb | 25 ++++++++++- .../upload_bdd_instructions_spec.rb | 44 +++++++++++++++++-- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/app/sidekiq/evss/disability_compensation_form/upload_bdd_instructions.rb b/app/sidekiq/evss/disability_compensation_form/upload_bdd_instructions.rb index 416d7613c0c..83ff877a074 100644 --- a/app/sidekiq/evss/disability_compensation_form/upload_bdd_instructions.rb +++ b/app/sidekiq/evss/disability_compensation_form/upload_bdd_instructions.rb @@ -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) @@ -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 diff --git a/spec/sidekiq/evss/disability_compensation_form/upload_bdd_instructions_spec.rb b/spec/sidekiq/evss/disability_compensation_form/upload_bdd_instructions_spec.rb index cc324b7c640..29a885a0dd0 100644 --- a/spec/sidekiq/evss/disability_compensation_form/upload_bdd_instructions_spec.rb +++ b/spec/sidekiq/evss/disability_compensation_form/upload_bdd_instructions_spec.rb @@ -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 @@ -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 @@ -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