diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 89a665419..c18215983 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -362,7 +362,7 @@ module Users Dossier.with_champs.find(params[:id]) end - def change_groupe_instructeur? + def should_change_groupe_instructeur? if params[:dossier].key?(:groupe_instructeur_id) groupe_instructeur_id = params[:dossier][:groupe_instructeur_id] if groupe_instructeur_id.nil? @@ -380,6 +380,14 @@ module Users end end + def should_fill_groupe_instructeur? + !@dossier.procedure.routee? && @dossier.groupe_instructeur_id.nil? + end + + def defaut_groupe_instructeur + @dossier.procedure.defaut_groupe_instructeur + end + def update_dossier_and_compute_errors errors = [] @@ -396,11 +404,15 @@ module Users if !@dossier.save(**validation_options) errors += @dossier.errors.full_messages - elsif change_groupe_instructeur? + elsif should_change_groupe_instructeur? @dossier.assign_to_groupe_instructeur(groupe_instructeur_from_params) end end + if should_fill_groupe_instructeur? + @dossier.assign_to_groupe_instructeur(defaut_groupe_instructeur) + end + if !save_draft? errors += @dossier.check_mandatory_champs diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index 8f72114cd..9c5d07a2b 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -11,8 +11,7 @@ # agent_connect_id :string # class Instructeur < ApplicationRecord - has_many :administrateurs_instructeurs - has_many :administrateurs, through: :administrateurs_instructeurs + has_and_belongs_to_many :administrateurs has_many :assign_to, dependent: :destroy has_many :groupe_instructeurs, through: :assign_to diff --git a/app/views/administrateurs/procedures/_procedures_list.html.haml b/app/views/administrateurs/procedures/_procedures_list.html.haml index b8592c9a9..372a09c2e 100644 --- a/app/views/administrateurs/procedures/_procedures_list.html.haml +++ b/app/views/administrateurs/procedures/_procedures_list.html.haml @@ -26,7 +26,7 @@ %span.badge.baseline= procedure.instructeurs.count %span.icon.folder - %span.badge.baseline= procedure.dossiers.count + %span.badge.baseline= procedure.dossiers.state_not_brouillon.visible_by_administration.count %div = link_to admin_procedure_path(procedure), class: 'button mr-1 edit-procedure' do diff --git a/db/migrate/20220302101337_add_foreign_keys_to_administrateurs_instructeurs.rb b/db/migrate/20220302101337_add_foreign_keys_to_administrateurs_instructeurs.rb new file mode 100644 index 000000000..fc524565f --- /dev/null +++ b/db/migrate/20220302101337_add_foreign_keys_to_administrateurs_instructeurs.rb @@ -0,0 +1,22 @@ +class AddForeignKeysToAdministrateursInstructeurs < ActiveRecord::Migration[6.1] + def up + # Sanity check + say_with_time 'Removing AdministrateursInstructeur where the associated Administrateur no longer exists ' do + deleted_administrateurs_ids = AdministrateursInstructeur.where.missing(:administrateur).pluck(:administrateur_id) + AdministrateursInstructeur.where(administrateur_id: deleted_administrateurs_ids).delete_all + end + + say_with_time 'Removing AdministrateursInstructeur where the associated Instructeur no longer exists ' do + deleted_instructeurs_ids = AdministrateursInstructeur.where.missing(:instructeur).pluck(:instructeur_id) + AdministrateursInstructeur.where(instructeur_id: deleted_instructeurs_ids).delete_all + end + + add_foreign_key :administrateurs_instructeurs, :administrateurs + add_foreign_key :administrateurs_instructeurs, :instructeurs + end + + def down + remove_foreign_key :administrateurs_instructeurs, :administrateurs + remove_foreign_key :administrateurs_instructeurs, :instructeurs + end +end diff --git a/db/schema.rb b/db/schema.rb index 3ef845f12..16c13c8d7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_02_04_130722) do +ActiveRecord::Schema.define(version: 2022_03_02_101337) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -852,6 +852,9 @@ ActiveRecord::Schema.define(version: 2022_02_04_130722) do end add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "administrateurs_instructeurs", "administrateurs" + add_foreign_key "administrateurs_instructeurs", "instructeurs" + add_foreign_key "administrateurs_procedures", "administrateurs" add_foreign_key "archives_groupe_instructeurs", "archives" add_foreign_key "archives_groupe_instructeurs", "groupe_instructeurs" add_foreign_key "assign_tos", "groupe_instructeurs" diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 9020996e6..efc58b459 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -464,6 +464,17 @@ describe Users::DossiersController, type: :controller do end end + context "when the dossier was created on a routee procedure, but routage was later disabled" do + let(:dossier) { create(:dossier, groupe_instructeur: nil, user: user) } + + it "sets a default groupe_instructeur" do + subject + + expect(response).to redirect_to(merci_dossier_path(dossier)) + expect(dossier.reload.groupe_instructeur).to eq(dossier.procedure.defaut_groupe_instructeur) + end + end + context "on an closed procedure" do before { dossier.procedure.close! } diff --git a/spec/models/instructeur_spec.rb b/spec/models/instructeur_spec.rb index bc0c87d94..b108cfe95 100644 --- a/spec/models/instructeur_spec.rb +++ b/spec/models/instructeur_spec.rb @@ -12,6 +12,10 @@ describe Instructeur, type: :model do procedure_3 end + describe 'associations' do + it { is_expected.to have_and_belong_to_many(:administrateurs) } + end + describe 'follow' do let(:dossier) { create :dossier } let(:already_followed_dossier) { create :dossier }