Skip to content

Commit

Permalink
Réinsertion de 'eligible_for_auto_retry?'
Browse files Browse the repository at this point in the history
  • Loading branch information
tnicolas1 committed Oct 10, 2024
1 parent b3b3753 commit c563108
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
8 changes: 4 additions & 4 deletions app/controllers/ribs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def update
@rib = @student.create_new_rib(rib_params)

if @rib.save
retry_eligible_payment_requests!
retry_rejected_or_unpaid_payment_request!

redirect_to student_path(@student),
notice: t(".success")
Expand Down Expand Up @@ -150,10 +150,10 @@ def rib_is_readonly
redirect_to student_path(@student), alert: t("flash.ribs.readonly", name: @student.full_name)
end

# TODO: Factoriser avec 'retry_eligible_payment_requests!' de 'schoolings_controller'
def retry_eligible_payment_requests!
# 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_rejected_and_unpaid_auto_retry?
if pfmp.latest_payment_request&.eligible_for_auto_retry?
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/schoolings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SchoolingsController < ApplicationController
def abrogate_decision
GenerateAbrogationDecisionJob.perform_now(@schooling)

retry_eligible_payment_requests!
retry_incomplete_payment_request!

redirect_to student_path(@schooling.student),
notice: t("flash.da.abrogated", name: @schooling.student.full_name)
Expand Down Expand Up @@ -69,10 +69,10 @@ def set_classe
alert: t("errors.classes.not_found") and return
end

def retry_eligible_payment_requests!
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_incomplete_retry?
payment_request.mark_ready! if payment_request&.eligible_for_auto_retry?
end
end

Expand Down
32 changes: 18 additions & 14 deletions app/models/asp/payment_request.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module ASP
class PaymentRequest < ApplicationRecord
class PaymentRequest < ApplicationRecord # rubocop:disable Metrics/ClassLength
TRANSITION_CLASS = ASP::PaymentRequestTransition
STATE_MACHINE_CLASS = ASP::PaymentRequestStateMachine

Expand Down Expand Up @@ -117,25 +117,29 @@ def active?
!terminated?
end

def eligible_for_incomplete_retry?
return false unless in_state?(:incomplete)
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?
retryable_messages = RETRYABLE_INCOMPLETE_VALIDATION_TYPES.map do |r|
I18n.t("activerecord.errors.models.asp/payment_request.attributes.ready_state_validation.#{r}")
end
last_transition.metadata["incomplete_reasons"]["ready_state_validation"].intersect?(retryable_messages)
ActiveDecorator::Decorator.instance.decorate(self).incomplete_reason.intersect?(retryable_messages)
end

def eligible_for_rejected_and_unpaid_auto_retry?
if in_state?(:rejected)
message = last_transition.metadata["Motif rejet"]
elsif in_state?(:unpaid)
message = last_transition.metadata["PAIEMENT"]["LIBELLEMOTIFINVAL"]
else
return false
end

def eligible_for_rejected_or_unpaid_auto_retry?
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) }
end
end
end # rubocop:enable Metrics/ClassLength
end
12 changes: 5 additions & 7 deletions spec/models/asp/payment_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
end
end

describe "eligible_for_incomplete_retry?" do
describe "eligible_for_auto_retry?" do
let(:p_r_incomplete_for_abrogation) do
create(:asp_payment_request, :incomplete, incomplete_reason: :needs_abrogated_attributive_decision)
end
Expand All @@ -148,24 +148,22 @@

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_incomplete_retry?).to be true
expect(p_r_incomplete_for_abrogation.eligible_for_auto_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_incomplete_retry?).to be true
expect(p_r_incomplete_for_missing_da.eligible_for_auto_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_incomplete_retry?).to be false
expect(p_r_ready.eligible_for_auto_retry?).to be false
end
end

describe "eligible_for_rejected_and_unpaid_auto_retry?" do
# TODO
end
# TODO: Ajouter un contexte pour le cas 'rejected' et 'unpaid'
end
end

0 comments on commit c563108

Please sign in to comment.