cannery/lib/cannery/mailer.ex

43 lines
1.3 KiB
Elixir
Raw Permalink Normal View History

2022-01-21 20:36:25 -05:00
defmodule Cannery.Mailer do
2022-01-22 21:40:29 -05:00
@moduledoc """
Mailer adapter for emails
2022-02-11 22:37:41 -05:00
Since emails are loaded as Oban jobs, the `:attrs` map must be serializable to
json with Jason, which restricts the use of structs.
2022-01-22 21:40:29 -05:00
"""
2022-01-21 20:36:25 -05:00
use Swoosh.Mailer, otp_app: :cannery
2022-02-11 22:37:41 -05:00
alias Cannery.{Accounts.User, EmailWorker}
2022-02-08 19:59:23 -05:00
alias Oban.Job
@doc """
Deliver instructions to confirm account.
"""
2022-02-11 22:37:41 -05:00
@spec deliver_confirmation_instructions(User.t(), String.t()) :: Job.t()
def deliver_confirmation_instructions(%User{id: user_id}, url) do
%{email: :welcome, user_id: user_id, attrs: %{url: url}}
|> EmailWorker.new()
|> Oban.insert!()
2022-02-08 19:59:23 -05:00
end
@doc """
Deliver instructions to reset a user password.
"""
2022-02-11 22:37:41 -05:00
@spec deliver_reset_password_instructions(User.t(), String.t()) :: Job.t()
def deliver_reset_password_instructions(%User{id: user_id}, url) do
%{email: :reset_password, user_id: user_id, attrs: %{url: url}}
|> EmailWorker.new()
|> Oban.insert!()
2022-02-08 19:59:23 -05:00
end
@doc """
Deliver instructions to update a user email.
"""
2022-02-11 22:37:41 -05:00
@spec deliver_update_email_instructions(User.t(), String.t()) :: Job.t()
def deliver_update_email_instructions(%User{id: user_id}, url) do
2022-02-16 22:18:05 -05:00
%{email: :update_email, user_id: user_id, attrs: %{url: url}}
|> EmailWorker.new()
|> Oban.insert!()
2022-02-08 19:59:23 -05:00
end
2022-01-21 20:36:25 -05:00
end