Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/1.13 #670

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/models/asp/payment_request_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Aplypro
VERSION = "1.12.1"
VERSION = "1.13"
end
4 changes: 4 additions & 0 deletions spec/factories/ribs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
82 changes: 63 additions & 19 deletions spec/models/asp/payment_request_state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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
Expand All @@ -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
Expand Down