Skip to content

Commit

Permalink
Merge pull request #4858 from DFE-Digital/3145-expose-multi-cohort-fl…
Browse files Browse the repository at this point in the history
…ag-to-providers

Flag multi cohort participants to providers
  • Loading branch information
ethax-ross authored Jun 5, 2024
2 parents 4ba1f13 + 4f990f7 commit 3cd946f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 17 deletions.
4 changes: 4 additions & 0 deletions app/models/cohort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def previous
self.class.find_by(start_year: start_year - 1)
end

def freeze_payments!
update!(payments_frozen_at: Time.zone.now)
end

# e.g. "2022"
def to_param
start_year.to_s
Expand Down
10 changes: 10 additions & 0 deletions app/models/participant_profile/ecf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ def archivable?
self.class.archivable(restrict_to_participant_ids: [id]).exists?
end

def previous_payments_frozen_cohort
return nil unless cohort_changed_after_payments_frozen?

participant_declarations
.includes(:cohort)
.where.not(cohort: { payments_frozen_at: nil })
.where.not(cohort: schedule.cohort)
.pick("cohort.start_year")
end

def completed_induction?
induction_completion_date.present?
end
Expand Down
5 changes: 1 addition & 4 deletions app/presenters/admin/participant_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ def detailed_cohort_information

return current_cohort unless participant_profile.cohort_changed_after_payments_frozen

original_cohort = participant_profile.participant_declarations
.includes(:cohort)
.where.not(cohort: { start_year: current_cohort })
.pick("cohort.start_year")
original_cohort = participant_profile.previous_payments_frozen_cohort

"#{current_cohort} (migrated after #{original_cohort || 'unknown cohort'} payments were frozen)"
end
Expand Down
4 changes: 3 additions & 1 deletion app/serializers/api/v3/ecf/participant_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def latest_induction_record(user:, profile:, lead_provider:)
created_at: profile.created_at.rfc3339,
induction_end_date: profile.induction_completion_date&.strftime("%Y-%m-%d"),
mentor_funding_end_date: profile.mentor_completion_date&.strftime("%Y-%m-%d"),
}
}.tap do |hash|
hash[:cohort_changed_after_payments_frozen] = profile.cohort_changed_after_payments_frozen if FeatureFlag.active?(:cohort_changed_after_payments_frozen)
end
}.compact
end

Expand Down
1 change: 1 addition & 0 deletions app/services/feature_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def feature
prevent_2023_ect_registrations
school_participant_status_language
npq_capping
cohort_changed_after_payments_frozen
].freeze

FEATURES = (PERMANENT_SETTINGS + TEMPORARY_FEATURE_FLAGS).index_with { |name|
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/cohorts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
start_year { Date.current.year + (Date.current.month < 9 ? 0 : 1) }
end

trait :payments_frozen do
payments_frozen_at { Time.zone.now }
end

trait :consecutive_years do
start_year { generate(:base_year) }
end
Expand Down
10 changes: 10 additions & 0 deletions spec/models/cohort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@
end
end

describe "#freeze_payments!" do
let(:cohort) { Cohort.previous }

it "sets payments_frozen_at to the current time" do
freeze_time do
expect { cohort.freeze_payments! }.to change { cohort.payments_frozen_at }.from(nil).to(Time.zone.now)
end
end
end

describe "#schedules" do
subject { described_class.create!(start_year: 3000) }

Expand Down
20 changes: 20 additions & 0 deletions spec/models/participant_profile/ecf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@
end
end

describe "#previous_payments_frozen_cohort" do
let(:participant_profile) { create(:ect) }

subject { participant_profile.previous_payments_frozen_cohort }

it { is_expected.to be_nil }

context "when payments were frozen on a previous cohort" do
let(:previous_cohort) { Cohort.previous.tap(&:freeze_payments!) }
let(:cpd_lead_provider) { participant_profile.lead_provider.cpd_lead_provider }
let(:participant_profile) { create(:ect, cohort_changed_after_payments_frozen: true) }

before do
create(:participant_declaration, participant_profile:, cohort: previous_cohort, state: :paid, cpd_lead_provider:, course_identifier: "ecf-induction")
end

it { is_expected.to eq(previous_cohort.start_year) }
end
end

describe "#withdrawn_for" do
let(:cpd_lead_provider) { subject.induction_records.latest&.cpd_lead_provider }

Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/admin/participant_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
end

context "when the previous cohort can be determined" do
let(:previous_cohort) { Cohort.previous }
let(:previous_cohort) { Cohort.previous.tap(&:freeze_payments!) }
let(:cpd_lead_provider) { participant_profile.lead_provider.cpd_lead_provider }
let(:course_identifier) { "ecf-induction" }

Expand Down
44 changes: 36 additions & 8 deletions spec/serializers/api/v3/ecf/participant_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ module ECF
let(:school_cohort) { create(:school_cohort, :fip, :with_induction_programme, delivery_partner:, school:, cohort:, lead_provider: cpd_lead_provider.lead_provider) }
let!(:provider_relationship) { create(:provider_relationship, cohort:, delivery_partner:, lead_provider: cpd_lead_provider.lead_provider) }
let(:participant) { create(:user) }
let!(:ect_profile) { create(:ect, :eligible_for_funding, school_cohort:, user: participant, induction_completion_date: Date.parse("2022-01-12")) }
let!(:ect_profile) { create(:ect, :eligible_for_funding, school_cohort:, user: participant, induction_completion_date: Date.parse("2022-01-12"), cohort_changed_after_payments_frozen: true) }
let(:previous_cohort) { Cohort.previous.tap(&:freeze_payments!) }
let!(:previous_cohort_declaration) { create(:participant_declaration, participant_profile: ect_profile, cohort: previous_cohort, state: :paid, cpd_lead_provider:, course_identifier: "ecf-induction") }
let(:ecf_enrolments) { result[:data][0][:attributes][:ecf_enrolments] }

before { freeze_time }

Expand Down Expand Up @@ -64,25 +67,50 @@ module ECF
])
end

context "when the cohort_changed_after_payments_frozen feature flag is active" do
before { FeatureFlag.activate(:cohort_changed_after_payments_frozen) }

it "includes the cohort_changed_after_payments_frozen" do
expect(ecf_enrolments[0][:cohort_changed_after_payments_frozen]).to be(true)
end

context "when the participant has not changed cohort after payments were frozen" do
before { ect_profile.update!(cohort_changed_after_payments_frozen: false) }

it "does not include the cohort_changed_after_payments_frozen" do
expect(ecf_enrolments[0][:cohort_changed_after_payments_frozen]).to be(false)
end
end
end

context "when the previous_payments_frozen feature flag is not active" do
before { FeatureFlag.deactivate(:cohort_changed_after_payments_frozen) }

it "does not include the cohort_changed_after_payments_frozen" do
expect(ecf_enrolments[0]).not_to have_key(:cohort_changed_after_payments_frozen)
end
end

describe "ecf_enrolments" do
context "when there are multiple providers involved" do
let(:another_school_cohort) { create(:school_cohort, :fip, :with_induction_programme) }
let!(:mentor_profile) { create(:mentor, school_cohort: another_school_cohort, user: participant) }

it "only includes enrolments from the querying provider" do
expect(result[:data][0][:attributes][:ecf_enrolments].size).to be(1)
expect(ecf_enrolments.size).to be(1)
end
end

context "when there are multiple profiles involved" do
let!(:mentor_profile) { create(:mentor, school_cohort:, user: participant) }
let(:mentor_enrolement) { ecf_enrolments.find { |efce| efce[:participant_type] == :mentor } }

before do
mentor_profile.update!(mentor_completion_date: Date.new(2021, 4, 19))
end

it "includes the second profile data" do
expect(result[:data][0][:attributes][:ecf_enrolments].find { |efce| efce[:participant_type] == :mentor }).to eq({
expect(mentor_enrolement).to eq({
training_record_id: mentor_profile.id,
email: participant.email,
mentor_id: nil,
Expand Down Expand Up @@ -113,8 +141,8 @@ module ECF
end

it "includes a withdrawal object" do
expect(result[:data][0][:attributes][:ecf_enrolments][0][:withdrawal][:reason]).to eq("other")
expect(result[:data][0][:attributes][:ecf_enrolments][0][:withdrawal][:date]).to eql(ect_profile.participant_profile_state.created_at.rfc3339)
expect(ecf_enrolments[0][:withdrawal][:reason]).to eq("other")
expect(ecf_enrolments[0][:withdrawal][:date]).to eql(ect_profile.participant_profile_state.created_at.rfc3339)
end
end

Expand All @@ -125,8 +153,8 @@ module ECF
end

it "includes a deferral object" do
expect(result[:data][0][:attributes][:ecf_enrolments][0][:deferral][:reason]).to eq("other")
expect(result[:data][0][:attributes][:ecf_enrolments][0][:deferral][:date]).to eql(ect_profile.participant_profile_state.created_at.rfc3339)
expect(ecf_enrolments[0][:deferral][:reason]).to eq("other")
expect(ecf_enrolments[0][:deferral][:date]).to eql(ect_profile.participant_profile_state.created_at.rfc3339)
end
end

Expand All @@ -143,7 +171,7 @@ module ECF
subject { described_class.new([participant_from_query], params: { cpd_lead_provider: }) }

it "selects the latest induction record correctly" do
expect(result[:data][0][:attributes][:ecf_enrolments][0][:email]).to eq(latest_induction_record.preferred_identity.email)
expect(ecf_enrolments[0][:email]).to eq(latest_induction_record.preferred_identity.email)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_examples/archivable_support.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.shared_examples "can archive participant profile" do |other_participant_type, completed_training_at_attribute|
let(:eligible_cohort) { create(:cohort, :current, payments_frozen_at: Time.zone.now) }
let(:eligible_cohort) { create(:cohort, :current, :payments_frozen) }
let(:ineligible_cohort) { eligible_cohort.next }

def build_profile(attrs = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let(:cohort) { Cohort.active_registration_cohort }

before do
current_cohort.update!(payments_frozen_at: Time.zone.now)
current_cohort.freeze_payments!

# Extra declaration on the same participant to ensure the results are distinct.
create(declaration_type, :payable, declaration_type: :"retained-1", participant_profile: eligible_participant, cpd_lead_provider:)
Expand Down Expand Up @@ -51,7 +51,7 @@
let(:current_cohort) { participant_profile.schedule.cohort }
let(:cohort) { Cohort.active_registration_cohort }

before { current_cohort.update!(payments_frozen_at: Time.zone.now) }
before { current_cohort.freeze_payments! }

subject { participant_profile }

Expand Down

0 comments on commit 3cd946f

Please sign in to comment.