Skip to content

Commit

Permalink
Merge pull request #5325 from DFE-Digital/CPDLP-3738
Browse files Browse the repository at this point in the history
[CPDLP-3738] Make declarations back to eligible when moving to an eligible statement
  • Loading branch information
leandroalemao authored Nov 21, 2024
2 parents c28e2ce + 8f67e7b commit f6b58d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ PLATFORMS
arm64-darwin-21
arm64-darwin-22
arm64-darwin-23
arm64-darwin-24
x86_64-darwin-22
x86_64-darwin-23
x86_64-linux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def migrate_line_items!(provider, from_statement, to_statement)
record_info("Migrating #{statement_line_items.size} declarations for #{provider.name}")
statement_line_items.update!(statement_id: to_statement.id)

change_declaration_states_for_to_statement(to_statement, statement_line_items)
make_eligible_declaration_payable_for_to_statement(to_statement, statement_line_items)
make_payable_declaration_eligible_for_to_statement(to_statement, statement_line_items)
end

def each_statements_by_provider
Expand All @@ -96,7 +97,7 @@ def each_statements_by_provider
end
end

def change_declaration_states_for_to_statement(to_statement, statement_line_items)
def make_eligible_declaration_payable_for_to_statement(to_statement, statement_line_items)
declarations = statement_line_items.map(&:participant_declaration).uniq
eligible_declarations = declarations.select(&:eligible?)

Expand All @@ -111,6 +112,19 @@ def change_declaration_states_for_to_statement(to_statement, statement_line_item
eligible_declarations.each { |declaration| service.call(declaration) }
end

def make_payable_declaration_eligible_for_to_statement(to_statement, statement_line_items)
declarations = statement_line_items.map(&:participant_declaration).uniq
payable_declarations = declarations.select(&:payable?)

return if to_statement.payable? || to_statement.paid?
return unless payable_declarations.any?

record_info("Marking #{payable_declarations.size} payable declarations back as eligible for #{to_statement.name} statement")

payable_declarations.each { |declaration| DeclarationState.eligible!(declaration) }
statement_line_items.select(&:payable?).map(&:eligible!)
end

def filter_statement_line_items(statement_line_items)
scope = statement_line_items.includes(:participant_declaration)
scope = scope.where(participant_declaration: { declaration_type: restrict_to_declaration_types }) if restrict_to_declaration_types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,32 @@
end
end

context "when migrating to an eligible statement" do
let(:to_statement) { create(:npq_statement, name: "May 2023", cpd_lead_provider:, cohort:) }
let(:declaration) { create(:npq_participant_declaration, :payable, cohort:, cpd_lead_provider:) }
let(:from_statement) { declaration.statements.first }

it "migrates payable declarations to the new statement and makes them eligible" do
migrate

declaration.reload

expect(declaration.statement_line_items.map(&:statement)).to all(eq(to_statement))
expect(declaration).to be_eligible
expect(to_statement.statement_line_items.map(&:state)).to eq(%w[eligible])
end

it "records information" do
migrate

expect(instance).to have_recorded_info([
"Migrating declarations from #{from_statement_name} to #{to_statement_name} for 1 providers",
"Migrating 1 declarations for #{npq_lead_provider.name}",
"Marking 1 payable declarations back as eligible for #{to_statement_name} statement",
])
end
end

context "when migrating from a paid statement" do
let(:declaration) { create(:npq_participant_declaration, :paid, cohort:, cpd_lead_provider:) }
let(:from_statement) { declaration.statements.first }
Expand Down

0 comments on commit f6b58d8

Please sign in to comment.