Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
fix: refactor TokenTag#sent assignment outside mailer (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Apr 26, 2023
1 parent dd14c4e commit 279ce93
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
5 changes: 5 additions & 0 deletions app/interactors/create_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,10 @@ def issue_receipt
def deliver
token.reload
SessionsMailer.magic_link_email(identifier, token.class.name, token.id, token.to_hash, scope:).deliver_later

return unless token.is_a?(Token)

token.tags << TokenTag.sent
token.save!
end
end
10 changes: 0 additions & 10 deletions app/mailers/sessions_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,9 @@ def magic_link_email(to, voter_type, voter_id, voter_hash, scope: :consultation)
@voter_hash = voter_hash
raise Errors::InvalidEmail unless EmailValidator.valid?(to, mode: :strict)

tag = TokenTag.sent
mail(to:, template_name:, subject:) do |format|
format.text
end
rescue StandardError => e
tag = TokenTag.failed

raise e
ensure
return unless voter_type == 'Token'

tag.token_id = voter_id
tag.save!
end

private
Expand Down
1 change: 1 addition & 0 deletions test/interactors/create_token_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CreateTokenTest < ActionDispatch::IntegrationTest

assert_enqueued_emails 1
assert_equal 1, Token.count
assert_equal %w[sent], Token.first.tags.map(&:value)
assert_equal 1, TokenReceipt.count
end
end
1 change: 0 additions & 1 deletion test/mailers/sessions_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ class SessionsMailerTest < ActionMailer::TestCase
assert_equal [to], email.to
assert_includes email.body, "http://example.com/sessions/#{token.to_hash}/login"
assert_includes email.body, "Thanks,\n#{Rails.configuration.x.asembleo.title}"
assert_equal ['sent'], token.reload.tags.map(&:value)
end
end

0 comments on commit 279ce93

Please sign in to comment.