diff --git a/app/models/promulgated_rating.rb b/app/models/promulgated_rating.rb index 542344e8c61..fcaaf7dbea5 100644 --- a/app/models/promulgated_rating.rb +++ b/app/models/promulgated_rating.rb @@ -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 @@ -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 diff --git a/lib/fakes/bgs_service.rb b/lib/fakes/bgs_service.rb index c17516b5c11..e193354cb96 100644 --- a/lib/fakes/bgs_service.rb +++ b/lib/fakes/bgs_service.rb @@ -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 @@ -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] ) @@ -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] diff --git a/spec/models/promulgated_rating_spec.rb b/spec/models/promulgated_rating_spec.rb index 3790b079784..b7ff6ba11f5 100644 --- a/spec/models/promulgated_rating_spec.rb +++ b/spec/models/promulgated_rating_spec.rb @@ -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", @@ -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 @@ -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", @@ -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 diff --git a/spec/models/rating_at_issue_spec.rb b/spec/models/rating_at_issue_spec.rb index 40f39b6d14f..7947b1bad9a 100644 --- a/spec/models/rating_at_issue_spec.rb +++ b/spec/models/rating_at_issue_spec.rb @@ -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" } @@ -96,7 +93,6 @@ let!(:unpromulgated_rating) do Generators::RatingAtIssue.build( participant_id: "DRAYMOND", - promulgation_date: receipt_date - 100.years, promulgation_date: nil ) end