diff --git a/app/queries/hearing_request_distribution_query.rb b/app/queries/hearing_request_distribution_query.rb index c03c7aa6890..646836cf5f6 100644 --- a/app/queries/hearing_request_distribution_query.rb +++ b/app/queries/hearing_request_distribution_query.rb @@ -53,6 +53,7 @@ def not_genpop_appeals ama_non_aod_hearing_query.or(ama_aod_hearing_query).uniq end + # rubocop:disable Metrics/AbcSize, Metrics/MethodLength def only_genpop_appeals ama_non_aod_hearing_query = generate_ama_only_genpop_non_aod_hearing_query(base_relation) ama_aod_hearing_query = generate_ama_only_genpop_aod_hearing_query(base_relation) @@ -79,9 +80,16 @@ def only_genpop_appeals # the base result is doing an inner join with hearings so it isn't retrieving any appeals that have no hearings # yet, so we add with_no_hearings to retrieve those appeals no_hearings_or_no_held_hearings = with_no_hearings.or(with_no_held_hearings) + no_hearings_or_only_no_held_hearings = [] + no_hearings_or_no_held_hearings.each do |appeal| + if appeal.hearings.blank? || appeal.hearings.pluck(:disposition).exclude?("held") + no_hearings_or_only_no_held_hearings << appeal + end + end - [*result, *no_hearings_or_no_held_hearings].uniq + [*result, *no_hearings_or_only_no_held_hearings].uniq end + # rubocop:enable Metrics/AbcSize, Metrics/MethodLength def generate_ama_not_genpop_non_aod_hearing_query(base_relation) query = diff --git a/spec/models/dockets/hearing_request_docket_spec.rb b/spec/models/dockets/hearing_request_docket_spec.rb index 3650e461b4d..2fce445d47d 100644 --- a/spec/models/dockets/hearing_request_docket_spec.rb +++ b/spec/models/dockets/hearing_request_docket_spec.rb @@ -308,6 +308,38 @@ end end + context "no_held_hearings" do + before { FeatureToggle.enable!(:acd_exclude_from_affinity) } + + let!(:judge_1) { create(:user, :judge, :with_vacols_judge_record) } + let!(:judge_2) { create(:user, :judge, :with_vacols_judge_record) } + + let!(:appeal_1_hearing) do + create(:appeal, + :hearing_docket, + :with_post_intake_tasks, + :held_hearing_and_ready_to_distribute, + tied_judge: judge_1) + end + + let!(:appeal_2_hearing) do + create(:appeal, + :hearing_docket, + :with_post_intake_tasks, + :held_hearing_and_ready_to_distribute, + tied_judge: judge_1) + end + + let!(:hearing) { create(:hearing, :postponed, appeal: appeal_2_hearing) } + + it "appeals with a held hearing aren't distributed to genpop" do + one_result = HearingRequestDocket.new.age_of_n_oldest_nonpriority_appeals_available_to_judge(judge_1, 3) + expect(one_result.count).to eq(2) + two_result = HearingRequestDocket.new.age_of_n_oldest_nonpriority_appeals_available_to_judge(judge_2, 3) + expect(two_result.count).to eq(0) + end + end + context "limit appeals class methods" do let(:appeal_1_week_old) { create_ready_aod_appeal(created_date: 1.week.ago) } let(:appeal_4_weeks_old) { create_ready_aod_appeal(created_date: 4.weeks.ago) }