diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 528c67a22..c72486994 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -22,7 +22,7 @@ class Dossier < ApplicationRecord TAILLE_MAX_ZIP = 50.megabytes - REMAINING_TIME_BEFORE_CLOSING = 2.days + REMAINING_DAYS_BEFORE_CLOSING = 2 has_one :etablissement, dependent: :destroy has_one :individual, validate: false, dependent: :destroy @@ -188,7 +188,7 @@ class Dossier < ApplicationRecord scope :without_brouillon_expiration_notice_sent, -> { where(brouillon_close_to_expiration_notice_sent_at: nil) } scope :without_en_construction_expiration_notice_sent, -> { where(en_construction_close_to_expiration_notice_sent_at: nil) } - scope :brouillon_close_to_procedure_closing_date, -> do + scope :brouillon_near_procedure_closing_date, -> do # select users who have submitted dossier for the given 'procedures.id' users_who_submitted = state_not_brouillon @@ -197,7 +197,7 @@ class Dossier < ApplicationRecord .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) - .where(procedures: { auto_archive_on: Time.zone.today + REMAINING_TIME_BEFORE_CLOSING }) + .where("procedures.auto_archive_on = (now() + INTERVAL '#{REMAINING_DAYS_BEFORE_CLOSING} days')::date") .where.not(user: users_who_submitted) end @@ -707,8 +707,8 @@ class Dossier < ApplicationRecord end def self.notify_draft_not_submitted - brouillon_close_to_procedure_closing_date - .includes(:procedure, :user) + brouillon_near_procedure_closing_date + .includes(:user) .find_each do |dossier| DossierMailer.notify_brouillon_not_submitted(dossier).deliver_later end diff --git a/spec/mailers/previews/dossier_mailer_preview.rb b/spec/mailers/previews/dossier_mailer_preview.rb index 19354e2da..c959003fd 100644 --- a/spec/mailers/previews/dossier_mailer_preview.rb +++ b/spec/mailers/previews/dossier_mailer_preview.rb @@ -80,7 +80,7 @@ class DossierMailerPreview < ActionMailer::Preview end def procedure - Procedure.new(id: 1234, libelle: 'Dotation d’Équipement des Territoires Ruraux - Exercice 2019', service: service, logo: Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png'), auto_archive_on: Time.zone.today + Dossier::REMAINING_TIME_BEFORE_CLOSING) + Procedure.new(id: 1234, libelle: 'Dotation d’Équipement des Territoires Ruraux - Exercice 2019', service: service, logo: Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png'), auto_archive_on: Time.zone.today + Dossier::REMAINING_DAYS_BEFORE_CLOSING.days) end def service diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index b484d0964..e36469e75 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1155,18 +1155,18 @@ describe Dossier do describe '#notify_draft_not_submitted' do let!(:user1) { create(:user) } let!(:user2) { create(:user) } - let!(:procedure_closed_in_time) { create(:procedure, auto_archive_on: Time.zone.today + Dossier::REMAINING_TIME_BEFORE_CLOSING) } - let!(:procedure_closed_later) { create(:procedure, auto_archive_on: Time.zone.today + Dossier::REMAINING_TIME_BEFORE_CLOSING + 1.day) } - let!(:procedure_closed_before) { create(:procedure, auto_archive_on: Time.zone.today + Dossier::REMAINING_TIME_BEFORE_CLOSING - 1.day) } + 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) } # 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_closed_in_time) } + let!(:draft_near_closing) { create(:dossier, user: user1, procedure: procedure_near_closing) } let!(:draft_before) { create(:dossier, user: user1, procedure: procedure_closed_before) } let!(:draft_later) { create(:dossier, user: user1, procedure: procedure_closed_later) } # user 2 submitted a draft and en_construction dossier for the same procedure ==> should not trigger the mail - let!(:draft_near_closing_2) { create(:dossier, :en_construction, user: user2, procedure: procedure_closed_in_time) } - let!(:submitted_in_time_2) { create(:dossier, user: user2, procedure: procedure_closed_in_time) } + let!(:draft_near_closing_2) { create(:dossier, :en_construction, user: user2, procedure: procedure_near_closing) } + let!(:submitted_near_closing_2) { create(:dossier, user: user2, procedure: procedure_near_closing) } before do allow(DossierMailer).to receive(:notify_brouillon_not_submitted).and_return(double(deliver_later: nil))