diff --git a/app/models/asp/payment_request_state_machine.rb b/app/models/asp/payment_request_state_machine.rb index d54f83f44..060fef270 100644 --- a/app/models/asp/payment_request_state_machine.rb +++ b/app/models/asp/payment_request_state_machine.rb @@ -41,6 +41,22 @@ class PaymentRequestStateMachine request.schooling.student? end + guard_transition(to: :ready) do |request| + !request.student.rib.reused? + end + + guard_transition(to: :ready) do |request| + request.student.rib.valid? + end + + guard_transition(to: :ready) do |request| + request.pfmp.valid? + end + + guard_transition(to: :ready) do |request| + !request.student.lost + end + guard_transition(to: :ready) do |request| !request.student.adult_without_personal_rib? end @@ -53,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/app/models/student.rb b/app/models/student.rb index acd88223b..e59ed09c9 100644 --- a/app/models/student.rb +++ b/app/models/student.rb @@ -33,6 +33,7 @@ class Student < ApplicationRecord scope :asp_ready, lambda { where(biological_sex: [1, 2]) .where.not(address_postal_code: nil) + .where.not(lost: true) .where.not(address_country_code: %w[995 990] + [nil]) .where.not(birthplace_country_insee_code: %w[995 990] + [nil]) .where.not( diff --git a/config/initializers/version.rb b/config/initializers/version.rb index af9b4bc53..9911b865a 100644 --- a/config/initializers/version.rb +++ b/config/initializers/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Aplypro - VERSION = "1.12.1" + VERSION = "1.13" end diff --git a/spec/factories/ribs.rb b/spec/factories/ribs.rb index 66efca41b..8abf39b3f 100644 --- a/spec/factories/ribs.rb +++ b/spec/factories/ribs.rb @@ -8,5 +8,9 @@ archived_at { nil } name { Faker::Name.name } personal { Faker::Boolean.boolean } + + trait :outside_sepa do + iban { Faker::Bank.iban(country_code: "sa") } + 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 d20dbc527..2313d1f2a 100644 --- a/spec/models/asp/payment_request_state_machine_spec.rb +++ b/spec/models/asp/payment_request_state_machine_spec.rb @@ -9,6 +9,12 @@ it { is_expected.to be_in_state :pending } + shared_examples "a blocked request" do + it "cannot transition to ready" do + expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError + end + end + describe "mark_ready!" do let(:asp_payment_request) { create(:asp_payment_request, :sendable) } @@ -29,33 +35,51 @@ context "when the schooling status is unknown" do before { asp_payment_request.schooling.update!(status: nil) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the schooling is for an apprentice" do before { asp_payment_request.schooling.update!(status: :apprentice) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" + end + + context "when the student is a lost record" do + before { asp_payment_request.student.update!(lost: true) } + + it_behaves_like "a blocked request" + end + + context "when the RIB has been reused somewhere else" do + before { create(:rib, iban: asp_payment_request.student.rib.iban) } + + it_behaves_like "a blocked request" + end + + # rubocop:disable Rails/SkipsModelValidations + context "when the PFMP is not valid" do + before { asp_payment_request.pfmp.update_column(:start_date, Date.new(2002, 1, 1)) } + + it_behaves_like "a blocked request" end + context "when the rib is not valid" do + before { asp_payment_request.student.rib.update_columns(attributes_for(:rib, :outside_sepa)) } + + it_behaves_like "a blocked request" + end + # rubocop:enable Rails/SkipsModelValidations + context "when the request is missing information" do before { student.rib&.destroy } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the PFMP is zero-amount" do before { asp_payment_request.pfmp.update!(amount: 0) } - it "raises an error" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the request belongs to a student over 18 with an external rib" do @@ -64,18 +88,38 @@ student.rib.update!(personal: false) end - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the attributive decision has not been attached" do - before do - asp_payment_request.pfmp.schooling.attributive_decision.detach + 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 - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError + 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