diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 510a34659..e45defcf7 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -537,11 +537,11 @@ class Dossier < ApplicationRecord end def can_accepter_automatiquement? - declarative_triggered_at.nil? && can_terminer? + declarative_triggered_at.nil? && procedure.declarative_accepte? && can_terminer? end def can_passer_automatiquement_en_instruction? - declarative_triggered_at.nil? + (declarative_triggered_at.nil? && procedure.declarative_en_instruction?) || procedure.auto_archive_on&.then { _1 <= Time.zone.today } end def can_repasser_en_instruction? diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 5b312fecc..56a4d36d9 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -56,6 +56,18 @@ FactoryBot.define do end end + trait :with_declarative_accepte do + after(:build) do |dossier, _evaluator| + dossier.procedure.declarative_with_state = 'accepte' + end + end + + trait :with_declarative_en_instruction do + after(:build) do |dossier, _evaluator| + dossier.procedure.declarative_with_state = 'en_instruction' + end + end + trait :archived do archived { true } end diff --git a/spec/jobs/cron/auto_archive_procedure_job_spec.rb b/spec/jobs/cron/auto_archive_procedure_job_spec.rb index 3ce167b74..a3c5abd75 100644 --- a/spec/jobs/cron/auto_archive_procedure_job_spec.rb +++ b/spec/jobs/cron/auto_archive_procedure_job_spec.rb @@ -17,15 +17,15 @@ RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do end context "when procedures have auto_archive_on set on yesterday or today" do - let!(:dossier1) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:brouillon), archived: false) } - let!(:dossier2) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:en_construction), archived: false) } - let!(:dossier3) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:en_construction), archived: false) } - let!(:dossier4) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:en_construction), archived: false) } - let!(:dossier5) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:en_instruction), archived: false) } - let!(:dossier6) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:accepte), archived: false) } - let!(:dossier7) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:refuse), archived: false) } - let!(:dossier8) { create(:dossier, procedure: procedure_hier, state: Dossier.states.fetch(:sans_suite), archived: false) } - let!(:dossier9) { create(:dossier, procedure: procedure_aujourdhui, state: Dossier.states.fetch(:en_construction), archived: false) } + let!(:dossier1) { create(:dossier, procedure: procedure_hier) } + let!(:dossier2) { create(:dossier, :en_construction, procedure: procedure_hier) } + let!(:dossier3) { create(:dossier, :en_construction, procedure: procedure_hier) } + let!(:dossier4) { create(:dossier, :en_construction, procedure: procedure_hier) } + let!(:dossier5) { create(:dossier, :en_instruction, procedure: procedure_hier) } + let!(:dossier6) { create(:dossier, :accepte, procedure: procedure_hier) } + let!(:dossier7) { create(:dossier, :refuse, procedure: procedure_hier) } + let!(:dossier8) { create(:dossier, :sans_suite, procedure: procedure_hier) } + let!(:dossier9) { create(:dossier, :en_construction, procedure: procedure_aujourdhui) } let(:last_operation) { dossier2.dossier_operation_logs.last } before do diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 59f6071b1..3d5f07201 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1052,7 +1052,7 @@ describe Dossier do end describe '#accepter_automatiquement!' do - let(:dossier) { create(:dossier, :en_construction, :with_individual) } + let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_declarative_accepte) } let(:last_operation) { dossier.dossier_operation_logs.last } let!(:now) { Time.zone.parse('01/01/2100') } let(:attestation) { Attestation.new } @@ -1097,7 +1097,7 @@ describe Dossier do end describe '#passer_automatiquement_en_instruction!' do - let(:dossier) { create(:dossier, :en_construction, en_construction_close_to_expiration_notice_sent_at: Time.zone.now) } + let(:dossier) { create(:dossier, :en_construction, :with_declarative_en_instruction, en_construction_close_to_expiration_notice_sent_at: Time.zone.now) } let(:last_operation) { dossier.dossier_operation_logs.last } let(:operation_serialized) { last_operation.data } let(:instructeur) { create(:instructeur) } @@ -1113,6 +1113,66 @@ describe Dossier do it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) } end + describe '#can_passer_automatiquement_en_instruction?' do + let(:dossier) { create(:dossier, :en_construction, declarative_triggered_at: declarative_triggered_at) } + let(:declarative_triggered_at) { nil } + + it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_falsey } + + context 'when dossier is declarative' do + before { dossier.procedure.update(declarative_with_state: :en_instruction) } + + context 'when dossier never transitioned' do + it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_truthy } + end + + context 'when dossier transitioned before' do + let(:declarative_triggered_at) { 1.day.ago } + + it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_falsey } + end + end + + context 'when procedure has auto archive set' do + before { dossier.procedure.update(auto_archive_on: 1.day.ago) } + + it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_truthy } + + context 'when auto_archive_on is in the future' do + before { dossier.procedure.update(auto_archive_on: 1.day.from_now) } + + it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_falsey } + end + + context 'when dossier transitioned before' do + let(:declarative_triggered_at) { 1.day.ago } + + it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_truthy } + end + end + end + + describe '#can_accepter_automatiquement?' do + let(:dossier) { create(:dossier, :en_instruction, declarative_triggered_at: declarative_triggered_at) } + let(:declarative_triggered_at) { nil } + + it { expect(dossier.can_accepter_automatiquement?).to be_falsey } + + context 'when dossier is declarative' do + before { dossier.procedure.update(declarative_with_state: :accepte) } + + context 'when dossier never transitioned' do + it { expect(dossier.can_accepter_automatiquement?).to be_truthy } + end + + context 'when dossier transitioned before' do + let(:declarative_triggered_at) { 1.day.ago } + + it { expect(dossier.can_accepter_automatiquement?).to be_falsey } + end + end + end + describe "can't transition to terminer when etablissement is in degraded mode" do let(:instructeur) { create(:instructeur) } let(:motivation) { 'motivation' } @@ -1138,8 +1198,8 @@ describe Dossier do end context "when dossier is en_construction" do - let(:dossier_incomplete) { create(:dossier, :en_construction, :with_entreprise, as_degraded_mode: true) } - let(:dossier_ok) { create(:dossier, :en_construction, :with_entreprise, as_degraded_mode: false) } + let(:dossier_incomplete) { create(:dossier, :en_construction, :with_entreprise, :with_declarative_accepte, as_degraded_mode: true) } + let(:dossier_ok) { create(:dossier, :en_construction, :with_entreprise, :with_declarative_accepte, as_degraded_mode: false) } it "can't accepter_automatiquement" do expect(dossier_incomplete.may_accepter_automatiquement?(instructeur:, motivation:)).to be_falsey @@ -1613,7 +1673,7 @@ describe Dossier do end context 'en_construction' do - let(:dossier) { create(:dossier, :en_construction, :followed, :with_individual) } + let(:dossier) { create(:dossier, :en_construction, :followed, :with_individual, :with_declarative_accepte) } it "clean up titres identite on accepter_automatiquement" do expect(champ_titre_identite.piece_justificative_file.attached?).to be_truthy