Skip to content

Commit

Permalink
Retry getting a rating profile on BGS::ShareError (#15316)
Browse files Browse the repository at this point in the history
connects #15259

### Description
Catches BGS::ShareError when fetching a rating profile, and re-tries fetching the rating profile with the other service.

### Acceptance Criteria
- [ ] When fetching rating profile on a PromulgatedRating, catch BGS Share errors ~with a similar message to above~
- [ ] Find the same rating using RatingAtIssue based on the profile date, and return it's rating profile

### Testing Plan
1. Automated test
  • Loading branch information
leikkisa authored Sep 29, 2020
1 parent a22fa11 commit 3642b8e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
23 changes: 21 additions & 2 deletions app/models/promulgated_rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def ratings_from_bgs_response(response)

attr_writer :rating_profile

def rating_profile
@rating_profile ||= fetch_rating_profile
end

private

def fetch_rating_profile
Expand All @@ -65,9 +69,24 @@ def fetch_rating_profile
)
rescue Savon::Error
{}
rescue BGS::ShareError
retry_fetching_rating_profile
end

def rating_profile
@rating_profile ||= fetch_rating_profile
# Re-tries fetching the rating profile with the RatingAtIssue service
def retry_fetching_rating_profile
ratings_at_issue = RatingAtIssue.fetch_in_range(
participant_id: participant_id,
start_date: profile_date,
end_date: profile_date
)
matching_rating = ratings_at_issue.find { |rating| profile_date_matches(rating.profile_date) }
matching_rating.present? ? matching_rating.rating_profile : {}
end

# The profile date is used as a key when fetching a rating by profile date.
# Profile dates in RatingAtIssue appear to have the same Date/Time stamp, but sometimes disagree by one timezone
def profile_date_matches(profile_date_to_compare)
profile_date.strftime("%Y-%m-%d %H:%M:%S") == profile_date_to_compare.strftime("%Y-%m-%d %H:%M:%S")
end
end
8 changes: 6 additions & 2 deletions lib/fakes/bgs_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ def fetch_ratings_in_range(participant_id:, start_date:, end_date:)
format_promulgated_rating(build_ratings_in_range(ratings, start_date, end_date))
end

def fetch_rating_profile(participant_id:, profile_date:)
stored_rating_profile(participant_id: participant_id, profile_date: profile_date)
end

def fetch_rating_profiles_in_range(participant_id:, start_date:, end_date:)
ratings = get_rating_record(participant_id)[:ratings] || []
# Simulate the response if participant doesn't exist or doesn't have any ratings
Expand Down Expand Up @@ -446,7 +450,7 @@ def rating_at_issue_profile_data(rating)
# If a PromulgatedRating was originally stored in rating_store
# convert to be compatible with RatingAtIssue
if promulgated_rating_data.present?
rating_profile = fetch_rating_profile(
rating_profile = stored_rating_profile(
participant_id: promulgated_rating_data[:ptcpnt_vet_id],
profile_date: promulgated_rating_data[:prfil_dt]
)
Expand All @@ -464,7 +468,7 @@ def rating_at_issue_profile_data(rating)
end
end

def fetch_rating_profile(participant_id:, profile_date:)
def stored_rating_profile(participant_id:, profile_date:)
normed_date_key = Fakes::RatingStore.normed_profile_date_key(profile_date).to_sym
rating_profile = (get_rating_record(participant_id)[:profiles] || {})[normed_date_key]

Expand Down
48 changes: 31 additions & 17 deletions spec/models/promulgated_rating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
# In comparison, Ratings at issue return both the rating and rating profile information from the RatingProfile service

describe PromulgatedRating do
context ".fetch_timely" do
let(:receipt_date) { Time.zone.today }
let(:receipt_date) { Time.zone.today }
let!(:rating) do
Generators::PromulgatedRating.build(
participant_id: "DRAYMOND",
promulgation_date: receipt_date - 370.days,
profile_date: receipt_date - 370.days
)
end

context ".fetch_timely" do
subject { PromulgatedRating.fetch_timely(participant_id: "DRAYMOND", from_date: receipt_date) }

let!(:rating) do
Generators::PromulgatedRating.build(
participant_id: "DRAYMOND",
promulgation_date: receipt_date - 371.days
)
end

let!(:untimely_rating) do
Generators::PromulgatedRating.build(
participant_id: "DRAYMOND",
Expand All @@ -32,7 +32,7 @@
let!(:another_rating) do
Generators::PromulgatedRating.build(
participant_id: "DRAYMOND",
promulgation_date: receipt_date - 370.days
promulgation_date: receipt_date - 371.days
)
end

Expand Down Expand Up @@ -66,13 +66,6 @@

subject { PromulgatedRating.fetch_all("DRAYMOND") }

let!(:rating) do
Generators::PromulgatedRating.build(
participant_id: "DRAYMOND",
promulgation_date: receipt_date - 370.days
)
end

let!(:untimely_rating) do
Generators::PromulgatedRating.build(
participant_id: "DRAYMOND",
Expand Down Expand Up @@ -116,4 +109,25 @@
)
end
end

context "#rating_profile" do
subject { rating.rating_profile }

context "when BGS throws a Share Error" do
let(:bgs) { Fakes::BGSService.new }

before do
allow(Fakes::BGSService).to receive(:new).and_return(bgs)
allow(bgs).to receive(:fetch_rating_profile)
.and_raise(BGS::ShareError, "Veteran does not meet the minimum disability requirements")
allow(bgs).to receive(:fetch_rating_profiles_in_range).and_call_original
end

it "Fetches the rating profile using RatingAtIssue" do
expect(subject.present?)
expect { rating.issues }.to_not raise_error
expect(bgs).to have_received(:fetch_rating_profiles_in_range)
end
end
end
end
4 changes: 0 additions & 4 deletions spec/models/rating_at_issue_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frozen_string_literal: true

describe RatingAtIssue do
before { FeatureToggle.enable!(:ratings_at_issue) }
after { FeatureToggle.disable!(:ratings_at_issue) }

let(:disability_sn) { "1234" }
let(:diagnostic_code) { "7611" }
let(:reference_id) { "1555" }
Expand Down Expand Up @@ -96,7 +93,6 @@
let!(:unpromulgated_rating) do
Generators::RatingAtIssue.build(
participant_id: "DRAYMOND",
promulgation_date: receipt_date - 100.years,
promulgation_date: nil
)
end
Expand Down

0 comments on commit 3642b8e

Please sign in to comment.