From 7c3dec29078cc3c68b68cb5739ab59cdb1011587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Wed, 3 Apr 2024 18:13:35 +0200 Subject: [PATCH 1/5] payment requests: block if the student is a lost record --- app/models/asp/payment_request_state_machine.rb | 4 ++++ app/models/student.rb | 1 + spec/models/asp/payment_request_state_machine_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/app/models/asp/payment_request_state_machine.rb b/app/models/asp/payment_request_state_machine.rb index d54f83f44..989559727 100644 --- a/app/models/asp/payment_request_state_machine.rb +++ b/app/models/asp/payment_request_state_machine.rb @@ -41,6 +41,10 @@ class PaymentRequestStateMachine request.schooling.student? end + guard_transition(to: :ready) do |request| + !request.student.lost + end + guard_transition(to: :ready) do |request| !request.student.adult_without_personal_rib? 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/spec/models/asp/payment_request_state_machine_spec.rb b/spec/models/asp/payment_request_state_machine_spec.rb index d20dbc527..07c35ff87 100644 --- a/spec/models/asp/payment_request_state_machine_spec.rb +++ b/spec/models/asp/payment_request_state_machine_spec.rb @@ -42,6 +42,14 @@ end end + context "when the student is a lost record" do + before { asp_payment_request.student.update!(lost: true) } + + it "blocks the transition" do + expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError + end + end + context "when the request is missing information" do before { student.rib&.destroy } From 600c9ca12fe161a716676fe2c3bc2663fff85e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Wed, 3 Apr 2024 18:15:48 +0200 Subject: [PATCH 2/5] payment requests: block if the student's rib is reused --- app/models/asp/payment_request_state_machine.rb | 4 ++++ spec/models/asp/payment_request_state_machine_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/asp/payment_request_state_machine.rb b/app/models/asp/payment_request_state_machine.rb index 989559727..4a810dab5 100644 --- a/app/models/asp/payment_request_state_machine.rb +++ b/app/models/asp/payment_request_state_machine.rb @@ -41,6 +41,10 @@ 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.lost end diff --git a/spec/models/asp/payment_request_state_machine_spec.rb b/spec/models/asp/payment_request_state_machine_spec.rb index 07c35ff87..606aa641a 100644 --- a/spec/models/asp/payment_request_state_machine_spec.rb +++ b/spec/models/asp/payment_request_state_machine_spec.rb @@ -50,6 +50,14 @@ end end + context "when the RIB has been reused somewhere else" do + before { create(:rib, iban: asp_payment_request.student.rib.iban) } + + it "blocks the transition" do + expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError + end + end + context "when the request is missing information" do before { student.rib&.destroy } From 45859ffd66c999b68bd076b2cd10b03cb87d3dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Thu, 4 Apr 2024 08:13:28 +0200 Subject: [PATCH 3/5] payment requests: block if PFMP or RIBs instances are not valid This means including all the model validation like correct dates and SEPA-ribs, etc. --- .../asp/payment_request_state_machine.rb | 8 +++ spec/factories/ribs.rb | 4 ++ .../asp/payment_request_state_machine_spec.rb | 52 ++++++++++--------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/app/models/asp/payment_request_state_machine.rb b/app/models/asp/payment_request_state_machine.rb index 4a810dab5..a0a83f1be 100644 --- a/app/models/asp/payment_request_state_machine.rb +++ b/app/models/asp/payment_request_state_machine.rb @@ -45,6 +45,14 @@ class PaymentRequestStateMachine !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 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 606aa641a..759c73e6e 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,49 +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 "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 RIB has been reused somewhere else" do before { create(:rib, iban: asp_payment_request.student.rib.iban) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + 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 @@ -80,9 +88,7 @@ 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 @@ -90,9 +96,7 @@ asp_payment_request.pfmp.schooling.attributive_decision.detach 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 end From 74c32c950e6248c385b6dbc04c2cef052bde396f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Thu, 4 Apr 2024 09:20:54 +0200 Subject: [PATCH 4/5] payment requests: block if there are duplicated validated PFMPs --- .../asp/payment_request_state_machine.rb | 6 ++++ app/models/pfmp.rb | 6 ++++ .../asp/payment_request_state_machine_spec.rb | 30 +++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) 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 From 268ddf9b4d0b2583d46fd9f1724398e4a98a37de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Wed, 3 Apr 2024 18:11:53 +0200 Subject: [PATCH 5/5] release: 1.13 --- config/initializers/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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