Skip to content

Commit

Permalink
91324: Move SavedClaim updates into DR controller transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
dfong-adh committed Sep 3, 2024
1 parent baca476 commit f68a9f9
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 79 deletions.
5 changes: 0 additions & 5 deletions app/controllers/v0/notice_of_disagreements_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# frozen_string_literal: true

require 'decision_review/utilities/saved_claim/service'

module V0
class NoticeOfDisagreementsController < AppealsBaseController
include DecisionReview::SavedClaim::Service
service_tag 'board-appeal'
def create
saved_claim_request_body = request_body_hash.to_json # serialize before modifications are made to request body
nod_response_body = AppealSubmission.submit_nod(
current_user: @current_user,
request_body_hash:,
decision_review_service:
)
store_saved_claim(claim_class: SavedClaim::NoticeOfDisagreement, form: saved_claim_request_body,
guid: nod_response_body.dig('data', 'id'))
render json: nod_response_body
rescue => e
request = begin
Expand Down
6 changes: 0 additions & 6 deletions app/controllers/v1/notice_of_disagreements_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# frozen_string_literal: true

require 'decision_review/utilities/saved_claim/service'

module V1
class NoticeOfDisagreementsController < AppealsBaseControllerV1
include DecisionReview::SavedClaim::Service
service_tag 'board-appeal'

def show
Expand All @@ -17,15 +14,12 @@ def show
end

def create
saved_claim_request_body = request_body_hash.to_json # serialize before modifications are made to request body
nod_response_body = AppealSubmission.submit_nod(
current_user: @current_user,
request_body_hash:,
decision_review_service:,
version_number:
)
store_saved_claim(claim_class: SavedClaim::NoticeOfDisagreement, form: saved_claim_request_body,
guid: nod_response_body.dig('data', 'id'))

render json: nod_response_body
rescue => e
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/v1/supplemental_claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,15 @@ def process_submission

sc_response = decision_review_service.create_supplemental_claim(request_body: req_body_obj, user: @current_user)
submitted_appeal_uuid = sc_response.body.dig('data', 'id')
return if submitted_appeal_uuid.nil?

ActiveRecord::Base.transaction do
appeal_submission_id = create_appeal_submission(submitted_appeal_uuid, zip_from_frontend)
handle_saved_claim(form: saved_claim_request_body, guid: submitted_appeal_uuid, form4142:)

::Rails.logger.info(post_create_log_msg(appeal_submission_id:, submitted_appeal_uuid:))
handle_4142(request_body: req_body_obj, form4142:, appeal_submission_id:, submitted_appeal_uuid:)
submit_evidence(sc_evidence, appeal_submission_id, submitted_appeal_uuid) if sc_evidence.present?

handle_saved_claim(form: saved_claim_request_body, guid: submitted_appeal_uuid, form4142:)

# Only destroy InProgressForm after evidence upload step
# so that we still have references if a fatal error occurs before this step
clear_in_progress_form
Expand Down
14 changes: 11 additions & 3 deletions app/models/appeal_submission.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require 'decision_review/utilities/saved_claim/service'

class AppealSubmission < ApplicationRecord
include DecisionReview::SavedClaim::Service

APPEAL_TYPES = %w[HLR NOD SC].freeze
validates :user_uuid, :submitted_appeal_uuid, presence: true
belongs_to :user_account, dependent: nil, optional: true
Expand All @@ -21,16 +25,20 @@ def self.submit_nod(request_body_hash:, current_user:, decision_review_service:
board_review_option: request_body_hash['data']['attributes']['boardReviewOption'],
upload_metadata: decision_review_service.class.file_upload_metadata(current_user))

form = request_body_hash.to_json # serialize before modifications are made to request body
uploads_arr = request_body_hash.delete('nodUploads') || []
nod_response_body = decision_review_service.create_notice_of_disagreement(request_body: request_body_hash,
user: current_user)
.body
appeal_submission.submitted_appeal_uuid = nod_response_body.dig('data', 'id')

guid = nod_response_body.dig('data', 'id')
appeal_submission.submitted_appeal_uuid = guid
appeal_submission.save!

appeal_submission.store_saved_claim(claim_class: SavedClaim::NoticeOfDisagreement, form:, guid:)

# Clear in-progress form if submit was successful
InProgressForm.form_for_user('10182',
current_user)&.destroy!
InProgressForm.form_for_user('10182', current_user)&.destroy!

appeal_submission.enqueue_uploads(uploads_arr, current_user, version_number)
nod_response_body
Expand Down
4 changes: 0 additions & 4 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,6 @@ features:
actor_type: user
description: Enable using MAR 2024 revision of 200996 Higher Level Review form when submitting to EMMS for intake
enable_in_development: false
decision_review_form_store_saved_claims:
actor_type: user
description: Enable storage of SavedClaim records for Decision Review forms (HLR, NOD, SC)
enable_in_development: true
decision_review_saved_claim_hlr_status_updater_job_enabled:
actor_type: user
description: Enable job to set delete_date for completed SavedClaim::HigherLevelReviews
Expand Down
4 changes: 0 additions & 4 deletions lib/decision_review/utilities/saved_claim/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ module Service
].freeze

def store_saved_claim(claim_class:, form:, guid:, uploaded_forms: [])
return unless Flipper.enabled? :decision_review_form_store_saved_claims

raise "Invalid class type '#{claim_class}'" unless VALID_CLASS.include? claim_class

claim = claim_class.new(form:, guid:, uploaded_forms:)
claim.save!
rescue => e
Rails.logger.warn("DecisionReview:Error saving #{claim_class}", { guid:, error: e.message })
end
end
end
Expand Down
28 changes: 5 additions & 23 deletions spec/lib/decision_review/utilities/saved_claim/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,19 @@ class Service < ApplicationController
Rails.root.join('spec', 'fixtures', 'notice_of_disagreements', 'valid_NOD_create_request.json').read
end

context 'when an invalid class is passed in' do
before do
Flipper.enable :decision_review_form_store_saved_claims
end

it 'handles the exception' do
expect { described_class.new.store_saved_claim(claim_class: SavedClaim, guid:, form:) }.not_to raise_error
context 'when an invalid claim_class is passed in' do
it 'throws an exception' do
expect { described_class.new.store_saved_claim(claim_class: SavedClaim, guid:, form:) }
.to raise_error(RuntimeError, "Invalid class type 'SavedClaim'")
expect(claim_class.find_by(guid:)).to be_nil
end
end

context 'when flipper is enabled' do
before do
Flipper.enable :decision_review_form_store_saved_claims
end

context 'when a valid claim_class is passed in' do
it 'saves the SavedClaim record' do
described_class.new.store_saved_claim(claim_class:, guid:, form:)
expect(claim_class.find_by(guid:).form).to eq form
end
end

context 'when flipper is disabled' do
before do
Flipper.disable :decision_review_form_store_saved_claims
end

it 'does not save the SavedClaim record' do
described_class.new.store_saved_claim(claim_class:, guid:, form:)
expect(claim_class.find_by(guid:)).to be_nil
end
end
end
end
74 changes: 51 additions & 23 deletions spec/requests/v1/higher_level_reviews_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,58 @@ def personal_information_logs
end
end

it 'adds to the PersonalInformationLog when an exception is thrown' do
VCR.use_cassette('decision_review/HLR-CREATE-RESPONSE-422_V1') do
expect(personal_information_logs.count).to be 0

allow(Rails.logger).to receive(:error)
expect(Rails.logger).to receive(:error).with(error_log_args)
expect(Rails.logger).to receive(:error).with(
message: "Exception occurred while submitting Higher Level Review: #{extra_error_log_message}",
backtrace: anything
)
expect(Rails.logger).to receive(:error).with(extra_error_log_message, anything)
allow(StatsD).to receive(:increment)
expect(StatsD).to receive(:increment).with('decision_review.form_996.overall_claim_submission.failure')
context 'when an error occurs with the api call' do
it 'adds to the PersonalInformationLog' do
VCR.use_cassette('decision_review/HLR-CREATE-RESPONSE-422_V1') do
expect(personal_information_logs.count).to be 0

subject
expect(personal_information_logs.count).to be 1
pil = personal_information_logs.first
%w[
first_name last_name birls_id icn edipi mhv_correlation_id
participant_id vet360_id ssn assurance_level birth_date
].each { |key| expect(pil.data['user'][key]).to be_truthy }
%w[message backtrace key response_values original_status original_body]
.each { |key| expect(pil.data['error'][key]).to be_truthy }
expect(pil.data['additional_data']['request']['body']).not_to be_empty
allow(Rails.logger).to receive(:error)
expect(Rails.logger).to receive(:error).with(error_log_args)
expect(Rails.logger).to receive(:error).with(
message: "Exception occurred while submitting Higher Level Review: #{extra_error_log_message}",
backtrace: anything
)
expect(Rails.logger).to receive(:error).with(extra_error_log_message, anything)
allow(StatsD).to receive(:increment)
expect(StatsD).to receive(:increment).with('decision_review.form_996.overall_claim_submission.failure')

subject
expect(personal_information_logs.count).to be 1
pil = personal_information_logs.first
%w[
first_name last_name birls_id icn edipi mhv_correlation_id
participant_id vet360_id ssn assurance_level birth_date
].each { |key| expect(pil.data['user'][key]).to be_truthy }
%w[message backtrace key response_values original_status original_body]
.each { |key| expect(pil.data['error'][key]).to be_truthy }
expect(pil.data['additional_data']['request']['body']).not_to be_empty
end
end
end

context 'when an error occurs in the transaction' do
shared_examples 'rolledback transaction' do |model|
before do
allow_any_instance_of(model).to receive(:save!).and_raise(ActiveModel::Error) # stub a model error
end

it 'rollsback transaction' do
VCR.use_cassette('decision_review/HLR-CREATE-RESPONSE-200_V1') do
expect(subject).to eq 500

# check that transaction rolled back / records were not persisted
expect(AppealSubmission.count).to eq 0
expect(SavedClaim.count).to eq 0
end
end
end

context 'for AppealSubmission' do
it_behaves_like 'rolledback transaction', AppealSubmission
end

context 'for SavedClaim' do
it_behaves_like 'rolledback transaction', SavedClaim
end
end
end
Expand Down
34 changes: 26 additions & 8 deletions spec/requests/v1/notice_of_disagreements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,35 @@ def personal_information_logs
expect(AppealSubmission.count).to eq 0
expect(AppealSubmissionUpload.count).to eq 0
expect(DecisionReview::SubmitUpload).not_to have_enqueued_sidekiq_job(anything)

expect(SavedClaim.count).to eq 0
end
end

it 'properly rollsback transaction if error occurs in wrapped code' do
allow_any_instance_of(AppealSubmission).to receive(:save!).and_raise(ActiveModel::Error) # stub a model error
VCR.use_cassette('decision_review/NOD-CREATE-RESPONSE-200_V1') do
subject
# check that transaction rolled back / records were not persisted / evidence upload job was not queued up
expect(AppealSubmission.count).to eq 0
expect(AppealSubmissionUpload.count).to eq 0
expect(DecisionReview::SubmitUpload).not_to have_enqueued_sidekiq_job(anything)
context 'when an error occurs in wrapped code' do
shared_examples 'rolledback transaction' do |model|
before do
allow_any_instance_of(model).to receive(:save!).and_raise(ActiveModel::Error) # stub a model error
end

it 'rollsback transaction' do
VCR.use_cassette('decision_review/NOD-CREATE-RESPONSE-200_V1') do
expect(subject).to eq 500
# check that transaction rolled back / records were not persisted / evidence upload job was not queued up
expect(AppealSubmission.count).to eq 0
expect(AppealSubmissionUpload.count).to eq 0
expect(DecisionReview::SubmitUpload).not_to have_enqueued_sidekiq_job(anything)
expect(SavedClaim.count).to eq 0
end
end
end

context 'for AppealSubmission' do
it_behaves_like 'rolledback transaction', AppealSubmission
end

context 'for SavedClaim' do
it_behaves_like 'rolledback transaction', SavedClaim
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/v1/supplemental_claims_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,33 @@ def personal_information_logs
end
end
end

context 'when an error occurs in the transaction' do
shared_examples 'rolledback transaction' do |model|
before do
allow_any_instance_of(model).to receive(:save!).and_raise(ActiveModel::Error) # stub a model error
end

it 'rollsback transaction' do
VCR.use_cassette('decision_review/SC-CREATE-RESPONSE-WITH-UPLOADS-200_V1') do
expect(subject).to eq 500

# check that transaction rolled back / records were not persisted / evidence upload job was not queued up
expect(AppealSubmission.count).to eq 0
expect(AppealSubmissionUpload.count).to eq 0
expect(DecisionReview::SubmitUpload).not_to have_enqueued_sidekiq_job(anything)
expect(SavedClaim.count).to eq 0
end
end
end

context 'for AppealSubmission' do
it_behaves_like 'rolledback transaction', AppealSubmission
end

context 'for SavedClaim' do
it_behaves_like 'rolledback transaction', SavedClaim
end
end
end
end

0 comments on commit f68a9f9

Please sign in to comment.