Skip to content

Commit

Permalink
Séparation en 2 fonctions de 'eligible_for_auto_retry?'
Browse files Browse the repository at this point in the history
  • Loading branch information
tnicolas1 committed Oct 9, 2024
1 parent 840c8b8 commit b3b3753
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
6 changes: 3 additions & 3 deletions app/controllers/ribs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ def rib_is_readonly
# TODO: Factoriser avec 'retry_eligible_payment_requests!' de 'schoolings_controller'
def retry_eligible_payment_requests!
@student.pfmps.in_state(:validated).each do |pfmp|
if pfmp.latest_payment_request&.eligible_for_auto_retry?
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
if pfmp.latest_payment_request&.eligible_for_rejected_and_unpaid_auto_retry?
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
end
end
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_eligible_payment_requests!
@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
26 changes: 16 additions & 10 deletions app/models/asp/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,25 @@ def active?
!terminated?
end

def eligible_for_auto_retry?
if 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
retryable_messages.intersect?(last_transition.metadata["incomplete_reasons"]["ready_state_validation"])
elsif in_state?(:rejected)
%w[RIB BIC PAIEMENT].any? { |word| last_transition.metadata["Motif rejet"].upcase.include?(word) }
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
last_transition.metadata["incomplete_reasons"]["ready_state_validation"].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)
%w[RIB BIC PAIEMENT].any? { |word| last_transition.metadata["PAIEMENT"]["LIBELLEMOTIFINVAL"].upcase.include?(word) }
message = last_transition.metadata["PAIEMENT"]["LIBELLEMOTIFINVAL"]
else
false
return false
end

%w[RIB BIC PAIEMENT].any? { |word| message.upcase.include?(word) }
end
end
end
12 changes: 8 additions & 4 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_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 @@ -148,20 +148,24 @@

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

describe "eligible_for_rejected_and_unpaid_auto_retry?" do
# TODO
end
end
end

0 comments on commit b3b3753

Please sign in to comment.