diff --git a/app/controllers/users/profil_controller.rb b/app/controllers/users/profil_controller.rb index 47558351f..27db94721 100644 --- a/app/controllers/users/profil_controller.rb +++ b/app/controllers/users/profil_controller.rb @@ -3,6 +3,7 @@ module Users before_action :ensure_update_email_is_authorized, only: :update_email def show + @waiting_merge_emails = waiting_merge_emails @waiting_transfers = current_user.dossiers.joins(:transfer).group('dossier_transfers.email').count.to_a end @@ -32,8 +33,32 @@ module Users redirect_to profil_path end + def accept_merge + users_requesting_merge.each { |user| current_user.merge(user) } + users_requesting_merge.update_all(requested_merge_into_id: nil) + + flash.notice = "Vous avez absorbé le compte #{waiting_merge_emails.join(', ')}" + redirect_to profil_path + end + + def refuse_merge + users = users_requesting_merge + users.update_all(requested_merge_into_id: nil) + + flash.notice = 'La fusion a été refusé' + redirect_to profil_path + end + private + def waiting_merge_emails + users_requesting_merge.pluck(:email) + end + + def users_requesting_merge + @requesting_merge ||= User.where(requested_merge_into: current_user) + end + def ensure_update_email_is_authorized if current_user.instructeur? && !target_email_allowed? flash.alert = t('users.profil.ensure_update_email_is_authorized.email_not_allowed', contact_email: CONTACT_EMAIL, requested_email: requested_email) diff --git a/app/views/users/profil/show.html.haml b/app/views/users/profil/show.html.haml index 46f60a875..d0021a281 100644 --- a/app/views/users/profil/show.html.haml +++ b/app/views/users/profil/show.html.haml @@ -5,6 +5,16 @@ #profil-page.container %h1 Profil + - if @waiting_merge_emails.any? + .card + .card-title Demande de fusion de comptes + %p + Acceptez-vous d’absorber le compte de + %span.email-address= @waiting_merge_emails.join(', ') + + = link_to 'Refuser la fusion', refuse_merge_path, method: :post, class: 'button', data: { confirm: "Confirmez-vous le refus ?" } + = link_to 'Accepter la fusion', accept_merge_path, method: :post, class: 'button', data: { confirm: "Confirmez-vous la fusion des comptes ?" } + .card .card-title Coordonnées %p diff --git a/config/routes.rb b/config/routes.rb index 249faa38c..45cd42836 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -280,6 +280,8 @@ Rails.application.routes.draw do get 'renew-api-token' => redirect('/profil') patch 'update_email' => 'profil#update_email' post 'transfer_all_dossiers' => 'profil#transfer_all_dossiers' + post 'accept_merge' => 'profil#accept_merge' + post 'refuse_merge' => 'profil#refuse_merge' end # diff --git a/spec/controllers/users/profil_controller_spec.rb b/spec/controllers/users/profil_controller_spec.rb index bb8cf8fcc..19e8f96bc 100644 --- a/spec/controllers/users/profil_controller_spec.rb +++ b/spec/controllers/users/profil_controller_spec.rb @@ -126,4 +126,38 @@ describe Users::ProfilController, type: :controller do expect(flash.notice).to eq("Le transfert de 3 dossiers à #{next_owner} est en cours") end end + + context 'POST #accept_merge' do + let!(:requesting_user) { create(:user, requested_merge_into: user) } + + subject { post :accept_merge } + + it 'merges the account' do + expect_any_instance_of(User).to receive(:merge) + + subject + requesting_user.reload + + expect(requesting_user.requested_merge_into).to be_nil + expect(flash.notice).to include('Vous avez absorbé') + expect(response).to redirect_to(profil_path) + end + end + + context 'POST #refuse_merge' do + let!(:requesting_user) { create(:user, requested_merge_into: user) } + + subject { post :refuse_merge } + + it 'merges the account' do + expect_any_instance_of(User).not_to receive(:merge) + + subject + requesting_user.reload + + expect(requesting_user.requested_merge_into).to be_nil + expect(flash.notice).to include('La fusion a été refusé') + expect(response).to redirect_to(profil_path) + end + end end