commit
3509f1d0ee
4 changed files with 74 additions and 10 deletions
|
@ -177,9 +177,10 @@ class Dossier < ApplicationRecord
|
||||||
user: [])
|
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)
|
joins(:procedure)
|
||||||
.where.not(procedures: { aasm_state: :brouillon })
|
.where(procedures: { aasm_state: states })
|
||||||
end
|
end
|
||||||
|
|
||||||
scope :brouillon_close_to_expiration, -> do
|
scope :brouillon_close_to_expiration, -> do
|
||||||
|
@ -241,7 +242,8 @@ class Dossier < ApplicationRecord
|
||||||
.where("groupe_instructeurs.procedure_id = procedures.id")
|
.where("groupe_instructeurs.procedure_id = procedures.id")
|
||||||
.select(:user_id)
|
.select(:user_id)
|
||||||
# select dossier in brouillon where procedure closes in two days and for which the user has not submitted a Dossier
|
# 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("procedures.auto_archive_on - INTERVAL :before_closing = :now", { now: Time.zone.today, before_closing: INTERVAL_BEFORE_CLOSING })
|
||||||
.where.not(user: users_who_submitted)
|
.where.not(user: users_who_submitted)
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,7 +77,7 @@ class ExpiredDossiersDeletionService
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.delete_expired_termine_and_notify
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -108,11 +108,11 @@ class ExpiredDossiersDeletionService
|
||||||
dossiers_close_to_expiration.update_all(close_to_expiration_flag => Time.zone.now)
|
dossiers_close_to_expiration.update_all(close_to_expiration_flag => Time.zone.now)
|
||||||
end
|
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.each(&:expired_keep_track!)
|
||||||
|
|
||||||
dossiers_to_remove
|
dossiers_to_remove
|
||||||
.with_notifiable_procedure
|
.with_notifiable_procedure(notify_on_closed: notify_on_closed_procedures_to_user)
|
||||||
.includes(:user)
|
.includes(:user)
|
||||||
.group_by(&:user)
|
.group_by(&:user)
|
||||||
.each do |(user, dossiers)|
|
.each do |(user, dossiers)|
|
||||||
|
@ -138,7 +138,7 @@ class ExpiredDossiersDeletionService
|
||||||
|
|
||||||
def self.group_by_fonctionnaire_email(dossiers)
|
def self.group_by_fonctionnaire_email(dossiers)
|
||||||
dossiers
|
dossiers
|
||||||
.with_notifiable_procedure
|
.with_notifiable_procedure(notify_on_closed: true)
|
||||||
.includes(:followers_instructeurs, procedure: [:administrateurs])
|
.includes(:followers_instructeurs, procedure: [:administrateurs])
|
||||||
.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |dossier, h|
|
.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 }
|
(dossier.followers_instructeurs + dossier.procedure.administrateurs).each { |destinataire| h[destinataire.email] << dossier }
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
namespace :after_party do
|
||||||
|
desc 'Deployment task: fix_champ_etablissement'
|
||||||
|
task fix_champ_etablissement: :environment do
|
||||||
|
puts "Running deploy task 'fix_champ_etablissement'"
|
||||||
|
|
||||||
|
etablissements = Etablissement.joins(:champ).where.not(dossier_id: nil).where('etablissements.created_at > ?', 1.month.ago)
|
||||||
|
dossiers_modif = []
|
||||||
|
etablissements.find_each do |e|
|
||||||
|
if e.dossier
|
||||||
|
user = e.dossier.user
|
||||||
|
dossier = e.dossier
|
||||||
|
if user.dossiers.count == 1 && user.siret == e.champ.value
|
||||||
|
e.update!(dossier_id: nil)
|
||||||
|
dossier.etablissement = e.dup
|
||||||
|
dossier.save!
|
||||||
|
dossiers_modif << dossier.id
|
||||||
|
fetch_api_entreprise_infos(dossier.etablissement.id, dossier.procedure.id, user.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
puts "Nb dossiers modifiés: #{dossiers_modif.size}"
|
||||||
|
AfterParty::TaskRecord.create version: '20200527124112'
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_api_entreprise_infos(etablissement_id, procedure_id, user_id)
|
||||||
|
[
|
||||||
|
ApiEntreprise::EntrepriseJob, ApiEntreprise::AssociationJob, ApiEntreprise::ExercicesJob,
|
||||||
|
ApiEntreprise::EffectifsJob, ApiEntreprise::EffectifsAnnuelsJob, ApiEntreprise::AttestationSocialeJob,
|
||||||
|
ApiEntreprise::BilansBdfJob
|
||||||
|
].each do |job|
|
||||||
|
job.perform_later(etablissement_id, procedure_id)
|
||||||
|
end
|
||||||
|
ApiEntreprise::AttestationFiscaleJob.perform_later(etablissement_id, procedure_id, user_id)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1128,9 +1128,9 @@ describe Dossier do
|
||||||
describe '#notify_draft_not_submitted' do
|
describe '#notify_draft_not_submitted' do
|
||||||
let!(:user1) { create(:user) }
|
let!(:user1) { create(:user) }
|
||||||
let!(:user2) { 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_near_closing) { create(:procedure, :published, 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_later) { create(:procedure, :published, 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_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
|
# 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) }
|
let!(:draft_near_closing) { create(:dossier, user: user1, procedure: procedure_near_closing) }
|
||||||
|
@ -1265,4 +1265,31 @@ describe Dossier do
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue