diff --git a/app/controllers/ribs_controller.rb b/app/controllers/ribs_controller.rb index 77fc1544e..3c5593311 100644 --- a/app/controllers/ribs_controller.rb +++ b/app/controllers/ribs_controller.rb @@ -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") @@ -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 diff --git a/app/jobs/sync/student_job.rb b/app/jobs/sync/student_job.rb index 675910a3d..608f9202e 100644 --- a/app/jobs/sync/student_job.rb +++ b/app/jobs/sync/student_job.rb @@ -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) @@ -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 diff --git a/app/models/asp/payment_request.rb b/app/models/asp/payment_request.rb index e6452207f..f98cea9ce 100644 --- a/app/models/asp/payment_request.rb +++ b/app/models/asp/payment_request.rb @@ -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.downcase.include?(word) } end end end diff --git a/app/models/student.rb b/app/models/student.rb index 8e3f22aab..638365f3e 100644 --- a/app/models/student.rb +++ b/app/models/student.rb @@ -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 diff --git a/spec/models/asp/payment_request_spec.rb b/spec/models/asp/payment_request_spec.rb index a2d7e04b2..ce65b3b3f 100644 --- a/spec/models/asp/payment_request_spec.rb +++ b/spec/models/asp/payment_request_spec.rb @@ -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 @@ -180,7 +182,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 @@ -188,7 +190,7 @@ 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 @@ -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