move merge method to user

This commit is contained in:
simon lehericey 2021-10-22 15:17:25 +02:00
parent 3d524e9488
commit c725999582
4 changed files with 67 additions and 48 deletions

View file

@ -14,19 +14,7 @@ module Manager
flash[:error] = user.errors.full_messages.to_sentence
end
else
user.dossiers.update_all(user_id: targeted_user.id)
[
[user.instructeur, targeted_user.instructeur],
[user.expert, targeted_user.expert],
[user.administrateur, targeted_user.administrateur]
].each do |old_role, targeted_role|
if targeted_role.nil?
old_role&.update(user: targeted_user)
else
targeted_role.merge(old_role)
end
end
targeted_user.merge(user)
flash[:notice] = "Le compte « #{targeted_email} » a absorbé le compte « #{user.email} »."
end

View file

@ -194,6 +194,22 @@ class User < ApplicationRecord
end
end
def merge(old_user)
old_user.dossiers.update_all(user_id: id)
[
[old_user.instructeur, instructeur],
[old_user.expert, expert],
[old_user.administrateur, administrateur]
].each do |old_role, targeted_role|
if targeted_role.nil?
old_role&.update(user: self)
else
targeted_role.merge(old_role)
end
end
end
private
def link_invites!

View file

@ -48,43 +48,12 @@ describe Manager::UsersController, type: :controller do
let(:targeted_user) { create(:user, email: 'email.existant@domaine.fr', password: '{My-$3cure-p4ssWord}') }
let(:nouvel_email) { targeted_user.email }
context 'and the old account has a dossier' do
let!(:dossier) { create(:dossier, user: user) }
it 'launches the merge process' do
expect_any_instance_of(User).to receive(:merge).with(user)
it 'transfers the dossier' do
subject
subject
expect(targeted_user.dossiers).to match([dossier])
end
end
context 'and the old account belongs to an instructeur, expert and administrateur' do
let!(:instructeur) { create(:instructeur, user: user) }
let!(:expert) { create(:expert, user: user) }
let!(:administrateur) { create(:administrateur, user: user, instructeur: instructeur) }
it 'transfers instructeur account' do
subject
targeted_user.reload
expect(targeted_user.instructeur).to match(instructeur)
expect(targeted_user.expert).to match(expert)
expect(targeted_user.administrateur).to match(administrateur)
expect(flash[:notice]).to match("Le compte « email.existant@domaine.fr » a absorbé le compte « ancien.email@domaine.fr ».")
end
context 'and the targeted account owns an instructeur and expert as well' do
let!(:targeted_instructeur) { create(:instructeur, user: targeted_user) }
let!(:targeted_expert) { create(:expert, user: targeted_user) }
let!(:targeted_administrateur) { create(:administrateur, user: targeted_user) }
it 'merge the account' do
expect_any_instance_of(Instructeur).to receive(:merge)
expect_any_instance_of(Expert).to receive(:merge)
expect_any_instance_of(Administrateur).to receive(:merge)
subject
end
end
expect(flash[:notice]).to match("Le compte « email.existant@domaine.fr » a absorbé le compte « ancien.email@domaine.fr ».")
end
end
end

View file

@ -417,4 +417,50 @@ describe User, type: :model do
end
end
end
describe '#merge' do
let(:old_user) { create(:user) }
let(:targeted_user) { create(:user) }
subject { targeted_user.merge(old_user) }
context 'and the old account has a dossier' do
let!(:dossier) { create(:dossier, user: old_user) }
it 'transfers the dossier' do
subject
expect(targeted_user.dossiers).to match([dossier])
end
end
context 'and the old account belongs to an instructeur, expert and administrateur' do
let!(:expert) { create(:expert, user: old_user) }
let!(:administrateur) { create(:administrateur, user: old_user) }
let!(:instructeur) { old_user.instructeur }
it 'transfers instructeur account' do
subject
targeted_user.reload
expect(targeted_user.instructeur).to match(instructeur)
expect(targeted_user.expert).to match(expert)
expect(targeted_user.administrateur).to match(administrateur)
end
context 'and the targeted account owns an instructeur and expert as well' do
let!(:targeted_administrateur) { create(:administrateur, user: targeted_user) }
let!(:targeted_instructeur) { targeted_user.instructeur }
let!(:targeted_expert) { create(:expert, user: targeted_user) }
it 'merge the account' do
expect(targeted_instructeur).to receive(:merge).with(instructeur)
expect(targeted_expert).to receive(:merge).with(expert)
expect(targeted_administrateur).to receive(:merge).with(administrateur)
subject
end
end
end
end
end