2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-11-14 15:54:34 +01:00
|
|
|
class DossierWithReferenceDate
|
|
|
|
def self.assign(dossier, state: nil, reference_date: nil)
|
|
|
|
created_at = reference_date.presence || default_created_at(dossier)
|
|
|
|
|
|
|
|
case (state || dossier.state)
|
|
|
|
when Dossier.states.fetch(:en_construction)
|
|
|
|
dossier.created_at = created_at
|
|
|
|
dossier.en_construction_at ||= created_at + 1.minute
|
|
|
|
dossier.depose_at ||= dossier.en_construction_at
|
|
|
|
when Dossier.states.fetch(:en_instruction)
|
|
|
|
assign(dossier, state: Dossier.states.fetch(:en_construction), reference_date: created_at)
|
|
|
|
dossier.en_instruction_at ||= created_at + 2.minutes
|
|
|
|
when Dossier.states.fetch(:accepte), Dossier.states.fetch(:refuse), Dossier.states.fetch(:sans_suite)
|
|
|
|
assign(dossier, state: Dossier.states.fetch(:en_instruction), reference_date: created_at)
|
|
|
|
dossier.processed_at ||= created_at + 3.minutes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_created_at(dossier)
|
|
|
|
reference_date, delta = case dossier.state
|
|
|
|
when Dossier.states.fetch(:en_construction)
|
|
|
|
[dossier.depose_at || dossier.en_construction_at, 1.minute]
|
|
|
|
when Dossier.states.fetch(:en_instruction)
|
|
|
|
[dossier.en_instruction_at, 2.minutes]
|
|
|
|
when Dossier.states.fetch(:accepte), Dossier.states.fetch(:refuse), Dossier.states.fetch(:sans_suite)
|
|
|
|
[dossier.processed_at, 3.minutes]
|
|
|
|
end
|
|
|
|
if reference_date.present?
|
|
|
|
reference_date - delta
|
|
|
|
else
|
|
|
|
10.minutes.ago
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|