Skip to content

Commit

Permalink
Unbock automatically incomplete payment requests after DA edit (#1060)
Browse files Browse the repository at this point in the history
#1049

---------

Co-authored-by: pskl <[email protected]>
  • Loading branch information
ac-dylan and pskl authored Jul 29, 2024
1 parent 34e3f72 commit 28f0eee
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/controllers/schoolings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def set_classe

def retry_eligibile_payment_requests!
@schooling.pfmps.in_state(:validated).each do |pfmp|
p_r = pfmp.latest_payment_request
p_r.mark_ready! if p_r.eligible_for_auto_retry?
payment_request = pfmp.latest_payment_request
payment_request.mark_ready! if payment_request.eligible_for_auto_retry?
end
end
end
16 changes: 11 additions & 5 deletions app/models/asp/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class PaymentRequest < ApplicationRecord

TRANSITION_RELATION_NAME = :asp_payment_request_transitions

RETRYABLE_INCOMPLETE_VALIDATION_TYPES = %i[
needs_abrogated_attributive_decision
missing_attributive_decision
].freeze

include ::StateMachinable

has_many :asp_payment_request_transitions, class_name: "ASP::PaymentRequestTransition", dependent: :destroy,
Expand Down Expand Up @@ -108,11 +113,12 @@ def active?
end

def eligible_for_auto_retry?
# rubocop:disable Layout/LineLength
error_message = I18n.t("activerecord.errors.models.asp/payment_request.attributes.ready_state_validation.needs_abrogated_attributive_decision")
# rubocop:enable Layout/LineLength
in_state?(:incomplete) &&
last_transition.metadata["incomplete_reasons"]["ready_state_validation"].include?(error_message)
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
end
end
24 changes: 16 additions & 8 deletions spec/factories/asp/payment_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

FactoryBot.define do
factory :asp_payment_request, class: "ASP::PaymentRequest" do
# rubocop:disable Layout/LineLength
error_message = I18n.t("activerecord.errors.models.asp/payment_request.attributes.ready_state_validation.needs_abrogated_attributive_decision")
# rubocop:enable Layout/LineLength

pfmp do
association(:pfmp, :validated).tap { |p| p.payment_requests.destroy_all }
end
Expand Down Expand Up @@ -45,12 +41,24 @@
end
end

trait :incomplete_for_missing_abrogation_da do
trait :incomplete_for do
sendable

after(:create) do |req|
req.errors.add(:ready_state_validation, :needs_abrogated_attributive_decision)
req.mark_incomplete!(incomplete_reasons: { ready_state_validation: [error_message] })
transient { incomplete_reason { "XXXX" } }

after(:create) do |req, ctx|
req.errors.add(:ready_state_validation, ctx.incomplete_reason)
req.mark_incomplete!(
incomplete_reasons: {
ready_state_validation:
[
I18n.t(
"asp/payment_request.attributes.ready_state_validation.#{ctx.incomplete_reason}",
scope: "activerecord.errors.models"
)
]
}
)
end
end

Expand Down
22 changes: 17 additions & 5 deletions spec/models/asp/payment_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,36 @@
end

describe "eligible_for_auto_retry?" do
let(:p_r_incomplete_for_abrogation) { create(:asp_payment_request, :incomplete_for_missing_abrogation_da) }
let(:p_r_incomplete_for_abrogation) do
create(:asp_payment_request, :incomplete_for, incomplete_reason: :needs_abrogated_attributive_decision)
end
let(:p_r_incomplete_for_missing_da) do
create(:asp_payment_request, :incomplete_for, incomplete_reason: :missing_attributive_decision)
end
let(:schooling) { create(:schooling, :with_attributive_decision) }
let(:p_r_incomplete) { create(:asp_payment_request, :incomplete, schooling: schooling) }
let(:p_r_ready) { create(:asp_payment_request, :ready) }
let(:p_r_incomplete) { create(:asp_payment_request, :incomplete) }

context "when the payment request is in 'incomplete' state with specific error message" do
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
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
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
end
end

context "when the payment request is in 'incomplete' state without the specific error message" do
it "returns false" do
context "when the payment request is in 'incomplete' state without any specific error message" do
it "returns true" do
expect(p_r_incomplete.eligible_for_auto_retry?).to be false
end
end
Expand Down
16 changes: 11 additions & 5 deletions spec/requests/schoolings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
let(:user) { create(:user, :director, :with_selected_establishment, establishment: student.classe.establishment) }

let(:schooling) { create(:schooling, :with_attributive_decision) }
let(:payment_request) { create(:asp_payment_request, :incomplete_for_missing_abrogation_da) }

# rubocop:disable Layout/LineLength
error_message = I18n.t("activerecord.errors.models.asp/payment_request.attributes.ready_state_validation.needs_abrogated_attributive_decision")
# rubocop:enable Layout/LineLength
let(:payment_request) do
create(:asp_payment_request, :incomplete_for, incomplete_reason: :missing_attributive_decision)
end

before do
sign_in(user)
Expand All @@ -36,6 +35,13 @@
end

describe "retry_eligibile_payment_requests" do
let(:expected_error_message) do
I18n.t(
"asp/payment_request.attributes.ready_state_validation.needs_abrogated_attributive_decision",
scope: "activerecord.errors.models"
)
end

before do
schooling.update!(end_date: Date.current, start_date: Date.current - 1.day)
end
Expand All @@ -45,7 +51,7 @@
delete abrogate_decision_school_year_class_schooling_path(schooling.classe.school_year,
class_id: schooling.classe.id, id: schooling.id),
params: { confirmed_director: "1" }
expect(payment_request.last_transition.metadata).not_to include(error_message)
expect(payment_request.last_transition.metadata).not_to include(expected_error_message)
end
end
end
Expand Down

0 comments on commit 28f0eee

Please sign in to comment.