Skip to content

Commit

Permalink
Merge branch 'main' into chore/refactor-amount-update
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Nov 4, 2024
2 parents 60799fe + db3f851 commit 19cfc6a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 17 deletions.
11 changes: 1 addition & 10 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_rejected_or_unpaid_payment_request!
@student.retry_pfmps_payment_requests!(%w[rib bic paiement])

redirect_to student_path(@student),
notice: t(".success")
Expand Down Expand Up @@ -149,13 +149,4 @@ def check_establishment!
def rib_is_readonly
redirect_to student_path(@student), alert: t("flash.ribs.readonly", name: @student.full_name)
end

def retry_rejected_or_unpaid_payment_request!
@student.pfmps.in_state(:validated).each do |pfmp|
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
end
end
end
12 changes: 12 additions & 0 deletions app/jobs/sync/student_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def fetch_student_data(schooling)
api.fetch_resource(:student, ine: schooling.student.ine)
.then { |data| map_student_attributes(data, api) }
.then { |attributes| schooling.student.update!(attributes) }

retry_payment_request_for_addresses_informations!(schooling.student)
end

def map_student_attributes(data, api)
Expand All @@ -34,5 +36,15 @@ def map_student_attributes(data, api)
.slice(*Student.updatable_attributes)
.except(:ine)
end

def retry_payment_request_for_addresses_informations!(student)
if student.previous_changes.key?("address_line1") ||
student.previous_changes.key?("address_line2") ||
student.previous_changes.key?("address_city_insee_code") ||
student.previous_changes.key?("address_country_code")

student.retry_pfmps_payment_requests!(%w[adresse pays postal résidence])
end
end
end
end
4 changes: 2 additions & 2 deletions app/models/asp/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ def eligible_for_incomplete_retry?
last_transition.metadata["incomplete_reasons"]["ready_state_validation"].intersect?(retryable_messages)
end

def eligible_for_rejected_or_unpaid_auto_retry?
def eligible_for_rejected_or_unpaid_auto_retry?(reasons)
return false unless in_state?(:rejected, :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) }
reasons.any? { |word| message.downcase.include?(word) }
end

def reconstructed_iban
Expand Down
4 changes: 4 additions & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def adult_at?(date)
date >= birthdate + 18.years
end

def retry_pfmps_payment_requests!(reasons)
pfmps.in_state(:validated).each { |pfmp| PfmpManager.new(pfmp).retry_payment_request!(reasons) }
end

private

def check_asp_file_reference
Expand Down
7 changes: 7 additions & 0 deletions app/services/pfmp_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def previously_locked_amount
.sum
end

def retry_payment_request!(reasons)
return unless @pfmp.latest_payment_request&.eligible_for_rejected_or_unpaid_auto_retry?(reasons)

p_r = create_new_payment_request!
p_r.mark_ready!
end

private

def other_pfmps_for_mef
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.20.3"
VERSION = "1.20.4"
end
19 changes: 19 additions & 0 deletions spec/jobs/sync/student_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@
end
end

context "when the addresses informations change and have a payment request rejected" do
let(:payment_request) { create(:asp_payment_request, :rejected, reason: "L'adresse ne correspond pas.") }
let(:pfmp) { payment_request.pfmp }

before { described_class.perform_now(student.current_schooling) }

it "does not create a new payment request" do
expect(pfmp.latest_payment_request).to eq(payment_request)
end

it "creates a new payment request" do
student.update!(address_line1: Faker::Address.street_name)
student.current_schooling.pfmps << pfmp
described_class.perform_now(student.current_schooling)

expect(pfmp.latest_payment_request).not_to eq(payment_request)
end
end

context "when the API responds with a 404" do
before do
WebMock
Expand Down
10 changes: 6 additions & 4 deletions spec/models/asp/payment_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@
end

describe "eligible_for_rejected_or_unpaid_auto_retry?" do
let(:reasons) { %w[rib bic paiement] }

context "when the payment request is in 'rejected' state without a RIB reason" do
let(:p_r) { create(:asp_payment_request, :rejected, reason: "Blabla") }

it "returns false" do
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?).to be false
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?(reasons)).to be false
end
end

Expand All @@ -180,15 +182,15 @@
end

it "returns true" do
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?).to be true
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?(reasons)).to be true
end
end

context "when the payment request is in 'unpaid' state without a RIB reason" do
let(:p_r) { create(:asp_payment_request, :unpaid, reason: "Blabla") }

it "returns false" do
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?).to be false
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?(reasons)).to be false
end
end

Expand All @@ -198,7 +200,7 @@
end

it "returns true" do
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?).to be true
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?(reasons)).to be true
end
end
end
Expand Down

0 comments on commit 19cfc6a

Please sign in to comment.