diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 08d12bf3d..1e41ffe4e 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -24,6 +24,7 @@ # last_commentaire_updated_at :datetime # motivation :text # private_search_terms :text +# process_expired :boolean default(FALSE) # processed_at :datetime # search_terms :text # state :string @@ -91,20 +92,17 @@ class Dossier < ApplicationRecord def passer_en_construction(instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:en_construction), instructeur_email: instructeur&.email, - process_expired: false, processed_at: processed_at) end def passer_en_instruction(instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:en_instruction), instructeur_email: instructeur&.email, - process_expired: false, processed_at: processed_at) end def accepter_automatiquement(processed_at: Time.zone.now) build(state: Dossier.states.fetch(:accepte), - process_expired: proxy_association.owner.procedure.feature_enabled?(:procedure_process_expired_dossiers_termine), processed_at: processed_at) end @@ -112,7 +110,6 @@ class Dossier < ApplicationRecord build(state: Dossier.states.fetch(:accepte), instructeur_email: instructeur&.email, motivation: motivation, - process_expired: proxy_association.owner.procedure.feature_enabled?(:procedure_process_expired_dossiers_termine), processed_at: processed_at) end @@ -120,7 +117,6 @@ class Dossier < ApplicationRecord build(state: Dossier.states.fetch(:refuse), instructeur_email: instructeur&.email, motivation: motivation, - process_expired: proxy_association.owner.procedure.feature_enabled?(:procedure_process_expired_dossiers_termine), processed_at: processed_at) end @@ -128,7 +124,6 @@ class Dossier < ApplicationRecord build(state: Dossier.states.fetch(:sans_suite), instructeur_email: instructeur&.email, motivation: motivation, - process_expired: proxy_association.owner.procedure.feature_enabled?(:procedure_process_expired_dossiers_termine), processed_at: processed_at) end end @@ -300,11 +295,10 @@ class Dossier < ApplicationRecord scope :interval_en_construction_close_to_expiration, -> do state_en_construction.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 :interval_en_instruction_close_to_expiration, -> do - state_en_instruction.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 :interval_termine_close_to_expiration, -> do - state_termine.where(id: Traitement.termine_close_to_expiration.select(:dossier_id).distinct) + state_termine + .where(procedures: { procedure_expires_when_termine_enabled: true}) + .where("dossiers.processed_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 @@ -313,9 +307,6 @@ class Dossier < ApplicationRecord scope :en_construction_close_to_expiration, -> do joins(:procedure).interval_en_construction_close_to_expiration end - scope :en_instruction_close_to_expiration, -> do - joins(:procedure).interval_en_instruction_close_to_expiration - end scope :termine_close_to_expiration, -> do joins(:procedure).interval_termine_close_to_expiration end @@ -324,7 +315,6 @@ class Dossier < ApplicationRecord joins(:procedure).scoping do interval_brouillon_close_to_expiration .or(interval_en_construction_close_to_expiration) - .or(interval_en_instruction_close_to_expiration) .or(interval_termine_close_to_expiration) end end @@ -545,7 +535,11 @@ class Dossier < ApplicationRecord end def expirable? - [brouillon?, en_construction?, termine? && procedure.feature_enabled?(:procedure_process_expired_dossiers_termine)].any? + [ + brouillon?, + en_construction?, + termine? && procedure.procedure_expires_when_termine_enabled + ].any? end def approximative_expiration_date_reference diff --git a/app/models/procedure.rb b/app/models/procedure.rb index eab2c373d..7d4397392 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -34,6 +34,7 @@ # monavis_embed :text # organisation :string # path :string not null +# procedure_expires_when_termine_enabled :boolean default(FALSE) # published_at :datetime # routing_criteria_name :text default("Votre ville") # routing_enabled :boolean diff --git a/app/models/traitement.rb b/app/models/traitement.rb index 8e6ce610f..86d7fd79b 100644 --- a/app/models/traitement.rb +++ b/app/models/traitement.rb @@ -2,13 +2,14 @@ # # Table name: traitements # -# id :bigint not null, primary key -# instructeur_email :string -# motivation :string -# process_expired :boolean -# processed_at :datetime -# state :string -# dossier_id :bigint +# id :bigint not null, primary key +# instructeur_email :string +# motivation :string +# process_expired :boolean +# process_expired_migrated :boolean default(FALSE) +# processed_at :datetime +# state :string +# dossier_id :bigint # class Traitement < ApplicationRecord belongs_to :dossier, optional: false diff --git a/db/migrate/20211126152402_move_process_expire_to_procedures.rb b/db/migrate/20211126152402_move_process_expire_to_procedures.rb new file mode 100644 index 000000000..c38dc9e7a --- /dev/null +++ b/db/migrate/20211126152402_move_process_expire_to_procedures.rb @@ -0,0 +1,17 @@ +class MoveProcessExpireToProcedures < ActiveRecord::Migration[6.1] + include Database::MigrationHelpers + + disable_ddl_transaction! + + def up + add_column :procedures, :procedure_expires_when_termine_enabled, :boolean, default: false + add_column :traitements, :process_expired_migrated, :boolean, default: false + add_concurrent_index :procedures, :procedure_expires_when_termine_enabled + end + + def down + remove_index :procedures, name: :index_procedures_on_process_expired + remove_column :traitements, :process_expired_migrated + remove_column :procedures, :procedure_expires_when_termine_enabled + end +end diff --git a/db/schema.rb b/db/schema.rb index 64a46604c..6d8197219 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -320,12 +320,12 @@ ActiveRecord::Schema.define(version: 2021_11_27_143736) do t.interval "conservation_extension", default: "PT0S" t.string "deleted_user_email_never_send" t.datetime "declarative_triggered_at" - t.index "to_tsvector('french'::regconfig, (search_terms || private_search_terms))", name: "index_dossiers_on_search_terms_private_search_terms", using: :gin - t.index "to_tsvector('french'::regconfig, search_terms)", name: "index_dossiers_on_search_terms", using: :gin t.bigint "dossier_transfer_id" t.datetime "identity_updated_at" t.datetime "depose_at" t.datetime "hidden_by_user_at" + t.index "to_tsvector('french'::regconfig, (search_terms || private_search_terms))", name: "index_dossiers_on_search_terms_private_search_terms", using: :gin + t.index "to_tsvector('french'::regconfig, search_terms)", name: "index_dossiers_on_search_terms", using: :gin t.index ["archived"], name: "index_dossiers_on_archived" t.index ["dossier_transfer_id"], name: "index_dossiers_on_dossier_transfer_id" t.index ["groupe_instructeur_id"], name: "index_dossiers_on_groupe_instructeur_id" @@ -646,15 +646,21 @@ ActiveRecord::Schema.define(version: 2021_11_27_143736) do t.text "api_particulier_scopes", default: [], array: true t.jsonb "api_particulier_sources", default: {} t.boolean "instructeurs_self_management_enabled" +<<<<<<< HEAD t.bigint "zone_id" t.index ["api_particulier_sources"], name: "index_procedures_on_api_particulier_sources", using: :gin +======= +>>>>>>> a6fcae97b (refactor(traitement.process_expired): move process expired to procedure) t.boolean "routing_enabled" + t.boolean "procedure_expires_when_termine_enabled", default: false + t.index ["api_particulier_sources"], name: "index_procedures_on_api_particulier_sources", using: :gin t.index ["declarative_with_state"], name: "index_procedures_on_declarative_with_state" t.index ["draft_revision_id"], name: "index_procedures_on_draft_revision_id" t.index ["hidden_at"], name: "index_procedures_on_hidden_at" t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id" t.index ["path", "closed_at", "hidden_at", "unpublished_at"], name: "procedure_path_uniqueness", unique: true t.index ["path", "closed_at", "hidden_at"], name: "index_procedures_on_path_and_closed_at_and_hidden_at", unique: true + t.index ["procedure_expires_when_termine_enabled"], name: "index_procedures_on_procedure_expires_when_termine_enabled" t.index ["published_revision_id"], name: "index_procedures_on_published_revision_id" t.index ["service_id"], name: "index_procedures_on_service_id" t.index ["zone_id"], name: "index_procedures_on_zone_id" @@ -745,6 +751,7 @@ ActiveRecord::Schema.define(version: 2021_11_27_143736) do t.datetime "processed_at" t.string "instructeur_email" t.boolean "process_expired" + t.boolean "process_expired_migrated", default: false t.index ["dossier_id"], name: "index_traitements_on_dossier_id" t.index ["process_expired"], name: "index_traitements_on_process_expired" end diff --git a/lib/tasks/deployment/20211126152402_move_traitement_process_expired_to_procedure.rake b/lib/tasks/deployment/20211126152402_move_traitement_process_expired_to_procedure.rake new file mode 100644 index 000000000..5a4f44df4 --- /dev/null +++ b/lib/tasks/deployment/20211126152402_move_traitement_process_expired_to_procedure.rake @@ -0,0 +1,21 @@ +namespace :after_party do + desc 'Deployment task: move_traitement_process_expired_to_procedure' + + task move_traitement_process_expired_to_procedure: :environment do + procedures = Procedure.joins(dossiers: :traitements).where(dossiers: {traitements: {process_expired: true, process_expired_migrated: false}}) + progress = ProgressReport.new(procedures.count) + + procedures.group(:id).find_each do |procedure| + ActiveRecord::Base.transaction do + puts "update traitements from dossier_ids: #{procedure.dossiers.ids}" + Traitement.where(id: procedure.dossiers.ids).update_all(process_expired_migrated: true) + Procedure.update(procedure_expires_when_termine_enabled: true) + progress.inc + end + end + progress.finish + + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 5a68978bc..af9efb863 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -109,39 +109,12 @@ describe Dossier do end end - describe 'en_instruction_close_to_expiration' do - let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6) } - let!(:young_dossier) { create(:dossier, procedure: procedure) } - let!(:expiring_dossier) { create(:dossier, :en_instruction, en_instruction_at: 175.days.ago, procedure: procedure) } - let!(:just_expired_dossier) { create(:dossier, :en_instruction, en_instruction_at: (6.months + 1.hour + 10.seconds).ago, procedure: procedure) } - let!(:long_expired_dossier) { create(:dossier, :en_instruction, en_instruction_at: 1.year.ago, procedure: procedure) } - - subject { Dossier.en_instruction_close_to_expiration } - - it do - is_expected.not_to include(young_dossier) - is_expected.to include(expiring_dossier) - is_expected.to include(just_expired_dossier) - is_expected.to include(long_expired_dossier) - end - - context 'when .close_to_expiration' do - subject { Dossier.close_to_expiration } - it do - is_expected.not_to include(young_dossier) - is_expected.to include(expiring_dossier) - is_expected.to include(just_expired_dossier) - is_expected.to include(long_expired_dossier) - end - end - end - describe 'termine_close_to_expiration' do - let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6) } - let!(:young_dossier) { create(:dossier, :accepte, procedure: procedure, traitements: [build(:traitement, :accepte)]) } - let!(:expiring_dossier) { create(:dossier, :accepte, procedure: procedure, traitements: [build(:traitement, :accepte, processed_at: 175.days.ago)]) } - let!(:just_expired_dossier) { create(:dossier, :accepte, procedure: procedure, traitements: [build(:traitement, :accepte, processed_at: (6.months + 1.hour + 10.seconds).ago)]) } - let!(:long_expired_dossier) { create(:dossier, :accepte, procedure: procedure, traitements: [build(:traitement, :accepte, processed_at: 1.year.ago)]) } + let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6, procedure_expires_when_termine_enabled: true) } + let!(:young_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 2.days.ago) } + let!(:expiring_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 175.days.ago) } + let!(:just_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: (6.months + 1.hour + 10.seconds).ago) } + let!(:long_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 1.year.ago) } subject { Dossier.termine_close_to_expiration }