feat(sva): reset start date when on a correction "incomplete"

This commit is contained in:
Colin Darie 2023-06-21 11:20:16 +02:00
parent 6ce8428ac6
commit 512f6ca0ec
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
3 changed files with 72 additions and 23 deletions

View file

@ -12,9 +12,18 @@ class SVASVRDecisionDateCalculatorService
end end
def decision_date def decision_date
base_date = determine_base_date duration = calculate_duration
duration = case unit start_date = determine_start_date
correction_delay = calculate_correction_delay(start_date)
start_date + correction_delay + duration
end
private
def calculate_duration
case unit
when :days when :days
period.days period.days
when :weeks when :weeks
@ -22,35 +31,36 @@ class SVASVRDecisionDateCalculatorService
when :months when :months
period.months period.months
end end
base_date + duration
end end
private def determine_start_date
return dossier.depose_at.to_date if dossier.corrections.empty?
return latest_correction_date if resume_method == :reset
return latest_incomplete_correction_date if dossier.corrections.any?(&:incomplete?)
def determine_base_date
return dossier.depose_at.to_date + total_correction_delay if resume_method == :continue
if dossier.corrections.any?
most_recent_correction_date
else
dossier.depose_at.to_date dossier.depose_at.to_date
end end
def latest_incomplete_correction_date
correction_date dossier.corrections.filter(&:incomplete?).max_by(&:resolved_at)
end end
def total_correction_delay def latest_correction_date
correction_date dossier.corrections.max_by(&:resolved_at)
end
def calculate_correction_delay(start_date)
dossier.corrections.sum do |correction| dossier.corrections.sum do |correction|
# If the correction is not resolved, we use the current date resolved_date = correction_date(correction)
# so interfaces could calculate how many remaining days next 0 unless resolved_date > start_date
resolved_date = correction.resolved_at&.to_date || Date.current
(resolved_date - correction.created_at.to_date).days (resolved_date - correction.created_at.to_date).days
end end
end end
def most_recent_correction_date def correction_date(correction)
return Date.current if dossier.pending_correction? # NOTE: when correction is not resolved, assume it could be done today
# so interfaces could show how many days are remaining after correction
dossier.corrections.max_by(&:resolved_at).resolved_at.to_date correction.resolved_at&.to_date || Date.current
end end
end end

View file

@ -2,6 +2,7 @@ FactoryBot.define do
factory :dossier_correction do factory :dossier_correction do
dossier dossier
commentaire commentaire
kind { :correction }
resolved_at { nil } resolved_at { nil }
trait :resolved do trait :resolved do

View file

@ -59,7 +59,7 @@ describe SVASVRDecisionDateCalculatorService do
end end
end end
context 'there is a pending correction' do context 'there is a pending correction kind = correct' do
before do before do
travel_to DateTime.new(2023, 5, 30, 18) do travel_to DateTime.new(2023, 5, 30, 18) do
dossier.flag_as_pending_correction!(build(:commentaire, dossier:)) dossier.flag_as_pending_correction!(build(:commentaire, dossier:))
@ -72,6 +72,44 @@ describe SVASVRDecisionDateCalculatorService do
expect(subject).to eq(Date.new(2023, 7, 26)) expect(subject).to eq(Date.new(2023, 7, 26))
end end
end end
context 'there is a pending correction kind = incomplete' do
before do
travel_to DateTime.new(2023, 5, 30, 18) do
dossier.flag_as_pending_correction!(build(:commentaire, dossier:), :incomplete)
end
travel_to DateTime.new(2023, 6, 5, 8) # 6 days elapsed
end
it 'calculates the date, like if resolution will be today' do
expect(subject).to eq(Date.new(2023, 8, 5))
end
end
context 'when correction was for an incomplete dossier' do
let!(:correction) do
created_at = DateTime.new(2023, 5, 20, 15)
resolved_at = DateTime.new(2023, 5, 25, 12)
create(:dossier_correction, :incomplete, dossier:, created_at:, resolved_at:)
end
it 'calculates the date by resetting delay' do
expect(subject).to eq(Date.new(2023, 7, 25))
end
context 'when there are multiple corrections' do
let!(:correction2) do
created_at = DateTime.new(2023, 5, 30, 18)
resolved_at = DateTime.new(2023, 6, 3, 8)
create(:dossier_correction, dossier:, created_at:, resolved_at:)
end
it 'calculates the date based on SVA rules with all correction delays' do
expect(subject).to eq(Date.new(2023, 7, 29))
end
end
end
end end
end end
@ -88,7 +126,7 @@ describe SVASVRDecisionDateCalculatorService do
before do before do
created_at = DateTime.new(2023, 5, 16, 15) created_at = DateTime.new(2023, 5, 16, 15)
resolved_at = DateTime.new(2023, 5, 17, 12) resolved_at = DateTime.new(2023, 5, 17, 12)
create(:dossier_correction, dossier:, created_at:, resolved_at:) create(:dossier_correction, :incomplete, dossier:, created_at:, resolved_at:)
created_at = DateTime.new(2023, 5, 20, 15) created_at = DateTime.new(2023, 5, 20, 15)
resolved_at = DateTime.new(2023, 5, 25, 12) resolved_at = DateTime.new(2023, 5, 25, 12)