From 20705d6e3019d27c41fb5095a0e8b172449eddb3 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 29 Apr 2020 19:18:12 +0200 Subject: [PATCH] =?UTF-8?q?Do=20not=20send=20draft=20norifications=20to=20?= =?UTF-8?q?users=20on=20inactive=20d=C3=A9marches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/dossier.rb | 8 +++-- .../expired_dossiers_deletion_service.rb | 8 ++--- spec/models/dossier_spec.rb | 33 +++++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 87f2d27cd..ea11bc25b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -177,9 +177,10 @@ class Dossier < ApplicationRecord user: []) } - scope :with_notifiable_procedure, -> do + scope :with_notifiable_procedure, -> (notify_on_closed: false) do + states = notify_on_closed ? [:publiee, :close, :depubliee] : [:publiee, :depubliee] joins(:procedure) - .where.not(procedures: { aasm_state: :brouillon }) + .where(procedures: { aasm_state: states }) end scope :brouillon_close_to_expiration, -> do @@ -241,7 +242,8 @@ class Dossier < ApplicationRecord .where("groupe_instructeurs.procedure_id = procedures.id") .select(:user_id) # select dossier in brouillon where procedure closes in two days and for which the user has not submitted a Dossier - brouillon.joins(:procedure) + state_brouillon + .with_notifiable_procedure .where("procedures.auto_archive_on - INTERVAL :before_closing = :now", { now: Time.zone.today, before_closing: INTERVAL_BEFORE_CLOSING }) .where.not(user: users_who_submitted) end diff --git a/app/services/expired_dossiers_deletion_service.rb b/app/services/expired_dossiers_deletion_service.rb index 6e0bb1f0d..fbf2c3752 100644 --- a/app/services/expired_dossiers_deletion_service.rb +++ b/app/services/expired_dossiers_deletion_service.rb @@ -77,7 +77,7 @@ class ExpiredDossiersDeletionService end def self.delete_expired_termine_and_notify - delete_expired_and_notify(Dossier.termine_expired) + delete_expired_and_notify(Dossier.termine_expired, notify_on_closed_procedures_to_user: true) end private @@ -108,11 +108,11 @@ class ExpiredDossiersDeletionService dossiers_close_to_expiration.update_all(close_to_expiration_flag => Time.zone.now) end - def self.delete_expired_and_notify(dossiers_to_remove) + def self.delete_expired_and_notify(dossiers_to_remove, notify_on_closed_procedures_to_user: false) dossiers_to_remove.each(&:expired_keep_track!) dossiers_to_remove - .with_notifiable_procedure + .with_notifiable_procedure(notify_on_closed: notify_on_closed_procedures_to_user) .includes(:user) .group_by(&:user) .each do |(user, dossiers)| @@ -138,7 +138,7 @@ class ExpiredDossiersDeletionService def self.group_by_fonctionnaire_email(dossiers) dossiers - .with_notifiable_procedure + .with_notifiable_procedure(notify_on_closed: true) .includes(:followers_instructeurs, procedure: [:administrateurs]) .each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |dossier, h| (dossier.followers_instructeurs + dossier.procedure.administrateurs).each { |destinataire| h[destinataire.email] << dossier } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 33aca4eb8..900e22e7e 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1128,9 +1128,9 @@ describe Dossier do describe '#notify_draft_not_submitted' do let!(:user1) { create(:user) } let!(:user2) { create(:user) } - let!(:procedure_near_closing) { create(:procedure, auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days) } - let!(:procedure_closed_later) { create(:procedure, auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days + 1.day) } - let!(:procedure_closed_before) { create(:procedure, auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days - 1.day) } + let!(:procedure_near_closing) { create(:procedure, :published, auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days) } + let!(:procedure_closed_later) { create(:procedure, :published, auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days + 1.day) } + let!(:procedure_closed_before) { create(:procedure, :published, auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days - 1.day) } # user 1 has three draft dossiers where one is for procedure that closes in two days ==> should trigger one mail let!(:draft_near_closing) { create(:dossier, user: user1, procedure: procedure_near_closing) } @@ -1265,4 +1265,31 @@ describe Dossier do }) end end + + describe "with_notifiable_procedure" do + let(:test_procedure) { create(:procedure) } + let(:published_procedure) { create(:procedure, :published) } + let(:closed_procedure) { create(:procedure, :closed) } + let(:unpublished_procedure) { create(:procedure, :unpublished) } + + let!(:dossier_on_test_procedure) { create(:dossier, procedure: test_procedure) } + let!(:dossier_on_published_procedure) { create(:dossier, procedure: published_procedure) } + let!(:dossier_on_closed_procedure) { create(:dossier, procedure: closed_procedure) } + let!(:dossier_on_unpublished_procedure) { create(:dossier, procedure: unpublished_procedure) } + + let(:notify_on_closed) { false } + let(:dossiers) { Dossier.with_notifiable_procedure(notify_on_closed: notify_on_closed) } + + it 'should find dossiers with notifiable procedure' do + expect(dossiers).to match_array([dossier_on_published_procedure, dossier_on_unpublished_procedure]) + end + + context 'when notify on closed is true' do + let(:notify_on_closed) { true } + + it 'should find dossiers with notifiable procedure' do + expect(dossiers).to match_array([dossier_on_published_procedure, dossier_on_closed_procedure, dossier_on_unpublished_procedure]) + end + end + end end