2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors.Service.Activation do
|
2018-07-04 14:29:17 +02:00
|
|
|
@moduledoc false
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.{Mailer, Repo, Actors.User, Actors}
|
|
|
|
alias Mobilizon.Email.User, as: UserEmail
|
2018-07-04 14:29:17 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def check_confirmation_token(token) when is_binary(token) do
|
2018-07-10 10:00:07 +02:00
|
|
|
with %User{} = user <- Repo.get_by(User, confirmation_token: token),
|
2018-07-27 10:45:35 +02:00
|
|
|
{:ok, %User{} = user} <-
|
|
|
|
Actors.update_user(user, %{
|
|
|
|
"confirmed_at" => DateTime.utc_now(),
|
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
}) do
|
2018-07-10 10:00:07 +02:00
|
|
|
{:ok, Repo.preload(user, :actors)}
|
2018-07-04 14:29:17 +02:00
|
|
|
else
|
|
|
|
_err ->
|
|
|
|
{:error, "Invalid token"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def resend_confirmation_email(%User{} = user, locale \\ "en") do
|
|
|
|
{:ok, user} = Actors.update_user(user, %{"confirmation_sent_at" => DateTime.utc_now()})
|
|
|
|
send_confirmation_email(user, locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_confirmation_email(%User{} = user, locale \\ "en") do
|
|
|
|
user
|
|
|
|
|> UserEmail.confirmation_email(locale)
|
|
|
|
|> Mailer.deliver_later()
|
|
|
|
end
|
|
|
|
end
|