simple cases when the preexisting targeted account does not have instructeur or profile profile
This commit is contained in:
parent
c56199e8f7
commit
9a6a53349f
2 changed files with 64 additions and 17 deletions
|
@ -2,14 +2,25 @@ module Manager
|
||||||
class UsersController < Manager::ApplicationController
|
class UsersController < Manager::ApplicationController
|
||||||
def update
|
def update
|
||||||
user = User.find(params[:id])
|
user = User.find(params[:id])
|
||||||
new_email = params[:user][:email]
|
|
||||||
user.skip_reconfirmation!
|
preexisting_user = User.find_by(email: targeted_email)
|
||||||
user.update(email: new_email)
|
|
||||||
if (user.valid?)
|
if preexisting_user.nil?
|
||||||
flash[:notice] = "L'email a été modifié en « #{new_email} » sans notification ni validation par email."
|
user.skip_reconfirmation!
|
||||||
|
user.update(email: targeted_email)
|
||||||
|
|
||||||
|
if (user.valid?)
|
||||||
|
flash[:notice] = "L'email a été modifié en « #{targeted_email} » sans notification ni validation par email."
|
||||||
|
else
|
||||||
|
flash[:error] = user.errors.full_messages.to_sentence
|
||||||
|
end
|
||||||
else
|
else
|
||||||
flash[:error] = "« #{new_email} » n’est pas une adresse valide."
|
user.dossiers.update_all(user_id: preexisting_user.id)
|
||||||
|
|
||||||
|
user.instructeur&.update(user: preexisting_user)
|
||||||
|
user.expert&.update(user: preexisting_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_to edit_manager_user_path(user)
|
redirect_to edit_manager_user_path(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -72,5 +83,11 @@ module Manager
|
||||||
end
|
end
|
||||||
redirect_to emails_manager_user_path(@user)
|
redirect_to emails_manager_user_path(@user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def targeted_email
|
||||||
|
params[:user][:email]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,24 +21,54 @@ describe Manager::UsersController, type: :controller do
|
||||||
|
|
||||||
subject { patch :update, params: { id: user.id, user: { email: nouvel_email } } }
|
subject { patch :update, params: { id: user.id, user: { email: nouvel_email } } }
|
||||||
|
|
||||||
describe 'with a valid email' do
|
context 'when the targeted email does not exist' do
|
||||||
let(:nouvel_email) { 'nouvel.email@domaine.fr' }
|
describe 'with a valid email' do
|
||||||
|
let(:nouvel_email) { 'nouvel.email@domaine.fr' }
|
||||||
|
|
||||||
it 'updates the user email' do
|
it 'updates the user email' do
|
||||||
subject
|
subject
|
||||||
|
|
||||||
expect(User.find_by(id: user.id).email).to eq(nouvel_email)
|
expect(User.find_by(id: user.id).email).to eq(nouvel_email)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'with an invalid email' do
|
||||||
|
let(:nouvel_email) { 'plop' }
|
||||||
|
|
||||||
|
it 'does not update the user email' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(User.find_by(id: user.id).email).not_to eq(nouvel_email)
|
||||||
|
expect(flash[:error]).to match("Courriel invalide")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'with an invalid email' do
|
context 'when the targeted email exists' do
|
||||||
let(:nouvel_email) { 'plop' }
|
let(:preexisting_user) { create(:user, email: 'email.existant@domaine.fr') }
|
||||||
|
let(:nouvel_email) { preexisting_user.email }
|
||||||
|
|
||||||
it 'does not update the user email' do
|
context 'and the old account has a dossier' do
|
||||||
subject
|
let!(:dossier) { create(:dossier, user: user) }
|
||||||
|
|
||||||
expect(User.find_by(id: user.id).email).not_to eq(nouvel_email)
|
it 'transfers the dossier' do
|
||||||
expect(flash[:error]).to match("« #{nouvel_email} » n’est pas une adresse valide.")
|
subject
|
||||||
|
|
||||||
|
expect(preexisting_user.dossiers).to match([dossier])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'and the old account belongs to an instructeur and expert' do
|
||||||
|
let!(:instructeur) { create(:instructeur, user: user) }
|
||||||
|
let!(:expert) { create(:expert, user: user) }
|
||||||
|
|
||||||
|
it 'transfers instructeur account' do
|
||||||
|
subject
|
||||||
|
preexisting_user.reload
|
||||||
|
|
||||||
|
expect(preexisting_user.instructeur).to match(instructeur)
|
||||||
|
expect(preexisting_user.expert).to match(expert)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue