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/lib/tasks/deployment/20200527124112_fix_champ_etablissement.rake b/lib/tasks/deployment/20200527124112_fix_champ_etablissement.rake new file mode 100644 index 000000000..68e6ae301 --- /dev/null +++ b/lib/tasks/deployment/20200527124112_fix_champ_etablissement.rake @@ -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 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