From d8257284efada3f1639a74b18b419208b7445a20 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 19 Nov 2021 14:07:47 +0100 Subject: [PATCH] tech(refactor): extract states close to expiration within their scope. --- app/models/dossier.rb | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 10e8c6ce1..1f0ded697 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -289,20 +289,35 @@ class Dossier < ApplicationRecord .where.not(user_id: nil) end + scope :interval_brouillon_close_to_expiration, -> do + where("dossiers.created_at + dossiers.conservation_extension + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) + end scope :brouillon_close_to_expiration, -> do - state_brouillon - .joins(:procedure) - .where("dossiers.created_at + dossiers.conservation_extension + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) + state_brouillon.joins(:procedure).interval_brouillon_close_to_expiration + end + + scope :interval_en_construction_close_to_expiration, -> do + where("dossiers.en_construction_at + dossiers.conservation_extension + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) end scope :en_construction_close_to_expiration, -> do - state_en_construction - .joins(:procedure) - .where("dossiers.en_construction_at + dossiers.conservation_extension + (duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: INTERVAL_BEFORE_EXPIRATION }) + state_en_construction.joins(:procedure).interval_en_construction_close_to_expiration + end + + scope :interval_en_instruction_close_to_expiration, -> do + 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 scope :en_instruction_close_to_expiration, -> do - state_en_instruction + state_en_instruction.joins(:procedure).interval_en_instruction_close_to_expiration + end + + + scope :interval_termine_close_to_expiration, -> do + where(id: Traitement.termine_close_to_expiration.select(:dossier_id).distinct) + end + scope :termine_close_to_expiration, -> do + state_termine .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 }) + .interval_termine_close_to_expiration end # TODO/MFO