From f8673894e2801d9a4e3bbabce748e8ea6ac9753e Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 26 May 2017 19:37:55 +0200 Subject: [PATCH 1/6] Stop erasing data when auto-archiving procedures --- app/workers/auto_archive_procedure_worker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/workers/auto_archive_procedure_worker.rb b/app/workers/auto_archive_procedure_worker.rb index 71659b5a5..dea73cfb3 100644 --- a/app/workers/auto_archive_procedure_worker.rb +++ b/app/workers/auto_archive_procedure_worker.rb @@ -8,7 +8,7 @@ class AutoArchiveProcedureWorker p.dossiers.state_en_construction.update_all(state: :received) end - procedures_to_archive.update_all(archived: true, auto_archive_on: nil) + procedures_to_archive.update_all(archived: true) end end From 68e734fbb51fc426e0181fb3d3c9fd510f593e5d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 26 May 2017 19:31:17 +0200 Subject: [PATCH 2/6] Remove a blank line --- app/workers/auto_archive_procedure_worker.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/workers/auto_archive_procedure_worker.rb b/app/workers/auto_archive_procedure_worker.rb index dea73cfb3..869c8b5c6 100644 --- a/app/workers/auto_archive_procedure_worker.rb +++ b/app/workers/auto_archive_procedure_worker.rb @@ -9,6 +9,5 @@ class AutoArchiveProcedureWorker end procedures_to_archive.update_all(archived: true) - end end From cc9521667282c270442d876f07eb2bbec11a2795 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 26 May 2017 19:35:02 +0200 Subject: [PATCH 3/6] Refactor AutoArchiveProcedureWorker#perform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the loop failed, we wouldn’t update the archived attribute for procedures for which the folders were archived. That would have caused to re-run the task for procedures for which the task had already been run. --- app/workers/auto_archive_procedure_worker.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/workers/auto_archive_procedure_worker.rb b/app/workers/auto_archive_procedure_worker.rb index 869c8b5c6..13d14ad9f 100644 --- a/app/workers/auto_archive_procedure_worker.rb +++ b/app/workers/auto_archive_procedure_worker.rb @@ -2,12 +2,10 @@ 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.update_all(state: :received) - 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) end end From 67d0fb5ddc069668887ff5a2fa2d8e844a1a58d2 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 26 May 2017 19:42:05 +0200 Subject: [PATCH 4/6] Further refactor of AutoArchiveProcedureWorker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Call received! so that validation and save callbacks are called. Previously, we wouldn’t have update the received_at date --- app/workers/auto_archive_procedure_worker.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/workers/auto_archive_procedure_worker.rb b/app/workers/auto_archive_procedure_worker.rb index 13d14ad9f..54875a961 100644 --- a/app/workers/auto_archive_procedure_worker.rb +++ b/app/workers/auto_archive_procedure_worker.rb @@ -3,7 +3,9 @@ class AutoArchiveProcedureWorker def perform(*args) Procedure.not_archived.where("auto_archive_on <= ?", Date.today).each do |procedure| - procedure.dossiers.state_en_construction.update_all(state: :received) + procedure.dossiers.state_en_construction.each do |dossier| + dossier.received! + end procedure.update_attributes!(archived: true) end From 107c3220627aee743b9b91a7356ce5e6df20e57a Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 26 May 2017 20:01:57 +0200 Subject: [PATCH 5/6] [Fix #323] Refactor received dossier notification: use a callback --- .../backoffice/dossiers_controller.rb | 2 -- app/models/dossier.rb | 7 ++++++ spec/models/dossier_spec.rb | 23 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) 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/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 From 9eb897f7211f084000128819235f543754cc731f Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 26 May 2017 23:31:45 +0200 Subject: [PATCH 6/6] Fix spacing in auto_archive_procedure_worker_spec.rb --- spec/workers/auto_archive_procedure_worker_spec.rb | 10 ---------- 1 file changed, 10 deletions(-) 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