Skip to content

Commit

Permalink
Fix addresse_line2 + day_count validation for rectification with day_…
Browse files Browse the repository at this point in the history
…count = 0 (#1291)
  • Loading branch information
pskl authored Dec 18, 2024
1 parent 1fa3206 commit d4b9080
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 5 additions & 1 deletion app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Pfmp < ApplicationRecord # rubocop:disable Metrics/ClassLength
allow_nil: true,
greater_than: 0,
less_than_or_equal_to: ->(pfmp) { (pfmp.end_date - pfmp.start_date).to_i + 1 }
}
}, unless: :rectified? # NOTE: a rectification with a 0 day count can be created to cancel a payment

after_create -> { self.administrative_number = administrative_number }

Expand All @@ -81,6 +81,10 @@ def rectify!
transition_to!(:rectified)
end

def rectified?
in_state?(:rectified?)
end

def relative_index
schooling.pfmps.order(created_at: :asc).pluck(:id).find_index(id)
end
Expand Down
16 changes: 10 additions & 6 deletions app/services/pfmp_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ExistingActivePaymentRequestError < PfmpManagerError; end
class PfmpNotModifiableError < PfmpManagerError; end
class PaymentRequestNotIncompleteError < PfmpManagerError; end
class RectificationAmountThresholdNotReachedError < PfmpManagerError; end
class RectificationAmountZeroError < PfmpManagerError; end

EXCESS_AMOUNT_RECTIFICATION_THRESHOLD = 30

Expand Down Expand Up @@ -57,12 +58,7 @@ def rectify_and_update_attributes!(confirmed_pfmp_params, confirmed_address_para
paid_amount = pfmp.amount
update!(confirmed_pfmp_params)
correct_amount = pfmp.reload.amount
delta = paid_amount - correct_amount

if delta.positive? && delta <= EXCESS_AMOUNT_RECTIFICATION_THRESHOLD
raise RectificationAmountThresholdNotReachedError
end

check_rectification_delta(paid_amount - correct_amount)
pfmp.student.update!(confirmed_address_params)
pfmp.rectify!
end
Expand Down Expand Up @@ -127,4 +123,12 @@ def transition!
pfmp.transition_to!(:pending)
end
end

def check_rectification_delta(delta)
if delta.positive? && delta <= EXCESS_AMOUNT_RECTIFICATION_THRESHOLD
raise RectificationAmountThresholdNotReachedError
end

raise RectificationAmountZeroError if delta.zero?
end
end
4 changes: 2 additions & 2 deletions lib/asp/entities/adresse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def self.from_payment_request(payment_request) # rubocop:disable Metrics/AbcSize
# what should be sent for cpltdistribution & pointremise
if payment_request.pfmp.in_state?(:rectified)
return new(
pointremise: payment_request.student.address_line1.to_s.slice(0, 38), # Max 38 characters
cpltdistribution: payment_request.student.address_line2.slice(0, 38), # Max 38 characters
pointremise: payment_request.student.address_line1.slice(0, 38), # Max 38 characters
cpltdistribution: payment_request.student.address_line2&.slice(0, 38), # Max 38 characters
codetypeadr: ASP::Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE,
codeinseepays: InseeCountryCodeMapper.call(payment_request.student.address_country_code),
codepostalcedex: payment_request.student.address_postal_code,
Expand Down
11 changes: 10 additions & 1 deletion spec/services/pfmp_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,23 @@
}.to(confirmed_address_params[:address_line1]) # rubocop:disable Layout/LineLength
end

it "raises an error when the corrected amount is too small" do # rubocop:disable RSpec/ExampleLength
it "raises an error when the corrected amount is below threshold" do # rubocop:disable RSpec/ExampleLength
expect do
manager.rectify_and_update_attributes!(
{ day_count: pfmp.day_count - 2, start_date: pfmp.start_date, end_date: pfmp.end_date },
confirmed_address_params
)
end.to raise_error(PfmpManager::RectificationAmountThresholdNotReachedError)
end

it "raises an error when the corrected amount is zero" do # rubocop:disable RSpec/ExampleLength
expect do
manager.rectify_and_update_attributes!(
{ day_count: pfmp.day_count, start_date: pfmp.start_date, end_date: pfmp.end_date },
confirmed_address_params
)
end.to raise_error(PfmpManager::RectificationAmountZeroError)
end
end

describe "#calculate_amount" do # rubocop:disable RSpec/MultipleMemoizedHelpers
Expand Down

0 comments on commit d4b9080

Please sign in to comment.