Skip to content

Commit

Permalink
Pass cohort into eligibility methors rather than start year
Browse files Browse the repository at this point in the history
  • Loading branch information
ethax-ross committed May 28, 2024
1 parent f0ca8df commit ba07b97
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
16 changes: 7 additions & 9 deletions app/models/participant_profile/ecf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def self.ransackable_associations(_auth_object = nil)
%w[cohort participant_identity school user teacher_profile induction_records]
end

def self.eligible_to_change_cohort_and_continue_training(in_cohort_start_year:, restrict_to_participant_ids: [])
def self.eligible_to_change_cohort_and_continue_training(cohort:, restrict_to_participant_ids: [])
billable_states = %w[eligible payable paid].freeze

return none unless Cohort.find_by_start_year(in_cohort_start_year) == Cohort.active_registration_cohort
return none unless cohort == Cohort.active_registration_cohort

completed_billable_declarations = ParticipantDeclaration.billable.for_declaration(:completed)
completed_billable_declarations = completed_billable_declarations.where(participant_profile_id: restrict_to_participant_ids) if restrict_to_participant_ids.any?
Expand All @@ -72,14 +72,12 @@ def self.eligible_to_change_cohort_and_continue_training(in_cohort_start_year:,
query
end

def eligible_to_change_cohort_back_to_their_payments_frozen_original?(to_cohort_start_year:)
to_cohort = Cohort.find_by_start_year(to_cohort_start_year)

def eligible_to_change_cohort_back_to_their_payments_frozen_original?(cohort:)
return false unless cohort_changed_after_payments_frozen?
return false unless to_cohort.payments_frozen?
return false unless cohort.payments_frozen?
return false if participant_declarations.billable_or_changeable.where(cohort: schedule.cohort).exists?

participant_declarations.billable.where(cohort: to_cohort).exists?
participant_declarations.billable.where(cohort:).exists?
end

def self.archivable(restrict_to_participant_ids: [])
Expand All @@ -104,8 +102,8 @@ def self.archivable(restrict_to_participant_ids: [])
end

# Instance Methods
def eligible_to_change_cohort_and_continue_training?(in_cohort_start_year:)
self.class.eligible_to_change_cohort_and_continue_training(in_cohort_start_year:, restrict_to_participant_ids: [id]).exists?
def eligible_to_change_cohort_and_continue_training?(cohort:)
self.class.eligible_to_change_cohort_and_continue_training(cohort:, restrict_to_participant_ids: [id]).exists?
end

def archivable?
Expand Down
4 changes: 2 additions & 2 deletions app/models/participant_profile/ect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def role
"Early career teacher"
end

def self.eligible_to_change_cohort_and_continue_training(in_cohort_start_year:, restrict_to_participant_ids: [])
super(in_cohort_start_year:, restrict_to_participant_ids:).where(induction_completion_date: nil)
def self.eligible_to_change_cohort_and_continue_training(cohort:, restrict_to_participant_ids: [])
super(cohort:, restrict_to_participant_ids:).where(induction_completion_date: nil)
end
end
4 changes: 2 additions & 2 deletions app/models/participant_profile/mentor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def role
"Mentor"
end

def self.eligible_to_change_cohort_and_continue_training(in_cohort_start_year:, restrict_to_participant_ids: [])
super(in_cohort_start_year:, restrict_to_participant_ids:).where(mentor_completion_date: nil)
def self.eligible_to_change_cohort_and_continue_training(cohort:, restrict_to_participant_ids: [])
super(cohort:, restrict_to_participant_ids:).where(mentor_completion_date: nil)
end
end
4 changes: 2 additions & 2 deletions app/services/change_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def cohort
def can_change_cohort_leaving_billable_declarations?
return false unless participant_profile.ecf?
return false unless attempt_to_change_cohort_leaving_billable_declarations
return true if participant_profile.eligible_to_change_cohort_and_continue_training?(in_cohort_start_year: cohort.start_year)
return true if participant_profile.eligible_to_change_cohort_and_continue_training?(cohort:)

participant_profile.eligible_to_change_cohort_back_to_their_payments_frozen_original?(to_cohort_start_year: cohort.start_year)
participant_profile.eligible_to_change_cohort_back_to_their_payments_frozen_original?(cohort:)
end

def cohort_start_years_delta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let(:cpd_lead_provider) { eligible_participant.lead_provider.cpd_lead_provider }
let(:eligible_participants) { create_list(declaration_type, 3, :payable, declaration_type: :started, cohort: Cohort.previous).map(&:participant_profile) }
let(:eligible_participant) { eligible_participants.first }
let(:in_cohort_start_year) { Cohort.active_registration_cohort.start_year }
let(:cohort) { Cohort.active_registration_cohort }

before do
current_cohort.update!(payments_frozen_at: Time.zone.now)
Expand All @@ -33,9 +33,9 @@
end
end

subject { described_class.eligible_to_change_cohort_and_continue_training(in_cohort_start_year:, restrict_to_participant_ids:) }
subject { described_class.eligible_to_change_cohort_and_continue_training(cohort:, restrict_to_participant_ids:) }

it { expect(current_cohort.start_year).not_to eq(in_cohort_start_year) }
it { expect(current_cohort).not_to eq(cohort) }
it { is_expected.to contain_exactly(*eligible_participants) }

context "when restricted to a set of participant IDs" do
Expand All @@ -49,25 +49,25 @@
let(:declaration) { create(declaration_type, :paid, declaration_type: :started, cohort: Cohort.previous) }
let(:participant_profile) { declaration.participant_profile }
let(:current_cohort) { participant_profile.schedule.cohort }
let(:in_cohort_start_year) { Cohort.active_registration_cohort.start_year }
let(:cohort) { Cohort.active_registration_cohort }

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

subject { participant_profile }

it { expect(current_cohort.start_year).not_to eq(in_cohort_start_year) }
it { is_expected.to be_eligible_to_change_cohort_and_continue_training(in_cohort_start_year:) }
it { expect(current_cohort).not_to eq(cohort) }
it { is_expected.to be_eligible_to_change_cohort_and_continue_training(cohort:) }

context "when the participant is not in a payments frozen cohort" do
before { current_cohort.update!(payments_frozen_at: nil) }

it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(in_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(cohort:) }
end

context "when the cohort they intend to continue training in is not the active registration cohort" do
let(:in_cohort_start_year) { Cohort.active_registration_cohort.previous }
let(:cohort) { Cohort.active_registration_cohort.previous }

it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(in_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(cohort:) }
end

%i[paid payable eligible].each do |billable_declaration_type|
Expand All @@ -84,64 +84,64 @@
cpd_lead_provider: participant_profile.lead_provider.cpd_lead_provider)
end

it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(in_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(cohort:) }
end
end

context "when the participant does not have billable, not completed declarations" do
before { declaration.destroy }

it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(in_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(cohort:) }
end

context "when the participant has a #{completed_training_at_attribute}" do
before { participant_profile.update!("#{completed_training_at_attribute}": 1.month.ago) }

it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(in_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_and_continue_training(cohort:) }
end
end

describe "#eligible_to_change_cohort_back_to_their_payments_frozen_original?" do
let(:declaration) { create(declaration_type, :paid, declaration_type: :started) }
let(:participant_profile) { declaration.participant_profile }
let(:current_cohort) { participant_profile.schedule.cohort }
let(:previous_cohort) { create(:cohort, payments_frozen_at: 1.week.ago, start_year: to_cohort_start_year) }
let(:cohort) { create(:cohort, payments_frozen_at: 1.week.ago, start_year: to_cohort_start_year) }
let(:to_cohort_start_year) { current_cohort.start_year - 3 }

before do
declaration.update!(cohort: previous_cohort)
declaration.update!(cohort:)
participant_profile.update!(cohort_changed_after_payments_frozen: true)
previous_cohort.update!(payments_frozen_at: Time.zone.now)
cohort.update!(payments_frozen_at: Time.zone.now)
end

subject { participant_profile }

it { expect(current_cohort.start_year).not_to eq(to_cohort_start_year) }
it { is_expected.to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(to_cohort_start_year:) }
it { expect(current_cohort).not_to eq(cohort) }
it { is_expected.to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(cohort:) }

context "when the participant does not have billable declarations in the previous cohort" do
before { declaration.update!(state: :ineligible) }

it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(to_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(cohort:) }
end

context "when the previous cohort is not payments frozen" do
before { previous_cohort.update!(payments_frozen_at: nil) }
before { cohort.update!(payments_frozen_at: nil) }

it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(to_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(cohort:) }
end

context "when the participant is not cohort_changed_after_payments_frozen" do
before { participant_profile.update!(cohort_changed_after_payments_frozen: false) }

it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(to_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(cohort:) }
end

%i[paid payable eligible submitted].each do |billable_or_changeable_declaration_type|
context "when the participant has a #{billable_or_changeable_declaration_type} declaration for the current cohort" do
before { create(declaration_type, :payable, declaration_type: "retained-1", participant_profile:, cpd_lead_provider: participant_profile.lead_provider.cpd_lead_provider) }

it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(to_cohort_start_year:) }
it { is_expected.not_to be_eligible_to_change_cohort_back_to_their_payments_frozen_original(cohort:) }
end
end
end
Expand Down

0 comments on commit ba07b97

Please sign in to comment.