fix(dossier): fix dossier.avis cascade

This commit is contained in:
Paul Chavard 2021-10-21 13:29:47 +02:00
parent 6eae97944d
commit d5d3995dad
4 changed files with 22 additions and 11 deletions

View file

@ -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

View file

@ -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.

View file

@ -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?

View file

@ -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)