Skip to content

Commit

Permalink
Merge pull request DFE-Digital#2794 from DFE-Digital/CPDNPQ-723
Browse files Browse the repository at this point in the history
[CPDNPQ-723] Expand NPQ Funding API call to include targeted funding
  • Loading branch information
Lockyy authored Dec 6, 2022
2 parents 43563cc + 3a1b65c commit d069f91
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/models/npq_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions app/services/npq/funding_eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,7 +22,7 @@ def npq_course
@npq_course ||= NPQCourse.find_by!(identifier: npq_course_identifier)
end

def all_npq_courses
def npq_course_and_rebranded_alternatives
npq_course.rebranded_alternative_courses
end

Expand All @@ -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: npq_course_and_rebranded_alternatives)
.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
4 changes: 2 additions & 2 deletions spec/factories/services/npq/npq_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
68 changes: 68 additions & 0 deletions spec/services/npq/funding_eligibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,6 +40,52 @@

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 for a different course" do
let(:trn) { application.teacher_reference_number }
let(:application) do
create(
:npq_application,
eligible_for_funding: true,
teacher_reference_number_verified: true,
)
end
let(:npq_application_course) { application.npq_course }
# Making sure they are completely separate courses
let(:npq_course) { create(:npq_course, identifier: npq_application_course.identifier.reverse) }

before do
NPQ::Application::Accept.new(npq_application: application).call
end

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 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

Expand All @@ -64,6 +111,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

Expand All @@ -85,6 +133,25 @@

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

Expand All @@ -96,6 +163,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
Expand Down

0 comments on commit d069f91

Please sign in to comment.