diff --git a/app/models/lighthouse526_document_upload.rb b/app/models/lighthouse526_document_upload.rb index f7cc708ffc1..c59d02163e3 100644 --- a/app/models/lighthouse526_document_upload.rb +++ b/app/models/lighthouse526_document_upload.rb @@ -44,6 +44,10 @@ class Lighthouse526DocumentUpload < ApplicationRecord end end + def form0781_types? + [FORM_0781_DOCUMENT_TYPE, FORM_0781A_DOCUMENT_TYPE].include?(document_type) + end + private def veteran_upload? diff --git a/lib/lighthouse/benefits_documents/form526/update_documents_status_service.rb b/lib/lighthouse/benefits_documents/form526/update_documents_status_service.rb index dbc890afd9d..d04aae030c3 100644 --- a/lib/lighthouse/benefits_documents/form526/update_documents_status_service.rb +++ b/lib/lighthouse/benefits_documents/form526/update_documents_status_service.rb @@ -64,30 +64,52 @@ def update_documents_status def update_document_status(status) document_upload = @lighthouse526_document_uploads.find_by!(lighthouse_document_request_id: status['requestId']) + statsd_document_base_key(STATSD_DOCUMENT_TYPE_KEY_MAP[document_upload.document_type]) - # UploadStatusUpdater encapsulates all parsing of a status response from Lighthouse - status_updater = BenefitsDocuments::Form526::UploadStatusUpdater.new(status, document_upload) - status_updater.update_status - - statsd_document_type_key = STATSD_DOCUMENT_TYPE_KEY_MAP[document_upload.document_type] - statsd_document_base_key = "#{STATSD_BASE_KEY}.#{statsd_document_type_key}" + status_updater(status, document_upload) + @status_updater.update_status if document_upload.completed? # ex. 'api.form526.lighthouse_document_upload_processing_status.bdd_instructions.complete' - StatsD.increment("#{statsd_document_base_key}.#{STATSD_DOCUMENT_COMPLETE_KEY}") + StatsD.increment("#{@statsd_document_base_key}.#{STATSD_DOCUMENT_COMPLETE_KEY}") elsif document_upload.failed? - # Because Lighthouse's processing steps are subject to change, these metrics must be dyanmic. - # Currently, this should return either claims_evidence or benefits_gateway_service - failure_step_key = status_updater.get_failure_step.downcase - - # ex. 'api.form526.lighthouse_document_upload_processing_status.bdd_instructions.failed.claims_evidence' - StatsD.increment("#{statsd_document_base_key}.#{STATSD_DOCUMENT_FAILED_KEY}.#{failure_step_key}") - elsif status_updater.processing_timeout? + log_failure(document_upload) + elsif @status_updater.processing_timeout? # Triggered when a document is still pending more than 24 hours after processing began # ex. 'api.form526.lighthouse_document_upload_processing_status.bdd_instructions.processing_timeout' - StatsD.increment("#{statsd_document_base_key}.#{STATSD_PROCESSING_TIMEOUT_KEY}") + StatsD.increment("#{@statsd_document_base_key}.#{STATSD_PROCESSING_TIMEOUT_KEY}") end end + + def status_updater(status, document_upload) + # UploadStatusUpdater encapsulates all parsing of a status response from Lighthouse + @status_updater ||= BenefitsDocuments::Form526::UploadStatusUpdater.new(status, document_upload) + end + + def statsd_document_base_key(statsd_document_type_key) + @statsd_document_base_key ||= "#{STATSD_BASE_KEY}.#{statsd_document_type_key}" + end + + def log_failure(document_upload) + # Because Lighthouse's processing steps are subject to change, these metrics must be dynamic. + # Currently, this should return either CLAIMS_EVIDENCE or BENEFITS_GATEWAY_SERVICE + failure_step = @status_updater.get_failure_step + + # ex. 'api.form526.lighthouse_document_upload_processing_status.bdd_instructions.failed.claims_evidence' + StatsD.increment("#{@statsd_document_base_key}.#{STATSD_DOCUMENT_FAILED_KEY}.#{failure_step.downcase}") + return unless document_upload.form0781_types? + + submission = document_upload.form526_submission + Rails.logger.warn( + 'Benefits Documents API responded with a failed document upload status', { + form526_submission_id: submission.id, + document_type: document_upload.document_type, + failure_step:, + lighthouse_document_request_id: document_upload.lighthouse_document_request_id, + user_uuid: submission.user_uuid + } + ) + end end end end diff --git a/spec/lib/lighthouse/benefits_documents/form526/update_documents_status_service_spec.rb b/spec/lib/lighthouse/benefits_documents/form526/update_documents_status_service_spec.rb index 8fb91d632ca..b4bb08e4761 100644 --- a/spec/lib/lighthouse/benefits_documents/form526/update_documents_status_service_spec.rb +++ b/spec/lib/lighthouse/benefits_documents/form526/update_documents_status_service_spec.rb @@ -145,4 +145,49 @@ def mock_success_response(request_id) 'api.form526.lighthouse_document_upload_processing_status.veteran_upload.complete' end end + + describe 'logging when Form 0781 document upload fails' do + let(:form0781_document_upload) { create(:lighthouse526_document_upload, document_type: 'Form 0781') } + let(:uploads) { Lighthouse526DocumentUpload.where(id: form0781_document_upload.id) } + let(:status_response) do + { + 'data' => { + 'statuses' => [ + { + 'requestId' => form0781_document_upload.lighthouse_document_request_id, + 'status' => 'FAILED', + 'time' => { 'startTime' => 499_152_030, 'endTime' => 499_152_060 }, + 'steps' => [ + { 'name' => 'CLAIMS_EVIDENCE', 'status' => 'FAILED' }, + { 'name' => 'BENEFITS_GATEWAY_SERVICE', 'status' => 'NOT_STARTED' } + ], + 'error' => { 'detail' => 'VBMS System Outage', 'step' => 'CLAIMS_EVIDENCE' } + } + ] + } + } + end + + let(:submission) { form0781_document_upload.form526_submission } + + before do + allow_any_instance_of(BenefitsDocuments::Form526::UploadStatusUpdater).to receive(:update_status) + allow_any_instance_of(Lighthouse526DocumentUpload).to receive(:failed?).and_return(true) + allow(Rails.logger).to receive(:warn) + end + + it 'logs the latest_status_response to the Rails logger' do + described_class.call(uploads, status_response) + + expect(Rails.logger).to have_received(:warn).with( + 'Benefits Documents API responded with a failed document upload status', { + form526_submission_id: submission.id, + document_type: form0781_document_upload.document_type, + failure_step: 'CLAIMS_EVIDENCE', + lighthouse_document_request_id: form0781_document_upload.lighthouse_document_request_id, + user_uuid: submission.user_uuid + } + ) + end + end end diff --git a/spec/models/lighthouse526_document_upload_spec.rb b/spec/models/lighthouse526_document_upload_spec.rb index 7c7486b8128..8b6bdc33033 100644 --- a/spec/models/lighthouse526_document_upload_spec.rb +++ b/spec/models/lighthouse526_document_upload_spec.rb @@ -106,5 +106,21 @@ end end end + + describe '#form0781_types?' do + it 'returns true for Form 0781 and Form 0781a document types' do + ['Form 0781', 'Form 0781a'].each do |document_type| + upload = build(:lighthouse526_document_upload, document_type:) + expect(upload.form0781_types?).to be(true) + end + end + + it 'returns false for other document types' do + ['BDD Instructions', 'Veteran Upload'].each do |document_type| + upload = build(:lighthouse526_document_upload, document_type:) + expect(upload.form0781_types?).to be(false) + end + end + end end end