Class: FormSubmissionAttempt

Inherits:
ApplicationRecord show all
Includes:
AASM
Defined in:
app/models/form_submission_attempt.rb

Constant Summary collapse

HOUR_TO_SEND_NOTIFICATIONS =
9

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

descendants_using_encryption, lockbox_options, #timestamp_attributes_for_update_in_model, #valid?

Class Method Details

.latest_attemptsObject



23
24
25
26
# File 'app/models/form_submission_attempt.rb', line 23

def self.latest_attempts
  select('DISTINCT ON (form_submission_id) form_submission_id, benefits_intake_uuid')
    .order('form_submission_id, created_at DESC')
end

Instance Method Details

#form526_form4142_email(log_info) ⇒ Object (private)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/form_submission_attempt.rb', line 116

def form526_form4142_email(log_info)
  if Flipper.enabled?(CentralMail::SubmitForm4142Job::POLLED_FAILURE_EMAIL)
    queue_form526_form4142_email(Form526Submission.find_by(saved_claim_id:).id, log_info)
  else
    Rails.logger.info(
      'Would queue EVSS::DisabilityCompensationForm::Form4142DocumentUploadFailureEmail, but flipper is off.',
      log_info.merge({ form526_submission_id: })
    )
  end
rescue => e
  cl = caller_locations.first
  call_location = Logging::CallLocation.new(
    CentralMail::SubmitForm4142Job::ZSF_DD_TAG_FUNCTION, cl.path, cl.lineno
  )
  ZeroSilentFailures::Monitor.new(Form526Submission::ZSF_DD_TAG_SERVICE).log_silent_failure(
    log_info.merge({ error_class: e.class, error_message: e.message }),
    log_info[:user_account_uuid], call_location:
  )
end

#log_status_changeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/form_submission_attempt.rb', line 74

def log_status_change
  log_hash = {
    form_submission_id:,
    benefits_intake_uuid:,
    form_type: form_submission&.form_type,
    from_state: aasm.from_state,
    to_state: aasm.to_state,
    event: aasm.current_event
  }

  case aasm.current_event
  when 'fail!'
    log_hash[:message] = 'Form Submission Attempt failed'
    Rails.logger.error(log_hash)
  when 'vbms!'
    log_hash[:message] = 'Form Submission Attempt went to vbms'
  when 'manual!'
    log_hash[:message] = 'Form Submission Attempt is being manually remediated'
  else
    log_hash[:message] = 'Form Submission Attempt State change'
  end

  Rails.logger.info(log_hash) if aasm.current_event != 'fail!'
end

#queue_form526_form4142_email(form526_submission_id, log_info) ⇒ Object (private)



106
107
108
109
110
111
112
113
114
# File 'app/models/form_submission_attempt.rb', line 106

def queue_form526_form4142_email(form526_submission_id, log_info)
  Rails.logger.info('Queuing Form526:Form4142 failure email to VaNotify',
                    log_info.merge({ form526_submission_id: }))
  jid = EVSS::DisabilityCompensationForm::Form4142DocumentUploadFailureEmail.perform_async(
    form526_submission_id
  )
  Rails.logger.info('Queuing Form526:Form4142 failure email to VaNotify completed',
                    log_info.merge({ jid:, form526_submission_id: }))
end

#should_send_simple_forms_emailObject (private)



136
137
138
# File 'app/models/form_submission_attempt.rb', line 136

def should_send_simple_forms_email
  simple_forms_form_number && Flipper.enabled?(:simple_forms_email_notifications)
end

#simple_forms_api_email(log_info) ⇒ Object (private)



101
102
103
104
# File 'app/models/form_submission_attempt.rb', line 101

def simple_forms_api_email(log_info)
  Rails.logger.info('Preparing to send Form Submission Attempt error email', log_info)
  simple_forms_enqueue_result_email(:error)
end

#simple_forms_enqueue_result_email(notification_type) ⇒ Object (private)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/form_submission_attempt.rb', line 140

def simple_forms_enqueue_result_email(notification_type)
  raw_form_data = form_submission.form_data || '{}'
  form_data = JSON.parse(raw_form_data)
  config = {
    form_data:,
    form_number: simple_forms_form_number,
    confirmation_number: benefits_intake_uuid,
    date_submitted: created_at.strftime('%B %d, %Y'),
    lighthouse_updated_at: lighthouse_updated_at&.strftime('%B %d, %Y')
  }

  SimpleFormsApi::NotificationEmail.new(
    config,
    notification_type:,
    user_account:
  ).send(at: time_to_send)
end

#simple_forms_form_numberObject (private)



170
171
172
173
174
175
176
177
# File 'app/models/form_submission_attempt.rb', line 170

def simple_forms_form_number
  @simple_forms_form_number ||=
    if SimpleFormsApi::NotificationEmail::TEMPLATE_IDS.keys.include? form_submission.form_type
      form_submission.form_type
    else
      SimpleFormsApi::V1::UploadsController::FORM_NUMBER_MAP[form_submission.form_type]
    end
end

#time_to_sendObject (private)



158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/form_submission_attempt.rb', line 158

def time_to_send
  now = Time.now.in_time_zone('Eastern Time (US & Canada)')
  if now.hour < HOUR_TO_SEND_NOTIFICATIONS
    now.change(hour: HOUR_TO_SEND_NOTIFICATIONS,
               min: 0)
  else
    now.tomorrow.change(
      hour: HOUR_TO_SEND_NOTIFICATIONS, min: 0
    )
  end
end