Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ltello committed Oct 17, 2024
1 parent 9231258 commit 49239aa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
6 changes: 6 additions & 0 deletions app/models/participant_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class ParticipantDeclaration < ApplicationRecord
self.ignored_columns = %w[statement_type statement_id voided_at]

ARCHIVABLE_STATES = %w[ineligible voided submitted].freeze

belongs_to :cpd_lead_provider
belongs_to :user
belongs_to :cohort
Expand Down Expand Up @@ -112,6 +114,10 @@ class ParticipantDeclaration < ApplicationRecord

before_create :build_initial_declaration_state

def self.non_archivable_states
states.keys.excluding(ARCHIVABLE_STATES)
end

def voidable?
%w[submitted eligible payable ineligible].include?(state)
end
Expand Down
4 changes: 1 addition & 3 deletions app/models/participant_profile/ect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class ParticipantProfile::ECT < ParticipantProfile::ECF
}

def self.archivable_from_frozen_cohort(restrict_to_participant_ids: [])
archivable_states = %i[ineligible voided submitted].freeze

# ECTs that have no FIP induction records
not_fip = InductionRecord.joins(:induction_programme, participant_profile: { schedule: :cohort })
.where.not(cohorts: { payments_frozen_at: nil })
Expand All @@ -29,7 +27,7 @@ def self.archivable_from_frozen_cohort(restrict_to_participant_ids: [])
.where(induction_completion_date: nil)
.where("induction_start_date IS NULL OR induction_start_date < make_date(cohorts.start_year, 9, 1)")
.where.not(id: not_fip_ids)
.where.not(participant_declarations: { state: archivable_states })
.where(participant_declarations: { state: ParticipantDeclaration.non_archivable_states })
.distinct
with_unarchivable_declaration = with_unarchivable_declaration.where(id: restrict_to_participant_ids) if restrict_to_participant_ids.any?
with_unarchivable_declaration_ids = with_unarchivable_declaration.pluck(:id)
Expand Down
4 changes: 1 addition & 3 deletions app/models/participant_profile/mentor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class ParticipantProfile::Mentor < ParticipantProfile::ECF
}

def self.archivable_from_frozen_cohort(restrict_to_participant_ids: [])
archivable_states = %i[ineligible voided submitted].freeze

# Mentors that have no FIP induction records
not_fip = InductionRecord.joins(:induction_programme, participant_profile: { schedule: :cohort })
.where.not(cohorts: { payments_frozen_at: nil })
Expand All @@ -38,7 +36,7 @@ def self.archivable_from_frozen_cohort(restrict_to_participant_ids: [])
.where.not(cohorts: { payments_frozen_at: nil })
.where(mentor_completion_date: nil)
.where.not(id: not_fip_ids)
.where.not(participant_declarations: { state: archivable_states })
.where(participant_declarations: { state: ParticipantDeclaration.non_archivable_states })
.distinct
with_unarchivable_declaration = with_unarchivable_declaration.where(id: restrict_to_participant_ids) if restrict_to_participant_ids.any?
with_unarchivable_declaration_ids = with_unarchivable_declaration.pluck(:id)
Expand Down
26 changes: 14 additions & 12 deletions spec/support/shared_examples/archivable_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,20 @@ def build_declaration(attrs = {})
expect(participant_profile).not_to be_archivable_from_frozen_cohort
end

it "returns false if the participant has not archivable declarations" do
paid_declaration = build_declaration(state: :paid, cohort: eligible_cohort)
participant_profile = paid_declaration.participant_profile
declaration_date = participant_profile.schedule.milestones.find_by(declaration_type: "retained-2").milestone_date - 1.day
travel_to declaration_date do
build_declaration(state: :voided,
cohort: eligible_cohort,
participant_profile:,
cpd_lead_provider: paid_declaration.cpd_lead_provider,
declaration_date:,
declaration_type: "retained-2")
expect(participant_profile).not_to be_archivable_from_frozen_cohort
ParticipantDeclaration.non_archivable_states.each do |declaration_state|
it "returns false if the participant has #{declaration_state} declarations" do
paid_declaration = build_declaration(state: declaration_state, cohort: eligible_cohort)
participant_profile = paid_declaration.participant_profile
declaration_date = participant_profile.schedule.milestones.find_by(declaration_type: "retained-2").milestone_date - 1.day
travel_to declaration_date do
build_declaration(state: :voided,
cohort: eligible_cohort,
participant_profile:,
cpd_lead_provider: paid_declaration.cpd_lead_provider,
declaration_date:,
declaration_type: "retained-2")
expect(participant_profile).not_to be_archivable_from_frozen_cohort
end
end
end

Expand Down

0 comments on commit 49239aa

Please sign in to comment.