From d8263922a109beb28008c900e09300d1372907d8 Mon Sep 17 00:00:00 2001 From: pskl Date: Tue, 12 Nov 2024 15:06:52 +0100 Subject: [PATCH] Make calculate_amount a private method --- app/controllers/pfmps_controller.rb | 4 +-- app/services/pfmp_manager.rb | 42 +++++++++++++++-------------- spec/services/pfmp_manager_spec.rb | 4 +-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/app/controllers/pfmps_controller.rb b/app/controllers/pfmps_controller.rb index 13876e5f3..8571a2a3b 100644 --- a/app/controllers/pfmps_controller.rb +++ b/app/controllers/pfmps_controller.rb @@ -20,9 +20,9 @@ def new def edit; end def create - @pfmp = Pfmp.new(pfmp_params.merge(schooling: @schooling)) + @pfmp = Pfmp.new - if PfmpManager.new(@pfmp).update(pfmp_params) + if PfmpManager.new(@pfmp).update(pfmp_params.merge(schooling: @schooling)) redirect_to student_path(@schooling.student), notice: t("pfmps.new.success") else diff --git a/app/services/pfmp_manager.rb b/app/services/pfmp_manager.rb index 3f00572a8..5beda5b59 100644 --- a/app/services/pfmp_manager.rb +++ b/app/services/pfmp_manager.rb @@ -22,10 +22,12 @@ def update(params) end def update!(params) + params = params.to_h.with_indifferent_access + Pfmp.transaction do pfmp.update!(params) - transition! recalculate_amounts! if params[:day_count].present? + transition! end end @@ -47,23 +49,14 @@ def retry_incomplete_payment_request! def rectify_and_update_attributes!(confirmed_pfmp_params, confirmed_address_params) Pfmp.transaction do - PfmpManager.new(pfmp).update!(confirmed_pfmp_params) + update!(confirmed_pfmp_params) pfmp.student.update!(confirmed_address_params) pfmp.rectify! end end - def calculate_amount - return 0 if pfmp.day_count.nil? - - [ - pfmp.day_count * pfmp.wage.daily_rate, - pfmp.wage.yearly_cap - previously_locked_amount - ].min - end - - def previously_locked_amount - other_priced_pfmps + def previously_locked_amount(pfmp) + other_priced_pfmps(pfmp) .map(&:amount) .compact .sum @@ -78,31 +71,40 @@ def retry_payment_request!(reasons) private + def calculate_amount(target_pfmp) + return 0 if target_pfmp.day_count.nil? + + [ + target_pfmp.day_count * target_pfmp.wage.daily_rate, + target_pfmp.wage.yearly_cap - previously_locked_amount(target_pfmp) + ].min + end + def recalculate_amounts! raise PfmpNotModifiableError unless pfmp.can_be_modified? pfmp.all_pfmps_for_mef.lock! - pfmp.update!(amount: calculate_amount) + pfmp.update!(amount: calculate_amount(pfmp)) rebalance_other_pfmps! end - def other_pfmps_for_mef - pfmp.all_pfmps_for_mef.excluding(pfmp) + def other_pfmps_for_mef(excluded_pfmp) + pfmp.all_pfmps_for_mef.excluding(excluded_pfmp) end - def other_priced_pfmps - other_pfmps_for_mef + def other_priced_pfmps(pfmp) + other_pfmps_for_mef(pfmp) .where.not(amount: nil) end def rebalancable_pfmps - @rebalancable_pfmps ||= other_pfmps_for_mef + @rebalancable_pfmps ||= other_pfmps_for_mef(pfmp) .select(&:can_be_rebalanced?) end def rebalance_other_pfmps! rebalancable_pfmps.each do |rebalancable_pfmp| - rebalancable_pfmp.update!(amount: PfmpManager.new(rebalancable_pfmp).calculate_amount) + rebalancable_pfmp.update!(amount: calculate_amount(rebalancable_pfmp)) end end diff --git a/spec/services/pfmp_manager_spec.rb b/spec/services/pfmp_manager_spec.rb index fc5d458e4..b16afd120 100644 --- a/spec/services/pfmp_manager_spec.rb +++ b/spec/services/pfmp_manager_spec.rb @@ -113,7 +113,7 @@ end describe "#calculate_amount" do # rubocop:disable RSpec/MultipleMemoizedHelpers - subject(:amount) { described_class.new(pfmp.reload).calculate_amount } + subject(:amount) { described_class.new(pfmp.reload).send(:calculate_amount, pfmp) } let(:establishment) { create(:establishment) } let(:pfmp) do @@ -202,7 +202,7 @@ end def other_pfmps_for_mef - manager.send(:other_pfmps_for_mef) + manager.send(:other_pfmps_for_mef, pfmp) end context "when there is no other pfmp for that school year and mef" do