diff --git a/app/components/procedure/invitation_with_typo_component.rb b/app/components/procedure/invitation_with_typo_component.rb index 72b581514..efd6cd15a 100644 --- a/app/components/procedure/invitation_with_typo_component.rb +++ b/app/components/procedure/invitation_with_typo_component.rb @@ -1,11 +1,11 @@ class Procedure::InvitationWithTypoComponent < ApplicationComponent - def initialize(maybe_typo:, url:, title:) - @maybe_typo = maybe_typo + def initialize(maybe_typos:, url:, title:) + @maybe_typos = maybe_typos @url = url @title = title end def render? - @maybe_typo.present? + @maybe_typos.present? end end diff --git a/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml b/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml index 8f63cb028..9428e20e1 100644 --- a/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml +++ b/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml @@ -2,9 +2,9 @@ - c.with_body do %p= @title %ul - - @maybe_typo.each do |(actual_email, suggested_email)| + - @maybe_typos.each do |(actual_email, suggested_email)| %li = "Je confirme " - = button_to "#{actual_email}", @url, method: :POST, params: { maybe_typo: actual_email }, class: 'fr-btn fr-btn--tertiary fr-btn--sm', form: {class: 'inline'} + = button_to "#{actual_email}", @url, method: :POST, params: { final_email: actual_email }, class: 'fr-btn fr-btn--tertiary fr-btn--sm', form: {class: 'inline'} = " ou " - = button_to "#{suggested_email}", @url, method: :POST, params: { maybe_typo: suggested_email }, class: 'fr-btn fr-btn--tertiary fr-btn--sm', form: {class: 'inline'} + = button_to "#{suggested_email}", @url, method: :POST, params: { final_email: suggested_email }, class: 'fr-btn fr-btn--tertiary fr-btn--sm', form: {class: 'inline'} diff --git a/app/controllers/administrateurs/experts_procedures_controller.rb b/app/controllers/administrateurs/experts_procedures_controller.rb index ce3f92cc9..2580bccea 100644 --- a/app/controllers/administrateurs/experts_procedures_controller.rb +++ b/app/controllers/administrateurs/experts_procedures_controller.rb @@ -2,8 +2,8 @@ module Administrateurs class ExpertsProceduresController < AdministrateurController include EmailSanitizableConcern before_action :retrieve_procedure - before_action :retrieve_experts_procedure, only: [:index, :create] - before_action :retrieve_experts_emails, only: [:index, :create] + before_action :retrieve_experts_procedure, only: [:index] + before_action :retrieve_experts_emails, only: [:index] def index end @@ -11,15 +11,15 @@ module Administrateurs def create emails = params['emails'].presence || [].to_json emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) } - @maybe_typo, emails = emails + @maybe_typos, emails = emails .map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] } .partition { _1[1].present? } - errors = if !@maybe_typo.empty? - ["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typo.map(&:first).join(', ')}"] + errors = if !@maybe_typos.empty? + ["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typos.map(&:first).join(', ')}"] else [] end - emails += [EmailSanitizer.sanitize(params['maybe_typo'])] if params['maybe_typo'].present? + emails += [EmailSanitizer.sanitize(params['final_email'])] if params['final_email'].present? valid_users, invalid_users = emails .map { |email| User.create_or_promote_to_expert(email, SecureRandom.hex) } @@ -46,6 +46,8 @@ module Administrateurs end flash.now[:alert] = errors.join(". ") if !errors.empty? + retrieve_experts_procedure + retrieve_experts_emails render :index end diff --git a/app/views/administrateurs/experts_procedures/index.html.haml b/app/views/administrateurs/experts_procedures/index.html.haml index 2ca4c4af4..bffd9f792 100644 --- a/app/views/administrateurs/experts_procedures/index.html.haml +++ b/app/views/administrateurs/experts_procedures/index.html.haml @@ -58,7 +58,7 @@ - if @procedure.experts_require_administrateur_invitation? .card - = render Procedure::InvitationWithTypoComponent.new(maybe_typo: @maybe_typo, url: admin_procedure_experts_path(@procedure), title: "Avant d'ajouter l'email à la liste d'expert prédéfinie, veuillez confirmer" ) + = render Procedure::InvitationWithTypoComponent.new(maybe_typos: @maybe_typos, url: admin_procedure_experts_path(@procedure), title: "Avant d'ajouter l'email à la liste d'expert prédéfinie, veuillez confirmer" ) = form_for :experts_procedure, url: admin_procedure_experts_path(@procedure), html: { class: 'form' } do |f| diff --git a/spec/controllers/administrateurs/experts_procedures_controller_spec.rb b/spec/controllers/administrateurs/experts_procedures_controller_spec.rb index 6894ef0ae..92f84d7e6 100644 --- a/spec/controllers/administrateurs/experts_procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/experts_procedures_controller_spec.rb @@ -33,7 +33,7 @@ describe Administrateurs::ExpertsProceduresController, type: :controller do expect(procedure.experts.include?(expert)).to be_truthy expect(procedure.experts.include?(expert2)).to be_truthy expect(flash.notice).to be_present - expect(assigns(:maybe_typo)).to eq([]) + expect(assigns(:maybe_typos)).to eq([]) expect(response).to have_http_status(:success) end end @@ -43,22 +43,24 @@ describe Administrateurs::ExpertsProceduresController, type: :controller do render_views it 'warns' do expect(flash.alert).to be_present - expect(assigns(:maybe_typo)).to eq([['martin@oraneg.fr', 'martin@orange.fr']]) + expect(assigns(:maybe_typos)).to eq([['martin@oraneg.fr', 'martin@orange.fr']]) expect(response).to have_http_status(:success) end end context 'when forcing email with typos' do - let(:maybe_typo) { 'martin@oraneg.fr' } - let(:params) { { procedure_id: procedure.id, maybe_typo: } } + render_views + let(:final_email) { 'martin@oraneg.fr' } + let(:params) { { procedure_id: procedure.id, final_email: } } it 'works' do - created_user = User.where(email: maybe_typo).first + created_user = User.where(email: final_email).first expect(created_user).to be_an_instance_of(User) expect(created_user.expert).to be_an_instance_of(Expert) expect(procedure.experts.include?(created_user.expert)).to be_truthy expect(flash.notice).to be_present expect(response).to have_http_status(:success) + expect(response.body).to have_content(final_email) end end end