diff --git a/app/models/asp/payment_request_state_machine.rb b/app/models/asp/payment_request_state_machine.rb index a0a83f1be..060fef270 100644 --- a/app/models/asp/payment_request_state_machine.rb +++ b/app/models/asp/payment_request_state_machine.rb @@ -69,6 +69,12 @@ class PaymentRequestStateMachine request.schooling.attributive_decision.attached? end + guard_transition(to: :ready) do |request| + request.pfmp.duplicates.none? do |pfmp| + pfmp.in_state?(:validated) + end + end + guard_transition(from: :ready, to: :sent) do |request| request.asp_request.present? end diff --git a/app/models/pfmp.rb b/app/models/pfmp.rb index 571133ce8..6c1d43252 100644 --- a/app/models/pfmp.rb +++ b/app/models/pfmp.rb @@ -112,4 +112,10 @@ def can_be_modified? def payment_due? day_count.present? end + + def duplicates + student.pfmps.excluding(self).select do |other| + other.start_date == start_date && other.end_date == end_date + end + end end diff --git a/spec/models/asp/payment_request_state_machine_spec.rb b/spec/models/asp/payment_request_state_machine_spec.rb index 759c73e6e..2313d1f2a 100644 --- a/spec/models/asp/payment_request_state_machine_spec.rb +++ b/spec/models/asp/payment_request_state_machine_spec.rb @@ -92,12 +92,36 @@ end context "when the attributive decision has not been attached" do - before do - asp_payment_request.pfmp.schooling.attributive_decision.detach - end + before { asp_payment_request.pfmp.schooling.attributive_decision.detach } it_behaves_like "a blocked request" end + + context "when there is another duplicated PFMP" do + let(:duplicate) do + pfmp = asp_payment_request.pfmp + + create( + :pfmp, + schooling: pfmp.schooling, + start_date: pfmp.start_date, + end_date: pfmp.end_date, + day_count: pfmp.day_count + ) + end + + context "when it is validated" do + before { duplicate.validate! } + + it_behaves_like "a blocked request" + end + + context "when it's not validated" do + it "allows the transition" do + expect { asp_payment_request.mark_ready! }.not_to raise_error + end + end + end end describe "mark_as_sent!" do