Skip to content

Commit

Permalink
Updated upload_status_updater.rb and wrote unit tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoforma committed Dec 6, 2024
1 parent 7c0958c commit a8f704d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
48 changes: 18 additions & 30 deletions lib/lighthouse/benefits_documents/upload_status_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@

module BenefitsDocuments
class UploadStatusUpdater
# Parses the status of a Lighthouse526DocumentUpload submitted to Lighthouse
# Parses the status of a LighthouseDocumentUpload [EvidenceSubmission] submitted to Lighthouse
# using metadata from the Lighthouse Benefits Documents API '/uploads/status' endpoint.
# Provides methods to determine if a document has completed all steps or failed,
# abstracting away the details of the Lighthouse status data structure.
#
# Additionally, updates the state of a Lighthouse526DocumentUpload in vets-api to reflect
# Additionally, updates the state of a LighthouseDocumentUpload [EvidenceSubmission] in vets-api to reflect
# the current status of a document as it transitions from Lighthouse > VBMS > BGS
#
# Documentation on the Lighthouse '/uploads/status' endpoint is available here:
# https://dev-developer.va.gov/explore/api/benefits-documents/docs?version=current

# LIGHTHOUSE_DOCUMENT_COMPLETE_STATUS = 'SUCCESS'
# LIGHTHOUSE_DOCUMENT_FAILED_STATUS = 'FAILED'
PROCESSING_TIMEOUT_WINDOW_IN_HOURS = 24

# @param lighthouse_document_status [Hash] includes a single document's status progress
# after it has been submitted to Lighthouse, while Lighthouse attempts to pass it on to
# VBMS and then BGS. These data come from Lighthouse's '/uploads/status' endpoint.
#
# @param lighthouse526_document_upload [Lighthouse526DocumentUpload] the VA.gov record of the document
# @param lighthouse_document_upload [EvidenceSubmission] the VA.gov record of the document
# submitted to Lighthouse for tracking.
#
# example lighthouse_document_status hash:
Expand Down Expand Up @@ -67,16 +65,7 @@ def update_status

process_failure if failed?

finalize_upload if completed? || failed?

# @lighthouse_document_upload.update!(status_last_polled_at: DateTime.now.utc)
end

def process_failure
@lighthouse_document_upload.update!(
acknowledgement_date: (DateTime.current + 30).utc,
error_message: @lighthouse_document_status_response['error']
)
process_upload if completed?
end

def get_failure_step
Expand Down Expand Up @@ -115,21 +104,20 @@ def log_status
)
end

def finalize_upload
# @lighthouse_document_upload.update!(lighthouse_processing_ended_at: end_time)

if completed?
@lighthouse_document_upload.update(
upload_status: BenefitsDocuments::Constants::UPLOAD_STATUS[:SUCCESS],
delete_date: (DateTime.current + 60).utc
)
else
@lighthouse_document_upload.update!(
upload_status: BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED],
failed_date: DateTime.now.utc,
error_message: @lighthouse_document_status_response['error']
)
end
def process_failure
@lighthouse_document_upload.update!(
upload_status: BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED],
failed_date: DateTime.now.utc,
acknowledgement_date: (DateTime.current + 30.days).utc,
error_message: @lighthouse_document_status_response['error']
)
end

def process_upload
@lighthouse_document_upload.update(
upload_status: BenefitsDocuments::Constants::UPLOAD_STATUS[:SUCCESS],
delete_date: (DateTime.current + 60.days).utc
)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let(:lighthouse_document_upload) { create(:bd_evidence_submission) }
let(:lighthouse_document_upload_timeout) { create(:bd_evidence_submission_timeout) }
let(:past_date_time) { DateTime.new(1985, 10, 26) }
let(:current_date_time) { DateTime.now.utc }

describe '#update_status' do
shared_examples 'status updater' do |status, error_message = nil|
Expand All @@ -33,9 +34,42 @@
end

if error_message
it 'saves the error_message' do
expect { status_updater.update_status }.to change(lighthouse_document_upload, :error_message)
.to(error_message)
context "when there's an error" do
it 'saves the error_message' do
Timecop.freeze(current_date_time) do
expect { status_updater.update_status }.to change(lighthouse_document_upload, :error_message)
.to(error_message.to_s)
end
end

it 'updates status, failed_date, and acknowledgement_date' do
Timecop.freeze(current_date_time) do
expect { status_updater.update_status }
.to change(lighthouse_document_upload, :acknowledgement_date)
.from(nil)
.to((current_date_time + 30.days).utc)
.and change(lighthouse_document_upload, :failed_date)
.from(nil)
.to(current_date_time.utc)
.and change(lighthouse_document_upload, :upload_status)
.from(nil)
.to(BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED])
end
end
end
else # testing success status
context 'when completed successfully' do
it 'updates status, and delete_date' do
Timecop.freeze(current_date_time) do
expect { status_updater.update_status }
.to change(lighthouse_document_upload, :delete_date)
.from(nil)
.to((current_date_time + 60.days).utc)
.and change(lighthouse_document_upload, :upload_status)
.from(nil)
.to(BenefitsDocuments::Constants::UPLOAD_STATUS[:SUCCESS])
end
end
end
end
end
Expand Down Expand Up @@ -73,15 +107,13 @@
describe '#processing_timeout?' do
shared_examples 'processing timeout' do |status, expected, expired|
it "returns #{expected}" do
# Timecop.freeze(past_date_time.utc) do
status_updater = described_class.new(
{
'status' => status
},
expired ? lighthouse_document_upload_timeout : lighthouse_document_upload
)
expect(status_updater.processing_timeout?).to eq(expected)
# end
end
end

Expand Down

0 comments on commit a8f704d

Please sign in to comment.