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
|
2020-10-30 15:16:01 +01:00
|
|
|
import Mix.Tasks.Mobilizon.Common
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
@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,
|
2020-10-13 14:36:55 +02:00
|
|
|
keep_email: :boolean
|
2019-11-21 15:51:13 +01:00
|
|
|
],
|
|
|
|
aliases: [
|
2020-06-11 19:13:21 +02:00
|
|
|
y: :assume_yes,
|
2020-10-13 15:53:18 +02:00
|
|
|
k: :keep_email
|
2019-11-21 15:51:13 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assume_yes? = Keyword.get(options, :assume_yes, false)
|
2020-10-13 14:36:55 +02:00
|
|
|
keep_email? = Keyword.get(options, :keep_email, false)
|
2019-11-21 15:51:13 +01:00
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
start_mobilizon()
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(email),
|
2020-10-30 15:16:01 +01:00
|
|
|
true <- assume_yes? or shell_yes?("Continue with deleting user #{user.email}?"),
|
2019-11-21 15:51:13 +01:00
|
|
|
{:ok, %User{} = user} <-
|
2020-10-13 14:36:55 +02:00
|
|
|
Users.delete_user(user, reserve_email: keep_email?) do
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_info("""
|
2019-11-21 15:51:13 +01:00
|
|
|
The user #{user.email} has been deleted
|
|
|
|
""")
|
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error("Error: No such user")
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
_ ->
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error("User has not been deleted.")
|
2019-11-21 15:51:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(_) do
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error("mobilizon.users.delete requires an email as argument")
|
2019-11-21 15:51:13 +01:00
|
|
|
end
|
|
|
|
end
|