fix(experts_procedures#create): ensure to reload expert list after addition

This commit is contained in:
mfo 2024-07-01 10:41:16 +02:00
parent ced634295e
commit e6a3f210d4
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
5 changed files with 22 additions and 18 deletions

View file

@ -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

View file

@ -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'}

View file

@ -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

View file

@ -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|

View file

@ -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