summaryrefslogtreecommitdiff
path: root/lib/mix/tasks
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-03-23 17:11:39 +0100
committerDawid Rycerz <dawid@rycerz.xyz>2025-04-05 21:16:51 +0200
commit0ab2e5ba2b0631b28b5b1405559237b3913c878f (patch)
tree791cea788b0a62bc483d0041fbd0c655d2ad49e8 /lib/mix/tasks
feat: initialize Phoenix application for weather alerts
This commit sets up the initial Silmataivas project structure, including: Phoenix web framework configuration, database models for users and locations, weather polling service, notification system, Docker and deployment configurations, CI/CD pipeline setup
Diffstat (limited to 'lib/mix/tasks')
-rw-r--r--lib/mix/tasks/silmataivas.user.new.ex48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/mix/tasks/silmataivas.user.new.ex b/lib/mix/tasks/silmataivas.user.new.ex
new file mode 100644
index 0000000..fe10c7f
--- /dev/null
+++ b/lib/mix/tasks/silmataivas.user.new.ex
@@ -0,0 +1,48 @@
+defmodule Mix.Tasks.Silmataivas.User.New do
+ use Mix.Task
+
+ @shortdoc "Creates a new user and prints its API token."
+
+ @moduledoc """
+ Creates a new user.
+
+ mix silmataivas.user.new
+ mix silmataivas.user.new <user_id>
+ mix silmataivas.user.new <user_id> <role>
+
+ This task starts the application and creates a user using the Silmataivas.Users context.
+
+ ## Options
+ * `<user_id>` - An optional user ID to use. If not provided, a UUID will be generated.
+ * `<role>` - An optional role, must be either "user" or "admin". Defaults to "user".
+ """
+
+ def run(args) do
+ Mix.Task.run("app.start", [])
+
+ {user_id, role} =
+ case args do
+ [provided_id, provided_role | _] -> {provided_id, provided_role}
+ [provided_id | _] -> {provided_id, "user"}
+ [] -> {Ecto.UUID.generate(), "user"}
+ end
+
+ # Validate role
+ unless role in ["user", "admin"] do
+ Mix.raise("Invalid role: #{role}. Role must be either \"user\" or \"admin\".")
+ end
+
+ user_params = %{user_id: user_id, role: role}
+
+ case Silmataivas.Users.create_user(user_params) do
+ {:ok, user} ->
+ IO.puts("\n✅ User created successfully!")
+ IO.puts(" User ID (API token): #{user.user_id}")
+ IO.puts(" Role: #{user.role}")
+
+ {:error, changeset} ->
+ IO.puts("\n❌ Failed to create user:")
+ IO.inspect(changeset.errors)
+ end
+ end
+end