2019-11-21 15:51:13 +01:00
|
|
|
defmodule Mix.Tasks.Mobilizon.Users.Delete do
|
|
|
|
@moduledoc """
|
|
|
|
Task to delete a user
|
|
|
|
"""
|
|
|
|
use Mix.Task
|
|
|
|
alias Mobilizon.Users
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
@shortdoc "Deletes a Mobilizon user"
|
|
|
|
|
|
|
|
@impl Mix.Task
|
|
|
|
def run([email | rest]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
2020-06-11 19:13:21 +02:00
|
|
|
assume_yes: :boolean,
|
|
|
|
force: :boolean
|
2019-11-21 15:51:13 +01:00
|
|
|
],
|
|
|
|
aliases: [
|
2020-06-11 19:13:21 +02:00
|
|
|
y: :assume_yes,
|
|
|
|
f: :force
|
2019-11-21 15:51:13 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assume_yes? = Keyword.get(options, :assume_yes, false)
|
2020-06-11 19:13:21 +02:00
|
|
|
force? = Keyword.get(options, :force, false)
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
Mix.Task.run("app.start")
|
|
|
|
|
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(email),
|
|
|
|
true <- assume_yes? or Mix.shell().yes?("Continue with deleting user #{user.email}?"),
|
|
|
|
{:ok, %User{} = user} <-
|
2020-06-11 19:13:21 +02:00
|
|
|
Users.delete_user(user, reserve_email: !force?) do
|
2019-11-21 15:51:13 +01:00
|
|
|
Mix.shell().info("""
|
|
|
|
The user #{user.email} has been deleted
|
|
|
|
""")
|
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
|
|
|
Mix.raise("Error: No such user")
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Mix.raise("User has not been deleted.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(_) do
|
|
|
|
Mix.raise("mobilizon.users.delete requires an email as argument")
|
|
|
|
end
|
|
|
|
end
|