feat(sva): calculcate decision date with corrections delays & resume methods
This commit is contained in:
parent
5db80ee6de
commit
4bdd4310ab
5 changed files with 175 additions and 58 deletions
|
@ -1070,7 +1070,7 @@ class Dossier < ApplicationRecord
|
|||
return unless procedure.sva_svr_enabled?
|
||||
return if sva_svr_decision_triggered_at.present?
|
||||
|
||||
self.sva_svr_decision_on = SVASVRDateCalculatorService.new(self, procedure).calculate
|
||||
self.sva_svr_decision_on = SVASVRDecisionDateCalculatorService.new(self, procedure).decision_date
|
||||
|
||||
if en_construction? && may_passer_automatiquement_en_instruction?
|
||||
passer_automatiquement_en_instruction!
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
class SVASVRDateCalculatorService
|
||||
attr_reader :dossier, :procedure
|
||||
|
||||
def initialize(dossier, procedure)
|
||||
@dossier = dossier
|
||||
@procedure = procedure
|
||||
end
|
||||
|
||||
def calculate
|
||||
config = procedure.sva_svr_configuration
|
||||
unit = config.unit.to_sym
|
||||
period = config.period.to_i
|
||||
|
||||
case unit
|
||||
when :days
|
||||
dossier.depose_at.to_date + period.days
|
||||
when :weeks
|
||||
dossier.depose_at.to_date + period.weeks
|
||||
when :months
|
||||
dossier.depose_at.to_date + period.months
|
||||
end
|
||||
end
|
||||
end
|
56
app/services/sva_svr_decision_date_calculator_service.rb
Normal file
56
app/services/sva_svr_decision_date_calculator_service.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
class SVASVRDecisionDateCalculatorService
|
||||
attr_reader :dossier, :procedure, :unit, :period, :resume_method
|
||||
|
||||
def initialize(dossier, procedure)
|
||||
@dossier = dossier
|
||||
@procedure = procedure
|
||||
|
||||
config = procedure.sva_svr_configuration
|
||||
@unit = config.unit.to_sym
|
||||
@period = config.period.to_i
|
||||
@resume_method = config.resume.to_sym
|
||||
end
|
||||
|
||||
def decision_date
|
||||
base_date = determine_base_date
|
||||
|
||||
duration = case unit
|
||||
when :days
|
||||
period.days
|
||||
when :weeks
|
||||
period.weeks
|
||||
when :months
|
||||
period.months
|
||||
end
|
||||
|
||||
base_date + duration
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
def total_correction_delay
|
||||
dossier.corrections.sum do |correction|
|
||||
# If the correction is not resolved, we use the current date
|
||||
# so interfaces could calculate how many remaining days
|
||||
resolved_date = correction.resolved_at&.to_date || Date.current
|
||||
|
||||
(resolved_date - correction.created_at.to_date).days
|
||||
end
|
||||
end
|
||||
|
||||
def most_recent_correction_date
|
||||
return Date.current if dossier.pending_correction?
|
||||
|
||||
dossier.corrections.max_by(&:resolved_at).resolved_at.to_date
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue