diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 47b6950ea..10e8c6ce1 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -304,10 +304,21 @@ class Dossier < ApplicationRecord .joins(:procedure) .where("dossiers.en_instruction_at + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) end + + # TODO/MFO + # maybe reconsider `termine_close_to_expiration` implementation, but for now this implementation is missing + # 1. the check on traitements.processed_at? -> is is replicated on dossier? + # 2. the check on process_expired + # scope :termine_close_to_expiration, -> do + # state_termine + # .joins(:procedure) + # .where("dossiers.processed_at + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) + # end + # TODO/MFO scope :termine_close_to_expiration, -> do state_termine .joins(:procedure) - .where("dossiers.processed_at + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) + .where(id: Traitement.termine_close_to_expiration.select(:dossier_id).distinct) end scope :brouillon_expired, -> do diff --git a/app/models/traitement.rb b/app/models/traitement.rb index 3e7f264f5..8e6ce610f 100644 --- a/app/models/traitement.rb +++ b/app/models/traitement.rb @@ -25,6 +25,14 @@ class Traitement < ApplicationRecord .order(:processed_at) end + scope :termine_close_to_expiration, -> do + joins(dossier: :procedure) + .termine + .where(process_expired: true) + .where('dossiers.state' => Dossier::TERMINE) + .where("traitements.processed_at + (procedures.duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: Dossier::INTERVAL_BEFORE_EXPIRATION }) + end + def self.count_dossiers_termines_by_month(groupe_instructeurs) last_traitements_per_dossier = Traitement .select('max(traitements.processed_at) as processed_at')