From ea918d9c5aa7fe530a8dc0c29f3779972e827079 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Thu, 18 Aug 2022 13:12:43 +0530 Subject: [PATCH] fix: send confirmation email on re-adding agent (#5279) Send confirmation email after re-adding the agent. Fixes: #4951 --- .../api/v1/accounts/agents_controller.rb | 2 +- .../mailer/confirmation_instructions.html.erb | 8 +++++++- spec/mailers/confirmation_instructions_spec.rb | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/accounts/agents_controller.rb b/app/controllers/api/v1/accounts/agents_controller.rb index 6b2f9ea75..eb409b9ff 100644 --- a/app/controllers/api/v1/accounts/agents_controller.rb +++ b/app/controllers/api/v1/accounts/agents_controller.rb @@ -39,7 +39,7 @@ class Api::V1::Accounts::AgentsController < Api::V1::Accounts::BaseController # TODO: move this to a builder and combine the save account user method into a builder # ensure the account user association is also created in a single transaction def create_user - return if @user + return @user.send_confirmation_instructions if @user @user = User.create!(new_agent_params.slice(:email, :name, :password, :password_confirmation)) end diff --git a/app/views/devise/mailer/confirmation_instructions.html.erb b/app/views/devise/mailer/confirmation_instructions.html.erb index eb92e5a17..32ea3fe91 100644 --- a/app/views/devise/mailer/confirmation_instructions.html.erb +++ b/app/views/devise/mailer/confirmation_instructions.html.erb @@ -5,9 +5,15 @@

<%= account_user.inviter.name %>, with <%= account_user.account.name %>, has invited you to try out <%= global_config['BRAND_NAME'] || 'Chatwoot' %>!

<% end %> +<% if @resource.confirmed? %> +

You can login to your account through the link below:

+<% else %>

You can confirm your account email through the link below:

+<% end %> -<% if account_user&.inviter.present? && @resource.unconfirmed_email.blank? %> +<% if @resource.confirmed? %> +

<%= link_to 'Login to my account', frontend_url('auth/sign_in') %>

+<% elsif account_user&.inviter.present? && @resource.unconfirmed_email.blank? %>

<%= link_to 'Confirm my account', frontend_url('auth/password/edit', reset_password_token: @resource.send(:set_reset_password_token)) %>

<% else %>

<%= link_to 'Confirm my account', frontend_url('auth/confirmation', confirmation_token: @token) %>

diff --git a/spec/mailers/confirmation_instructions_spec.rb b/spec/mailers/confirmation_instructions_spec.rb index aa75627bb..ebf9faf4b 100644 --- a/spec/mailers/confirmation_instructions_spec.rb +++ b/spec/mailers/confirmation_instructions_spec.rb @@ -11,6 +11,7 @@ RSpec.describe 'Confirmation Instructions', type: :mailer do before do # to verify the token in email + confirmable_user.update!(confirmed_at: nil) confirmable_user.send(:generate_confirmation_token) end @@ -61,5 +62,17 @@ RSpec.describe 'Confirmation Instructions', type: :mailer do expect(confirmable_user.unconfirmed_email.blank?).to be false end end + + context 'when user already confirmed' do + before do + confirmable_user.confirm + confirmable_user.account_users.last.destroy! + end + + it 'send instructions with the link to login' do + confirmation_mail = Devise::Mailer.confirmation_instructions(confirmable_user.reload, nil, {}) + expect(confirmation_mail.body).to include('/auth/sign_in') + end + end end end