Merge pull request #5099 from tchak/fix-draft-notofications

Do not send draft norifications to users on inactive démarches
This commit is contained in:
Paul Chavard 2020-05-27 17:23:29 +02:00 committed by GitHub
commit 8c5fd58bd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 10 deletions

View file

@ -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

View file

@ -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 }

View file

@ -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