diff --git a/app/controllers/v0/pension_claims_controller.rb b/app/controllers/v0/pension_claims_controller.rb index 79f039d112d..7eb44845368 100644 --- a/app/controllers/v0/pension_claims_controller.rb +++ b/app/controllers/v0/pension_claims_controller.rb @@ -12,40 +12,31 @@ def claim_class SavedClaim::Pension end - # rubocop:disable Metrics/MethodLength def show claim = claim_class.find_by!({ guid: params[:id] }) # will raise ActiveRecord::NotFound - form_submission = claim.form_submissions&.order(id: :asc)&.last submission_attempt = form_submission&.form_submission_attempts&.order(created_at: :asc)&.last - if submission_attempt # this is to satisfy frontend check for successful submission state = submission_attempt.aasm_state == 'failure' ? 'failure' : 'success' - response = { - data: { - id: claim.id, - form_id: claim.form_id, - guid: claim.guid, - attributes: { - state:, - benefits_intake_uuid: form_submission.benefits_intake_uuid, - form_type: form_submission.form_type, - attempt_id: submission_attempt.id, - aasm_state: submission_attempt.aasm_state - } - } - } + response = format_show_response(claim, state, form_submission, submission_attempt) end - render(json: response) + rescue ActiveRecord::RecordNotFound => e + Rails.logger.error('21P-527EZ submission not found', + { confirmation_number: params[:id], user_uuid: current_user&.uuid, errors: e.message }) + render(json: { error: e.to_s }, status: :not_found) + rescue => e + Rails.logger.error('Fetch 21P-527EZ submission failed', + { confirmation_number: params[:id], user_uuid: current_user&.uuid, errors: e.message }) + raise e end - # rubocop:enable Metrics/MethodLength # Creates and validates an instance of the class, removing any copies of # the form that had been previously saved by the user. def create PensionBurial::TagSentry.tag_sentry + StatsD.increment("#{stats_key}.attempt") claim = claim_class.new(form: filtered_params[:form]) user_uuid = current_user&.uuid @@ -55,21 +46,20 @@ def create claim.itf_datetime = in_progress_form.created_at if in_progress_form unless claim.save - StatsD.increment("#{stats_key}.failure") + track_create_error(in_progress_form, claim) log_validation_error_to_metadata(in_progress_form, claim) - Rails.logger.error("Submit #{claim.class::FORM} Failed for user_id: #{user_uuid}", - { in_progress_form_id: in_progress_form&.id, errors: claim&.errors&.errors }) raise Common::Exceptions::ValidationErrors, claim.errors end claim.upload_to_lighthouse - StatsD.increment("#{stats_key}.success") - Rails.logger.info("Submit #{claim.class::FORM} Success", - { confirmation_number: claim.confirmation_number, user_uuid: }) + track_create_success(in_progress_form, claim) clear_saved_form(claim.form_id) render(json: claim) + rescue => e + track_create_error(in_progress_form, claim, e) + raise e end private @@ -81,5 +71,37 @@ def log_validation_error_to_metadata(in_progress_form, claim) metadata['submission']['error_message'] = claim&.errors&.errors&.to_s in_progress_form.update(metadata:) end + + def format_show_response(claim, state, form_submission, submission_attempt) + { + data: { + id: claim.id, + form_id: claim.form_id, + guid: claim.guid, + attributes: { + state:, + benefits_intake_uuid: form_submission.benefits_intake_uuid, + form_type: form_submission.form_type, + attempt_id: submission_attempt.id, + aasm_state: submission_attempt.aasm_state + } + } + } + end + + def track_create_error(in_progress_form, claim, e) + Rails.logger.error('21P-527EZ submission to Sidekiq failed', + { confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid, + in_progress_form_id: in_progress_form&.id, errors: claim&.errors&.errors, + message: e&.message }) + StatsD.increment("#{stats_key}.failure") + end + + def track_create_success(in_progress_form, claim) + Rails.logger.info('21P-527EZ submission to Sidekiq success', + { confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid, + in_progress_form_id: in_progress_form&.id }) + StatsD.increment("#{stats_key}.success") + end end end diff --git a/app/sidekiq/lighthouse/pension_benefit_intake_job.rb b/app/sidekiq/lighthouse/pension_benefit_intake_job.rb index e75b27be4f6..3f61a0a3848 100644 --- a/app/sidekiq/lighthouse/pension_benefit_intake_job.rb +++ b/app/sidekiq/lighthouse/pension_benefit_intake_job.rb @@ -170,6 +170,15 @@ def form_submission_polling def cleanup_file_paths Common::FileHelpers.delete_file_if_exists(@form_path) if @form_path @attachment_paths&.each { |p| Common::FileHelpers.delete_file_if_exists(p) } + rescue => e + Rails.logger.error('Lighthouse::PensionBenefitIntakeJob cleanup failed', + { + error: e.message, + claim_id: @claim&.id, + benefits_intake_uuid: @lighthouse_service&.uuid, + confirmation_number: @claim&.confirmation_number + }) + raise e end end end diff --git a/spec/controllers/v0/pension_claims_controller_spec.rb b/spec/controllers/v0/pension_claims_controller_spec.rb index fbb17e151d2..ef51594a714 100644 --- a/spec/controllers/v0/pension_claims_controller_spec.rb +++ b/spec/controllers/v0/pension_claims_controller_spec.rb @@ -3,6 +3,36 @@ require 'rails_helper' require 'support/controller_spec_helper' +def logger_mock(str, x) + print(str, x) +end + RSpec.describe V0::PensionClaimsController, type: :controller do it_behaves_like 'a controller that deletes an InProgressForm', 'pension_claim', 'pension_claim', '21P-527EZ' + describe '#create' do + let(:form) { build(:pension_claim) } + let(:param_name) { :pension_claim } + let(:form_id) { '21P-527EZ' } + let(:user) { create(:user) } + + it('logs a success') do + expect(Rails.logger).to receive(:info).with('Begin 21P-527EZ Submission', hash_including(:guid, :user_uuid)) + expect(Rails.logger).to receive(:info).with('21P-527EZ submission to Sidekiq success', + hash_including(:confirmation_number, :user_uuid, + :in_progress_form_id)) + expect(Rails.logger).to receive(:info).at_least(:once) + post(:create, params: { param_name => { form: form.form } }) + end + end + + describe '#show' do + it 'logs an error if no claim found' do + expect(Rails.logger).to receive(:error).once + claim = create(:pension_claim) + guid = claim.guid + claim.destroy + response = get(:show, params: { id: guid }) + expect(response.status).to eq(404) + end + end end diff --git a/spec/sidekiq/lighthouse/pension_benefit_intake_job_spec.rb b/spec/sidekiq/lighthouse/pension_benefit_intake_job_spec.rb index f2bd268ae3c..e9041b9ed95 100644 --- a/spec/sidekiq/lighthouse/pension_benefit_intake_job_spec.rb +++ b/spec/sidekiq/lighthouse/pension_benefit_intake_job_spec.rb @@ -178,5 +178,19 @@ end end + describe '#cleanup_file_paths' do + before do + job.instance_variable_set(:@form_path, 'path/file.pdf') + job.instance_variable_set(:@attachment_paths, '/invalid_path/should_be_an_array.failure') + end + + it 'returns expected hash' do + expect(Rails.logger).to receive(:error).with('Lighthouse::PensionBenefitIntakeJob cleanup failed', + hash_including(:error, :claim_id, :confirmation_number, + :benefits_intake_uuid)) + expect { job.cleanup_file_paths }.to raise_error(NoMethodError) + end + end + # Rspec.describe end