diff --git a/app/jobs/cron/discarded_dossiers_deletion_job.rb b/app/jobs/cron/discarded_dossiers_deletion_job.rb index 45dd82fe6..9db9623bf 100644 --- a/app/jobs/cron/discarded_dossiers_deletion_job.rb +++ b/app/jobs/cron/discarded_dossiers_deletion_job.rb @@ -1,16 +1,7 @@ class Cron::DiscardedDossiersDeletionJob < Cron::CronJob self.schedule_expression = "every day at 2 am" - def perform(*args) - DossierOperationLog.where(dossier: Dossier.discarded_en_construction_expired) - .where.not(operation: DossierOperationLog.operations.fetch(:supprimer)) - .destroy_all - DossierOperationLog.where(dossier: Dossier.discarded_termine_expired) - .where.not(operation: DossierOperationLog.operations.fetch(:supprimer)) - .destroy_all - - Dossier.discarded_brouillon_expired.destroy_all - Dossier.discarded_en_construction_expired.destroy_all - Dossier.discarded_termine_expired.destroy_all + def perform + Dossier.purge_discarded end end diff --git a/app/models/avis.rb b/app/models/avis.rb index ff45b0fe2..bd23547cd 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -49,6 +49,7 @@ class Avis < ApplicationRecord scope :for_dossier, -> (dossier_id) { where(dossier_id: dossier_id) } scope :by_latest, -> { order(updated_at: :desc) } scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) } + scope :discarded_termine_expired, -> { unscope(:joins).where(dossier: Dossier.discarded_termine_expired) } # The form allows subtmitting avis requests to several emails at once, # hence this virtual attribute. diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 92682a766..8f3f2bb13 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -960,6 +960,21 @@ class Dossier < ApplicationRecord user&.locale || I18n.default_locale end + def self.purge_discarded + discarded_brouillon_expired.destroy_all + + transaction do + DossierOperationLog.discarded_en_construction_expired.destroy_all + discarded_en_construction_expired.destroy_all + end + + transaction do + DossierOperationLog.discarded_termine_expired.destroy_all + Avis.discarded_termine_expired.destroy_all + discarded_termine_expired.destroy_all + end + end + private def defaut_groupe_instructeur? diff --git a/app/models/dossier_operation_log.rb b/app/models/dossier_operation_log.rb index 2144ebc57..9ecd29d3b 100644 --- a/app/models/dossier_operation_log.rb +++ b/app/models/dossier_operation_log.rb @@ -36,6 +36,10 @@ class DossierOperationLog < ApplicationRecord belongs_to :dossier, optional: true belongs_to :bill_signature, optional: true + scope :not_deletion, -> { where.not(operation: operations.fetch(:supprimer)) } + scope :discarded_en_construction_expired, -> { where(dossier: Dossier.discarded_en_construction_expired).not_deletion } + scope :discarded_termine_expired, -> { where(dossier: Dossier.discarded_termine_expired).not_deletion } + def self.create_and_serialize(params) dossier = params.fetch(:dossier) diff --git a/app/models/user.rb b/app/models/user.rb index 6b89c37f5..f2143203a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -182,17 +182,16 @@ class User < ApplicationRecord raise "Cannot delete this user because they are also instructeur, expert or administrateur" end - Invite.where(dossier: dossiers.with_discarded).destroy_all - dossiers.state_en_construction.each do |dossier| - dossier.discard_and_keep_track!(administration, :user_removed) + transaction do + Invite.where(dossier: dossiers.with_discarded).destroy_all + dossiers.state_en_construction.each do |dossier| + dossier.discard_and_keep_track!(administration, :user_removed) + end + DossierOperationLog.where(dossier: dossiers.with_discarded.discarded).not_deletion.destroy_all + dossiers.with_discarded.discarded.destroy_all + dossiers.update_all(deleted_user_email_never_send: email, user_id: nil, dossier_transfer_id: nil) + destroy! end - DossierOperationLog - .where(dossier: dossiers.with_discarded.discarded) - .where.not(operation: DossierOperationLog.operations.fetch(:supprimer)) - .destroy_all - dossiers.with_discarded.discarded.destroy_all - dossiers.update_all(deleted_user_email_never_send: email, user_id: nil, dossier_transfer_id: nil) - destroy! end private