From a480b31eb5582ec6fb378541287002d457af001c Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 4 Oct 2021 15:26:11 +0200 Subject: [PATCH] merge expert --- app/controllers/manager/users_controller.rb | 6 +++++- app/models/expert.rb | 6 ++++++ spec/controllers/manager/users_controller_spec.rb | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/controllers/manager/users_controller.rb b/app/controllers/manager/users_controller.rb index fcc19b17b..7c1b9643a 100644 --- a/app/controllers/manager/users_controller.rb +++ b/app/controllers/manager/users_controller.rb @@ -23,7 +23,11 @@ module Manager preexisting_user.instructeur.merge(user.instructeur) end - user.expert&.update(user: preexisting_user) + if preexisting_user.expert.nil? + user.expert&.update(user: preexisting_user) + else + preexisting_user.expert.merge(user.expert) + end end redirect_to edit_manager_user_path(user) diff --git a/app/models/expert.rb b/app/models/expert.rb index 95d82cdbf..d2766a3f7 100644 --- a/app/models/expert.rb +++ b/app/models/expert.rb @@ -11,6 +11,7 @@ class Expert < ApplicationRecord has_many :experts_procedures has_many :avis, through: :experts_procedures has_many :dossiers, through: :avis + has_many :commentaires default_scope { eager_load(:user) } @@ -34,4 +35,9 @@ class Expert < ApplicationRecord @avis_summary = { unanswered: result.unanswered, total: result.total } end end + + def merge(old_expert) + old_expert.experts_procedures.update_all(expert_id: id) + old_expert.commentaires.update_all(expert_id: id) + end end diff --git a/spec/controllers/manager/users_controller_spec.rb b/spec/controllers/manager/users_controller_spec.rb index c078ebc9b..beb043ac3 100644 --- a/spec/controllers/manager/users_controller_spec.rb +++ b/spec/controllers/manager/users_controller_spec.rb @@ -97,6 +97,20 @@ describe Manager::UsersController, type: :controller do expect(preexisting_user.instructeur.bulk_messages).to match([bulk_message]) end end + + context 'and the source expert has some avis and commentaires' do + let(:dossier) { create(:dossier) } + let(:experts_procedure) { create(:experts_procedure, expert: user.expert, procedure: dossier.procedure) } + let!(:avis) { create(:avis, dossier: dossier, claimant: create(:instructeur), experts_procedure: experts_procedure) } + let!(:commentaire) { create(:commentaire, expert: expert, dossier: dossier) } + + it 'transfers the avis' do + subject + + expect(preexisting_user.expert.avis).to match([avis]) + expect(preexisting_user.expert.commentaires).to match([commentaire]) + end + end end end end