diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 4d9c75b04..531a30db1 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -149,7 +149,7 @@ module Users errors = update_dossier_and_compute_errors if passage_en_construction? && errors.blank? - @dossier.en_construction! + @dossier.passer_en_construction! NotificationMailer.send_initiated_notification(@dossier).deliver_later @dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur| DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later diff --git a/app/models/dossier.rb b/app/models/dossier.rb index b1898d2e2..5cd6cc178 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -282,7 +282,6 @@ class Dossier < ApplicationRecord delegate :types_de_champ, to: :procedure delegate :france_connect_information, to: :user - before_validation :update_state_dates, if: -> { state_changed? } before_save :build_default_champs, if: Proc.new { groupe_instructeur_id_was.nil? } before_save :update_search_terms @@ -508,20 +507,23 @@ class Dossier < ApplicationRecord end end + def after_passer_en_construction + update!(en_construction_at: Time.zone.now) if self.en_construction_at.nil? + end + def after_passer_en_instruction(instructeur) instructeur.follow(self) + update!(en_instruction_at: Time.zone.now) if self.en_instruction_at.nil? log_dossier_operation(instructeur, :passer_en_instruction) end def after_passer_automatiquement_en_instruction + update!(en_instruction_at: Time.zone.now) if self.en_instruction_at.nil? log_automatic_dossier_operation(:passer_en_instruction) end def after_repasser_en_construction(instructeur) - self.en_instruction_at = nil - - save! log_dossier_operation(instructeur, :repasser_en_construction) end @@ -529,6 +531,7 @@ class Dossier < ApplicationRecord self.archived = false self.processed_at = nil self.motivation = nil + self.en_instruction_at = Time.zone.now attestation&.destroy save! @@ -543,6 +546,8 @@ class Dossier < ApplicationRecord self.justificatif_motivation.attach(justificatif) end + self.processed_at = Time.zone.now + if attestation.nil? self.attestation = build_attestation end @@ -559,6 +564,7 @@ class Dossier < ApplicationRecord self.attestation = build_attestation end + self.processed_at = Time.zone.now save! NotificationMailer.send_closed_notification(self).deliver_later log_automatic_dossier_operation(:accepter, self) @@ -571,6 +577,7 @@ class Dossier < ApplicationRecord self.justificatif_motivation.attach(justificatif) end + self.processed_at = Time.zone.now save! NotificationMailer.send_refused_notification(self).deliver_later log_dossier_operation(instructeur, :refuser, self) @@ -583,6 +590,7 @@ class Dossier < ApplicationRecord self.justificatif_motivation.attach(justificatif) end + self.processed_at = Time.zone.now save! NotificationMailer.send_without_continuation_notification(self).deliver_later log_dossier_operation(instructeur, :classer_sans_suite, self) @@ -766,16 +774,6 @@ class Dossier < ApplicationRecord end end - def update_state_dates - if en_construction? && !self.en_construction_at - self.en_construction_at = Time.zone.now - elsif en_instruction? && !self.en_instruction_at - self.en_instruction_at = Time.zone.now - elsif TERMINE.include?(state) && !self.processed_at - self.processed_at = Time.zone.now - end - end - def send_dossier_received if saved_change_to_state? && en_instruction? && !procedure.declarative_accepte? NotificationMailer.send_dossier_received(self).deliver_later diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index 88a228c27..ec2d68386 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -194,7 +194,7 @@ describe Instructeurs::DossiersController, type: :controller do describe '#terminer' do context "with refuser" do before do - dossier.en_instruction! + dossier.passer_en_instruction!(instructeur) sign_in(instructeur.user) end @@ -235,7 +235,7 @@ describe Instructeurs::DossiersController, type: :controller do context "with classer_sans_suite" do before do - dossier.en_instruction! + dossier.passer_en_instruction!(instructeur) sign_in(instructeur.user) end context 'without attachment' do @@ -277,7 +277,7 @@ describe Instructeurs::DossiersController, type: :controller do context "with accepter" do before do - dossier.en_instruction! + dossier.passer_en_instruction!(instructeur) sign_in(instructeur.user) expect(NotificationMailer).to receive(:send_closed_notification) diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index b98d4a090..9dd0cdfd5 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -657,7 +657,7 @@ describe Users::DossiersController, type: :controller do let!(:invite) { create(:invite, dossier: dossier, user: user) } before do - dossier.en_construction! + dossier.passer_en_construction! subject end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 4af2276c8..6cbf0503a 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -346,13 +346,14 @@ describe Dossier do let(:state) { Dossier.states.fetch(:brouillon) } let(:dossier) { create(:dossier, state: state) } let(:beginning_of_day) { Time.zone.now.beginning_of_day } + let(:instructeur) { create(:instructeur) } before { Timecop.freeze(beginning_of_day) } after { Timecop.return } context 'when dossier is en_construction' do before do - dossier.en_construction! + dossier.passer_en_construction! dossier.reload end @@ -361,8 +362,8 @@ describe Dossier do it 'should keep first en_construction_at date' do Timecop.return - dossier.en_instruction! - dossier.en_construction! + dossier.passer_en_instruction!(instructeur) + dossier.repasser_en_construction!(instructeur) expect(dossier.en_construction_at).to eq(beginning_of_day) end @@ -370,9 +371,10 @@ describe Dossier do context 'when dossier is en_instruction' do let(:state) { Dossier.states.fetch(:en_construction) } + let(:instructeur) { create(:instructeur) } before do - dossier.en_instruction! + dossier.passer_en_instruction!(instructeur) dossier.reload end @@ -381,39 +383,48 @@ describe Dossier do it 'should keep first en_instruction_at date if dossier is set to en_construction again' do Timecop.return - dossier.en_construction! - dossier.en_instruction! + dossier.repasser_en_construction!(instructeur) + dossier.passer_en_instruction!(instructeur) expect(dossier.en_instruction_at).to eq(beginning_of_day) end end - shared_examples 'dossier is processed' do |new_state| - before do - dossier.update(state: new_state) - dossier.reload - end - - it { expect(dossier.state).to eq(new_state) } - it { expect(dossier.processed_at).to eq(beginning_of_day) } - end - context 'when dossier is accepte' do let(:state) { Dossier.states.fetch(:en_instruction) } - it_behaves_like 'dossier is processed', Dossier.states.fetch(:accepte) + before do + dossier.accepter!(instructeur, nil, nil) + dossier.reload + end + + it { expect(dossier.state).to eq(Dossier.states.fetch(:accepte)) } + it { expect(dossier.processed_at).to eq(beginning_of_day) } + end context 'when dossier is refuse' do let(:state) { Dossier.states.fetch(:en_instruction) } - it_behaves_like 'dossier is processed', Dossier.states.fetch(:refuse) + before do + dossier.refuser!(instructeur, nil, nil) + dossier.reload + end + + it { expect(dossier.state).to eq(Dossier.states.fetch(:refuse)) } + it { expect(dossier.processed_at).to eq(beginning_of_day) } end context 'when dossier is sans_suite' do let(:state) { Dossier.states.fetch(:en_instruction) } - it_behaves_like 'dossier is processed', Dossier.states.fetch(:sans_suite) + before do + dossier.classer_sans_suite!(instructeur, nil, nil) + dossier.reload + end + + it { expect(dossier.state).to eq(Dossier.states.fetch(:sans_suite)) } + it { expect(dossier.processed_at).to eq(beginning_of_day) } end end @@ -478,13 +489,14 @@ describe Dossier do describe "#send_dossier_received" do let(:procedure) { create(:procedure) } let(:dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_construction)) } + let(:instructeur) { create(:instructeur) } before do allow(NotificationMailer).to receive(:send_dossier_received).and_return(double(deliver_later: nil)) end it "sends an email when the dossier becomes en_instruction" do - dossier.en_instruction! + dossier.passer_en_instruction!(instructeur) expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier) end @@ -777,6 +789,7 @@ describe Dossier do describe 'webhook' do let(:dossier) { create(:dossier) } + let(:instructeur) { create(:instructeur) } it 'should not call webhook' do expect { @@ -792,7 +805,7 @@ describe Dossier do }.to_not have_enqueued_job(WebHookJob) expect { - dossier.en_construction! + dossier.passer_en_construction! }.to have_enqueued_job(WebHookJob) expect { @@ -800,7 +813,7 @@ describe Dossier do }.to_not have_enqueued_job(WebHookJob) expect { - dossier.en_instruction! + dossier.passer_en_instruction!(instructeur) }.to have_enqueued_job(WebHookJob) end end