Skip to content

Commit

Permalink
Suppression de la fonction 'eligible_for_auto_retry?'
Browse files Browse the repository at this point in the history
  • Loading branch information
tnicolas1 committed Oct 17, 2024
1 parent e32b65f commit 30f7aaa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
3 changes: 1 addition & 2 deletions app/controllers/ribs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ def rib_is_readonly
redirect_to student_path(@student), alert: t("flash.ribs.readonly", name: @student.full_name)
end

# TODO: Factoriser avec 'retry_incomplete_payment_request!' de 'schoolings_controller' ?
def retry_rejected_or_unpaid_payment_request!
@student.pfmps.in_state(:validated).each do |pfmp|
if pfmp.latest_payment_request&.eligible_for_auto_retry?
if pfmp.latest_payment_request&.eligible_for_rejected_or_unpaid_auto_retry?
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/schoolings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def set_classe
def retry_incomplete_payment_request!
@schooling.pfmps.in_state(:validated).each do |pfmp|
payment_request = pfmp.latest_payment_request
payment_request.mark_ready! if payment_request&.eligible_for_auto_retry?
payment_request.mark_ready! if payment_request&.eligible_for_incomplete_retry?
end
end

Expand Down
16 changes: 4 additions & 12 deletions app/models/asp/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,18 @@ def active?
!terminated?
end

def eligible_for_auto_retry?
if in_state?(:incomplete)
eligible_for_incomplete_retry?
elsif in_state?(:rejected) || in_state?(:unpaid)
eligible_for_rejected_or_unpaid_auto_retry?
else
false
end
end

private

def eligible_for_incomplete_retry?
return false unless in_state?(:incomplete)

retryable_messages = RETRYABLE_INCOMPLETE_VALIDATION_TYPES.map do |r|
I18n.t("activerecord.errors.models.asp/payment_request.attributes.ready_state_validation.#{r}")
end
ActiveDecorator::Decorator.instance.decorate(self).incomplete_reason.intersect?(retryable_messages)
end

def eligible_for_rejected_or_unpaid_auto_retry?
return false unless in_state?(:rejected) || in_state?(:unpaid)

decorator = ActiveDecorator::Decorator.instance.decorate(self)
message = in_state?(:rejected) ? decorator.rejected_reason : decorator.unpaid_reason
%w[RIB BIC PAIEMENT].any? { |word| message.upcase.include?(word) }
Expand Down
38 changes: 19 additions & 19 deletions spec/models/asp/payment_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@
end
end

# rubocop:disable RSpec/MultipleMemoizedHelpers
describe "eligible_for_auto_retry?" do
describe "eligible_for_incomplete_retry?" do
let(:p_r_incomplete_for_abrogation) do
create(:asp_payment_request, :incomplete, incomplete_reason: :needs_abrogated_attributive_decision)
end
Expand All @@ -147,56 +146,57 @@
end
let(:p_r_ready) { create(:asp_payment_request, :ready) }

let(:p_r_rejected) { create(:asp_payment_request, :rejected) }
let(:p_r_rejected_rib) do
create(:asp_payment_request, :rejected, reason: "Test d'une raison de blocage d'un paiement bancaire")
end
let(:p_r_unpaid) { create(:asp_payment_request, :unpaid) }
let(:p_r_unpaid_rib) do
create(:asp_payment_request, :unpaid, reason: "Test d'une raison de blocage d'un paiement bancaire")
end

context "when the payment request is in 'incomplete' state with the abrogation specific error message" do
it "returns true" do
expect(p_r_incomplete_for_abrogation.eligible_for_auto_retry?).to be true
expect(p_r_incomplete_for_abrogation.eligible_for_incomplete_retry?).to be true
end
end

context "when the payment request is in 'incomplete' state with the missing DA specific error message" do
it "returns true" do
expect(p_r_incomplete_for_missing_da.eligible_for_auto_retry?).to be true
expect(p_r_incomplete_for_missing_da.eligible_for_incomplete_retry?).to be true
end
end

context "when the payment request is not in 'incomplete' state" do
it "returns false" do
expect(p_r_ready.eligible_for_auto_retry?).to be false
expect(p_r_ready.eligible_for_incomplete_retry?).to be false
end
end
end

describe "eligible_for_rejected_or_unpaid_auto_retry?" do
let(:p_r_rejected) { create(:asp_payment_request, :rejected) }
let(:p_r_rejected_rib) do
create(:asp_payment_request, :rejected, reason: "Test d'une raison de blocage d'un paiement bancaire")
end
let(:p_r_unpaid) { create(:asp_payment_request, :unpaid) }
let(:p_r_unpaid_rib) do
create(:asp_payment_request, :unpaid, reason: "Test d'une raison de blocage d'un paiement bancaire")
end

context "when the payment request is in 'rejected' state without a RIB reason" do
it "returns false" do
expect(p_r_rejected.eligible_for_auto_retry?).to be false
expect(p_r_rejected.eligible_for_rejected_or_unpaid_auto_retry?).to be false
end
end

context "when the payment request is in 'rejected' state with a RIB reason" do
it "returns true" do
expect(p_r_rejected_rib.eligible_for_auto_retry?).to be true
expect(p_r_rejected_rib.eligible_for_rejected_or_unpaid_auto_retry?).to be true
end
end

context "when the payment request is in 'unpaid' state without a RIB reason" do
it "returns false" do
expect(p_r_unpaid.eligible_for_auto_retry?).to be false
expect(p_r_unpaid.eligible_for_rejected_or_unpaid_auto_retry?).to be false
end
end

context "when the payment request is in 'unpaid' state with a RIB reason" do
it "returns true" do
expect(p_r_unpaid_rib.eligible_for_auto_retry?).to be true
expect(p_r_unpaid_rib.eligible_for_rejected_or_unpaid_auto_retry?).to be true
end
end
end
# rubocop:enable RSpec/MultipleMemoizedHelpers
end

0 comments on commit 30f7aaa

Please sign in to comment.