diff --git a/app/models/npq_application.rb b/app/models/npq_application.rb index 32f8942bb8..3fadf8e574 100644 --- a/app/models/npq_application.rb +++ b/app/models/npq_application.rb @@ -38,6 +38,8 @@ class NPQApplication < ApplicationRecord rejected: "rejected", } + scope :with_targeted_delivery_funding_eligibility, -> { where(targeted_delivery_funding_eligibility: true) } + validates :eligible_for_funding_before_type_cast, inclusion: { in: [true, false, "true", "false"] } delegate :start_year, to: :cohort, prefix: true, allow_nil: true diff --git a/app/services/npq/funding_eligibility.rb b/app/services/npq/funding_eligibility.rb index a00ffc2591..d37af2c6a4 100644 --- a/app/services/npq/funding_eligibility.rb +++ b/app/services/npq/funding_eligibility.rb @@ -12,6 +12,7 @@ def initialize(trn:, npq_course_identifier:) def call { previously_funded: previously_funded?, + previously_received_targeted_funding_support: previously_received_targeted_funding_support?, } end @@ -33,14 +34,26 @@ def teacher_profiles @teacher_profiles ||= TeacherProfile.where(trn:) end - def previously_funded? - users.flat_map.any? do |user| - user.npq_applications - .where(npq_course: all_npq_courses) - .where(eligible_for_funding: true) - .accepted - .any? + def accepted_applications + @accepted_applications ||= begin + application_ids = users.flat_map do |user| + user.npq_applications + .where(npq_course: all_npq_courses) + .where(eligible_for_funding: true) + .accepted + .pluck(:id) + end + + NPQApplication.where(id: application_ids) end end + + def previously_funded? + accepted_applications.any? + end + + def previously_received_targeted_funding_support? + accepted_applications.with_targeted_delivery_funding_eligibility.any? + end end end diff --git a/spec/factories/services/npq/npq_application.rb b/spec/factories/services/npq/npq_application.rb index ef46335c1a..97144f7252 100644 --- a/spec/factories/services/npq/npq_application.rb +++ b/spec/factories/services/npq/npq_application.rb @@ -21,8 +21,8 @@ eligible_for_funding { false } funding_eligiblity_status_code { :ineligible_establishment_type } targeted_delivery_funding_eligibility { false } - teacher_catchment { "england" } - teacher_catchment_country { nil } + teacher_catchment { "england" } + teacher_catchment_country { nil } initialize_with do NPQ::BuildApplication.call( diff --git a/spec/services/npq/funding_eligibility_spec.rb b/spec/services/npq/funding_eligibility_spec.rb index 93a6e368e7..73bbe84af8 100644 --- a/spec/services/npq/funding_eligibility_spec.rb +++ b/spec/services/npq/funding_eligibility_spec.rb @@ -19,6 +19,7 @@ it "returns falsey" do expect(subject.call[:previously_funded]).to be_falsey + expect(subject.call[:previously_received_targeted_funding_support]).to be_falsey end end @@ -39,6 +40,29 @@ it "returns truthy" do expect(subject.call[:previously_funded]).to be_truthy + expect(subject.call[:previously_received_targeted_funding_support]).to be_falsey + end + end + + context "when previously funded with targeted delivery funding" do + let(:trn) { application.teacher_reference_number } + let(:application) do + create( + :npq_application, + eligible_for_funding: true, + teacher_reference_number_verified: true, + targeted_delivery_funding_eligibility: true, + ) + end + let(:npq_course) { application.npq_course } + + before do + NPQ::Application::Accept.new(npq_application: application).call + end + + it "returns truthy" do + expect(subject.call[:previously_funded]).to be_truthy + expect(subject.call[:previously_received_targeted_funding_support]).to be_truthy end end @@ -64,6 +88,7 @@ it "returns truthy" do expect(subject.call[:previously_funded]).to be_truthy + expect(subject.call[:previously_received_targeted_funding_support]).to be_falsey end end @@ -85,9 +110,28 @@ it "returns truthy" do expect(subject.call[:previously_funded]).to be_truthy + expect(subject.call[:previously_received_targeted_funding_support]).to be_falsey end end + context "when previously funded with targeted delivery funding but not accepted" do + let(:trn) { application.teacher_reference_number } + let(:application) do + create( + :npq_application, + eligible_for_funding: true, + teacher_reference_number_verified: true, + targeted_delivery_funding_eligibility: true, + ) + end + let(:npq_course) { application.npq_course } + + it "returns truthy" do + expect(subject.call[:previously_funded]).to be_falsey + expect(subject.call[:previously_received_targeted_funding_support]).to be_falsey + end +end + context "when trn does not yield any teachers" do let(:trn) { "0000000" } let(:npq_course) { create(:npq_course) } @@ -96,6 +140,7 @@ it "returns falsey" do expect(subject.call[:previously_funded]).to be_falsey + expect(subject.call[:previously_received_targeted_funding_support]).to be_falsey end end end