Skip to content

Commit

Permalink
Ajout de la fonction 'retry_pfmps_payment_requests!' dans 'Student'
Browse files Browse the repository at this point in the history
  • Loading branch information
tnicolas1 committed Oct 25, 2024
1 parent b4f3006 commit 9a52f9c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 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!
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?(%w[RIB BIC PAIEMENT])
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
end
end
end
end
11 changes: 3 additions & 8 deletions app/jobs/sync/student_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def fetch_student_data(schooling)
.then { |data| map_student_attributes(data, api) }
.then { |attributes| schooling.student.update!(attributes) }

retry_rejected_or_unpaid_payment_request!(schooling.student)
retry_payment_request_for_addresses_informations!(schooling.student)
end

def map_student_attributes(data, api)
Expand All @@ -37,18 +37,13 @@ def map_student_attributes(data, api)
.except(:ine)
end

def retry_rejected_or_unpaid_payment_request!(student)
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.pfmps.in_state(:validated).each do |pfmp|
if pfmp.latest_payment_request&.eligible_for_rejected_or_unpaid_auto_retry?(%w[ADRESSE PAYS])
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
end
end
student.retry_pfmps_payment_requests!(%w[adresse pays postal résidence])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/asp/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def eligible_for_rejected_or_unpaid_auto_retry?(reasons)

decorator = ActiveDecorator::Decorator.instance.decorate(self)
message = in_state?(:rejected) ? decorator.rejected_reason : decorator.unpaid_reason
reasons.any? { |word| message.upcase.include?(word) }
reasons.any? { |word| message.lowercase.include?(word) }
end
end
end
9 changes: 9 additions & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ def adult_at?(date)
date >= birthdate + 18.years
end

def retry_pfmps_payment_requests!(reasons)
pfmps.in_state(:validated).each do |pfmp|
if pfmp.latest_payment_request&.eligible_for_rejected_or_unpaid_auto_retry?(reasons)
p_r = PfmpManager.new(pfmp).create_new_payment_request!
p_r.mark_ready!
end
end
end

private

def check_asp_file_reference
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) }

it "returns false" do
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?(%w[RIB BIC PAIEMENT])).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?(%w[RIB BIC PAIEMENT])).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) }

it "returns false" do
expect(p_r.eligible_for_rejected_or_unpaid_auto_retry?(%w[RIB BIC PAIEMENT])).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?(%w[RIB BIC PAIEMENT])).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 9a52f9c

Please sign in to comment.