Class: BenefitsDocuments::Form526::UploadStatusUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/lighthouse/benefits_documents/form526/upload_status_updater.rb

Constant Summary collapse

LIGHTHOUSE_DOCUMENT_COMPLETE_STATUS =

Parses the status of a Lighthouse526DocumentUpload 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 the current status of a document as it transitions from Lighthouse > VBMS > BGS

Documentation on the Lighthouse ‘/uploads/status’ endpoint is available here: dev-developer.va.gov/explore/api/benefits-documents/docs?version=current

'SUCCESS'
LIGHTHOUSE_DOCUMENT_FAILED_STATUS =
'FAILED'
PROCESSING_TIMEOUT_WINDOW_IN_HOURS =
24

Instance Method Summary collapse

Constructor Details

#initialize(lighthouse526_document_status, lighthouse526_document_upload) ⇒ UploadStatusUpdater

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.

submitted to Lighthouse for tracking.

example lighthouse526_document_status hash:

{
  "requestId": 600000001,
  "time": {
    "startTime": 1502199000,
    "endTime": 1502199000
  },
  "status": "IN_PROGRESS",
  "steps": [
    {
      "name": "BENEFITS_GATEWAY_SERVICE",
      "nextStepName": "BENEFITS_GATEWAY_SERVICE",
      "description": "string",
      "status": "NOT_STARTED"
    }
  ],
  "error": {
    "detail": "string",
    "step": "BENEFITS_GATEWAY_SERVICE"
  }
}

Parameters:

  • lighthouse526_document_status (Hash)

    includes a single document’s status progress

  • lighthouse526_document_upload (Lighthouse526DocumentUpload)

    the VA.gov record of the document



51
52
53
54
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 51

def initialize(lighthouse526_document_status, lighthouse526_document_upload)
  @lighthouse526_document_status = lighthouse526_document_status
  @lighthouse526_document_upload = lighthouse526_document_upload
end

Instance Method Details

#completed?Boolean (private)

Returns:

  • (Boolean)


107
108
109
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 107

def completed?
  @lighthouse526_document_status['status'] == LIGHTHOUSE_DOCUMENT_COMPLETE_STATUS
end

#end_timeObject (private)



98
99
100
101
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 98

def end_time
  unix_end_time = @lighthouse526_document_status.dig('time', 'endTime')
  Time.at(unix_end_time / 1000).utc.to_datetime
end

#failed?Boolean (private)

Returns:

  • (Boolean)


103
104
105
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 103

def failed?
  @lighthouse526_document_status['status'] == LIGHTHOUSE_DOCUMENT_FAILED_STATUS
end

#finalize_uploadObject (private)



120
121
122
123
124
125
126
127
128
129
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 120

def finalize_upload
  @lighthouse526_document_upload.update!(lighthouse_processing_ended_at: end_time)

  if completed?
    @lighthouse526_document_upload.complete!
  else
    @lighthouse526_document_upload.update!(error_message: @lighthouse526_document_status['error'])
    @lighthouse526_document_upload.fail!
  end
end

#get_failure_stepObject



73
74
75
76
77
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 73

def get_failure_step
  return unless failed? && @lighthouse526_document_status['error']

  @lighthouse526_document_status['error']['step']
end

#log_statusObject (private)



111
112
113
114
115
116
117
118
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 111

def log_status
  Rails.logger.info(
    'BenefitsDocuments::Form526::UploadStatusUpdater',
    status: @lighthouse526_document_status['status'],
    status_response: @lighthouse526_document_status,
    updated_at: DateTime.now.utc
  )
end

#processing_timeout?Boolean

Returns true if document is still processing in Lighthouse, and initiated more than a set number of hours ago

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 80

def processing_timeout?
  return false if @lighthouse526_document_status.dig('time', 'endTime')

  start_time < PROCESSING_TIMEOUT_WINDOW_IN_HOURS.hours.ago.utc
end

#start_timeObject (private)

Lighthouse returns date times as UNIX timestamps in milliseconds



93
94
95
96
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 93

def start_time
  unix_start_time = @lighthouse526_document_status.dig('time', 'startTime')
  Time.at(unix_start_time / 1000).utc.to_datetime
end

#status_changed?Boolean (private)

Returns:

  • (Boolean)


88
89
90
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 88

def status_changed?
  @lighthouse526_document_status != @lighthouse526_document_upload.last_status_response
end

#update_statusObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lighthouse/benefits_documents/form526/upload_status_updater.rb', line 56

def update_status
  # Only save an upload's status if it has transitioned since the last Lighthouse poll
  return unless status_changed?

  # Ensure start time and latest status response from API are saved, regardless if document is still in progress
  @lighthouse526_document_upload.update!(
    lighthouse_processing_started_at: start_time,
    last_status_response: @lighthouse526_document_status
  )

  log_status

  finalize_upload if completed? || failed?

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