Merge pull request #6574 from tchak/fix-discarded-dossiers-deletion

fix(dossier): fix dossier.avis cascade
This commit is contained in:
Paul Chavard 2021-10-26 12:01:10 +02:00 committed by GitHub
commit 05293ad849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 21 deletions

View file

@ -1,16 +1,7 @@
class Cron::DiscardedDossiersDeletionJob < Cron::CronJob class Cron::DiscardedDossiersDeletionJob < Cron::CronJob
self.schedule_expression = "every day at 2 am" self.schedule_expression = "every day at 2 am"
def perform(*args) def perform
DossierOperationLog.where(dossier: Dossier.discarded_en_construction_expired) Dossier.purge_discarded
.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
end end
end end

View file

@ -49,6 +49,7 @@ class Avis < ApplicationRecord
scope :for_dossier, -> (dossier_id) { where(dossier_id: dossier_id) } scope :for_dossier, -> (dossier_id) { where(dossier_id: dossier_id) }
scope :by_latest, -> { order(updated_at: :desc) } scope :by_latest, -> { order(updated_at: :desc) }
scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) } 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, # The form allows subtmitting avis requests to several emails at once,
# hence this virtual attribute. # hence this virtual attribute.

View file

@ -960,6 +960,21 @@ class Dossier < ApplicationRecord
user&.locale || I18n.default_locale user&.locale || I18n.default_locale
end 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 private
def defaut_groupe_instructeur? def defaut_groupe_instructeur?

View file

@ -36,6 +36,10 @@ class DossierOperationLog < ApplicationRecord
belongs_to :dossier, optional: true belongs_to :dossier, optional: true
belongs_to :bill_signature, 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) def self.create_and_serialize(params)
dossier = params.fetch(:dossier) dossier = params.fetch(:dossier)

View file

@ -182,18 +182,17 @@ class User < ApplicationRecord
raise "Cannot delete this user because they are also instructeur, expert or administrateur" raise "Cannot delete this user because they are also instructeur, expert or administrateur"
end end
transaction do
Invite.where(dossier: dossiers.with_discarded).destroy_all Invite.where(dossier: dossiers.with_discarded).destroy_all
dossiers.state_en_construction.each do |dossier| dossiers.state_en_construction.each do |dossier|
dossier.discard_and_keep_track!(administration, :user_removed) dossier.discard_and_keep_track!(administration, :user_removed)
end end
DossierOperationLog DossierOperationLog.where(dossier: dossiers.with_discarded.discarded).not_deletion.destroy_all
.where(dossier: dossiers.with_discarded.discarded)
.where.not(operation: DossierOperationLog.operations.fetch(:supprimer))
.destroy_all
dossiers.with_discarded.discarded.destroy_all dossiers.with_discarded.discarded.destroy_all
dossiers.update_all(deleted_user_email_never_send: email, user_id: nil, dossier_transfer_id: nil) dossiers.update_all(deleted_user_email_never_send: email, user_id: nil, dossier_transfer_id: nil)
destroy! destroy!
end end
end
private private