From 51312b178e75a37b1842f02abfcde3ce6446e4e6 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 9 Apr 2024 16:22:43 +0200 Subject: [PATCH] task(routing): reroute dossiers en construction --- app/models/dossier_assignment.rb | 4 +- app/models/routing_engine.rb | 4 +- lib/tasks/re_routing_dossiers.rake | 27 +++++++++++++ spec/lib/tasks/re_routing_dossiers_spec.rb | 45 ++++++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 lib/tasks/re_routing_dossiers.rake create mode 100644 spec/lib/tasks/re_routing_dossiers_spec.rb diff --git a/app/models/dossier_assignment.rb b/app/models/dossier_assignment.rb index 90c517ad2..239a218d4 100644 --- a/app/models/dossier_assignment.rb +++ b/app/models/dossier_assignment.rb @@ -6,8 +6,10 @@ class DossierAssignment < ApplicationRecord enum mode: { auto: 'auto', - manual: 'manual' + manual: 'manual', + tech: 'tech' } + scope :manual, -> { where(mode: :manual) } def groupe_instructeur_label diff --git a/app/models/routing_engine.rb b/app/models/routing_engine.rb index 4d9e00c76..040becb72 100644 --- a/app/models/routing_engine.rb +++ b/app/models/routing_engine.rb @@ -1,5 +1,5 @@ module RoutingEngine - def self.compute(dossier) + def self.compute(dossier, assignment_mode: DossierAssignment.modes.fetch(:auto)) return if dossier.forced_groupe_instructeur matching_groupe = dossier.procedure.groupe_instructeurs.active.reject(&:invalid_rule?).find do |gi| @@ -8,6 +8,6 @@ module RoutingEngine matching_groupe ||= dossier.procedure.defaut_groupe_instructeur - dossier.assign_to_groupe_instructeur(matching_groupe, DossierAssignment.modes.fetch(:auto)) + dossier.assign_to_groupe_instructeur(matching_groupe, assignment_mode) end end diff --git a/lib/tasks/re_routing_dossiers.rake b/lib/tasks/re_routing_dossiers.rake new file mode 100644 index 000000000..93925f090 --- /dev/null +++ b/lib/tasks/re_routing_dossiers.rake @@ -0,0 +1,27 @@ +require Rails.root.join("lib", "tasks", "task_helper") + +namespace :re_routing_dossiers do + desc <<~EOD + Given an procedure id in argument, run the RoutingEngine again for all "en construction" dossiers of the procedure + ex: rails re_routing_dossiers:run\[85869\] + EOD + + task :run, [:procedure_id] => :environment do |_t, args| + procedure = Procedure.find_by(id: args[:procedure_id]) + + dossiers = procedure.dossiers.state_en_construction + + progress = ProgressReport.new(dossiers.count) + + assignment_mode = DossierAssignment.modes.fetch(:tech) + + dossiers.each do |dossier| + RoutingEngine.compute(dossier, assignment_mode:) + + rake_puts "Dossier #{args[:dossier_id]} routed to groupe instructeur #{dossier.groupe_instructeur.label}" + + progress.inc + end + progress.finish + end +end diff --git a/spec/lib/tasks/re_routing_dossiers_spec.rb b/spec/lib/tasks/re_routing_dossiers_spec.rb new file mode 100644 index 000000000..5721bad1a --- /dev/null +++ b/spec/lib/tasks/re_routing_dossiers_spec.rb @@ -0,0 +1,45 @@ +describe 're_routing_dossiers' do + describe 'run' do + include Logic + + let(:admin) { create(:administrateur) } + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :departements, libelle: 'Votre département' }], administrateurs: [admin]) } + let(:dossier1) { create(:dossier, :en_construction, :with_populated_champs, procedure: procedure) } + let!(:dossier2) { create(:dossier, :en_construction, :with_populated_champs, procedure: procedure) } + + before do + dossier1.champs.last.update(value: 'Aisne') + + dossier2.champs.last.update(value: 'Allier') + + tdc = procedure.active_revision.routable_types_de_champ.first + + tdc_options = APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } + + rule_operator = :ds_eq + + create_groups_from_territorial_tdc(tdc_options, tdc.stable_id, rule_operator, admin) + + Rake.application.invoke_task "re_routing_dossiers:run\[#{procedure.id}\]" + + dossier1.reload + dossier2.reload + end + + it 'runs' do + expect(dossier1.groupe_instructeur.label).to eq('02 – Aisne') + expect(dossier2.groupe_instructeur.label).to eq('03 – Allier') + end + + def create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator, administrateur) + tdc_options.each do |label, code| + routing_rule = send(rule_operator, champ_value(stable_id), constant(code)) + + procedure + .groupe_instructeurs + .find_or_create_by(label: label) + .update(instructeurs: [administrateur.instructeur], routing_rule:) + end + end + end +end