Skip to content

Commit

Permalink
Expand NPQ Funding API call to include targeted funding
Browse files Browse the repository at this point in the history
api/v1/npq-funding/:id wil now return previously_received_targeted_funding_support in its response. This will be true if the user has any accepted applications where targeted_delivery_funding_eligibility is true
  • Loading branch information
Lockyy committed Dec 2, 2022
1 parent 86bc026 commit 51ec6a4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 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
27 changes: 20 additions & 7 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 @@ -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
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
45 changes: 45 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,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

Expand All @@ -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

Expand All @@ -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) }
Expand All @@ -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
Expand Down

0 comments on commit 51ec6a4

Please sign in to comment.