diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index ec34b7cff..77478f2c5 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -99,8 +99,6 @@ class Backoffice::DossiersController < Backoffice::DossiersListController dossier.received! flash.notice = 'Dossier considéré comme reçu.' - NotificationMailer.send_notification(dossier, dossier.procedure.received_mail_template).deliver_now! - redirect_to backoffice_dossier_path(id: dossier.id) end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index c2b641644..8203618ed 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -78,6 +78,7 @@ class Dossier < ActiveRecord::Base after_save :build_default_champs, if: Proc.new { procedure_id_changed? } after_save :build_default_individual, if: Proc.new { procedure.for_individual? } + after_save :send_notification_email validates :user, presence: true @@ -302,4 +303,10 @@ class Dossier < ActiveRecord::Base def serialize_value_for_export(value) value.nil? || value.kind_of?(Time) ? value : value.to_s end + + def send_notification_email + if state_changed? && EN_INSTRUCTION.include?(state) + NotificationMailer.send_notification(self, procedure.received_mail_template).deliver_now! + end + end end diff --git a/app/workers/auto_archive_procedure_worker.rb b/app/workers/auto_archive_procedure_worker.rb index 71659b5a5..54875a961 100644 --- a/app/workers/auto_archive_procedure_worker.rb +++ b/app/workers/auto_archive_procedure_worker.rb @@ -2,13 +2,12 @@ class AutoArchiveProcedureWorker include Sidekiq::Worker def perform(*args) - procedures_to_archive = Procedure.not_archived.where("auto_archive_on <= ?", Date.today) + Procedure.not_archived.where("auto_archive_on <= ?", Date.today).each do |procedure| + procedure.dossiers.state_en_construction.each do |dossier| + dossier.received! + end - procedures_to_archive.each do |p| - p.dossiers.state_en_construction.update_all(state: :received) + procedure.update_attributes!(archived: true) end - - procedures_to_archive.update_all(archived: true, auto_archive_on: nil) - end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index b682281b7..ac668ee64 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -867,4 +867,27 @@ describe Dossier do it { is_expected.to include(dossier3)} it { is_expected.to include(dossier4)} end + + describe "#send_notification_email" do + let(:procedure) { create(:procedure) } + let(:dossier) { create(:dossier, procedure: procedure, state: :initiated) } + + before do + ActionMailer::Base.deliveries.clear + end + + it "sends an email when the dossier becomes received" do + dossier.received! + + mail = ActionMailer::Base.deliveries.last + + expect(mail.subject).to eq("Votre dossier TPS nº #{dossier.id} va être instruit") + end + + it "does not an email when the dossier becomes closed" do + dossier.closed! + + expect(ActionMailer::Base.deliveries.size).to eq(0) + end + end end diff --git a/spec/workers/auto_archive_procedure_worker_spec.rb b/spec/workers/auto_archive_procedure_worker_spec.rb index e67a19133..2a30fa764 100644 --- a/spec/workers/auto_archive_procedure_worker_spec.rb +++ b/spec/workers/auto_archive_procedure_worker_spec.rb @@ -1,7 +1,6 @@ require 'rails_helper' RSpec.describe AutoArchiveProcedureWorker, type: :worker do - let!(:procedure) { create(:procedure, archived: false, auto_archive_on: nil )} let!(:procedure_hier) { create(:procedure, archived: false, auto_archive_on: 1.day.ago )} let!(:procedure_aujourdhui) { create(:procedure, archived: false, auto_archive_on: Date.today )} @@ -17,11 +16,9 @@ RSpec.describe AutoArchiveProcedureWorker, type: :worker do end it { expect(procedure.archived).to eq false } - end context "when procedures have auto_archive_on set on yesterday or today" do - describe "titi" do before do subject @@ -31,12 +28,9 @@ RSpec.describe AutoArchiveProcedureWorker, type: :worker do it { expect(procedure_hier.archived).to eq true } it { expect(procedure_aujourdhui.archived).to eq true } - end - context "with dossiers" do - let!(:dossier1) { create(:dossier, procedure: procedure_hier, state: 'draft', archived: false)} let!(:dossier2) { create(:dossier, procedure: procedure_hier, state: 'initiated', archived: false)} let!(:dossier3) { create(:dossier, procedure: procedure_hier, state: 'replied', archived: false)} @@ -61,18 +55,14 @@ RSpec.describe AutoArchiveProcedureWorker, type: :worker do it { expect(dossier6.state).to eq 'closed' } it { expect(dossier7.state).to eq 'refused' } it { expect(dossier8.state).to eq 'without_continuation' } - end end context "when procedures have auto_archive_on set on future" do - before do subject end it { expect(procedure_demain.archived).to eq false } - end - end