diff --git a/config/config.exs b/config/config.exs index a43af46..43c8cf8 100644 --- a/config/config.exs +++ b/config/config.exs @@ -49,7 +49,7 @@ config :spark, config :mv, ecto_repos: [Mv.Repo], generators: [timestamp_type: :utc_datetime], - ash_domains: [Mv.Membership] + ash_domains: [Mv.Membership, Mv.Accounts] # Configures the endpoint config :mv, MvWeb.Endpoint, diff --git a/lib/accounts/accounts.ex b/lib/accounts/accounts.ex new file mode 100644 index 0000000..1bec856 --- /dev/null +++ b/lib/accounts/accounts.ex @@ -0,0 +1,13 @@ +defmodule Mv.Accounts do + use Ash.Domain, + extensions: [AshPhoenix] + + resources do + resource Mv.Accounts.User do + define(:create_user, action: :create) + define(:list_users, action: :read) + define(:update_user, action: :update) + define(:destroy_user, action: :destroy) + end + end +end diff --git a/lib/accounts/user.ex b/lib/accounts/user.ex new file mode 100644 index 0000000..0481b84 --- /dev/null +++ b/lib/accounts/user.ex @@ -0,0 +1,26 @@ +defmodule Mv.Accounts.User do + use Ash.Resource, + domain: Mv.Accounts, + data_layer: AshPostgres.DataLayer + + postgres do + table("users") + repo(Mv.Repo) + end + + attributes do + uuid_primary_key(:id) + + attribute(:email, :string, allow_nil?: true, public?: true) + attribute(:password_hash, :string, sensitive?: true) + attribute(:oicd_id, :string) + end + + actions do + defaults([:read, :destroy, :create, :update]) + end + + relationships do + belongs_to(:member, Mv.Membership.Member) + end +end diff --git a/priv/repo/migrations/20250530101732_account_migration.exs b/priv/repo/migrations/20250530101732_account_migration.exs new file mode 100644 index 0000000..7d0f832 --- /dev/null +++ b/priv/repo/migrations/20250530101732_account_migration.exs @@ -0,0 +1,32 @@ +defmodule Mv.Repo.Migrations.AccountMigration do + @moduledoc """ + Updates resources based on their most recent snapshots. + + This file was autogenerated with `mix ash_postgres.generate_migrations` + """ + + use Ecto.Migration + + def up do + create table(:users, primary_key: false) do + add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true + add :email, :text + add :password_hash, :text + add :oicd_id, :text + + add :member_id, + references(:members, + column: :id, + name: "users_member_id_fkey", + type: :uuid, + prefix: "public" + ) + end + end + + def down do + drop constraint(:users, "users_member_id_fkey") + + drop table(:users) + end +end diff --git a/priv/resource_snapshots/repo/users/20250530101733.json b/priv/resource_snapshots/repo/users/20250530101733.json new file mode 100644 index 0000000..d34cb68 --- /dev/null +++ b/priv/resource_snapshots/repo/users/20250530101733.json @@ -0,0 +1,88 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"gen_random_uuid()\")", + "generated?": false, + "primary_key?": true, + "references": null, + "size": null, + "source": "id", + "type": "uuid" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "email", + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "password_hash", + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "oicd_id", + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": { + "deferrable": false, + "destination_attribute": "id", + "destination_attribute_default": null, + "destination_attribute_generated": null, + "index?": false, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "users_member_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "members" + }, + "size": null, + "source": "member_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "1CA33A4C9EBDC29717F4B6ADB27E76B42B5BEA7085E47BFBDD9D84E592D44649", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.Mv.Repo", + "schema": null, + "table": "users" +} \ No newline at end of file