diff --git a/lib/lighthouse/benefits_documents/upload_status_updater.rb b/lib/lighthouse/benefits_documents/upload_status_updater.rb index ee9721bb910..468a322cc5b 100644 --- a/lib/lighthouse/benefits_documents/upload_status_updater.rb +++ b/lib/lighthouse/benefits_documents/upload_status_updater.rb @@ -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: @@ -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 @@ -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 diff --git a/spec/lib/lighthouse/benefits_documents/upload_status_updater_spec.rb b/spec/lib/lighthouse/benefits_documents/upload_status_updater_spec.rb index 982ab8e27f5..493118927cd 100644 --- a/spec/lib/lighthouse/benefits_documents/upload_status_updater_spec.rb +++ b/spec/lib/lighthouse/benefits_documents/upload_status_updater_spec.rb @@ -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| @@ -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 @@ -73,7 +107,6 @@ 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 @@ -81,7 +114,6 @@ expired ? lighthouse_document_upload_timeout : lighthouse_document_upload ) expect(status_updater.processing_timeout?).to eq(expected) - # end end end