defmodule Aldente.Management.Invitation do
use Ash.Resource,
domain: Aldente.Management,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource]
import Aldente.Changes
alias Aldente.{Management, Accounts}
graphql do
type :invitation
end
postgres do
table "invitations"
repo Aldente.Repo
end
actions do
defaults [:read, :destroy]
create :create do
primary? true
accept [:role]
end
update :accept do
argument :code, :uuid, allow_nil?: false
validate attribute_equals(:code, arg(:code))
validate attributes_absent(:member_id)
# TODO: make a new Member with `role` and `entity_id`, for current actor
# change relation(:member, %{user_id: actor(:id), role: attr(:role)}, type: :create)
end
end
multitenancy do
strategy :attribute
attribute :entity_id
end
attributes do
uuid_primary_key :id
attribute :code, :uuid, generated?: true, allow_nil?: false
attribute :role, Management.Role, allow_nil?: false
end
relationships do
belongs_to :entity, Management.Entity, allow_nil?: false, public?: true
belongs_to :member, Management.Member, public?: true
end
defp generate_code do
Ash.UUID.generate()
end
end
defmodule Aldente.Management.Invitation do
use Ash.Resource,
domain: Aldente.Management,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource]
import Aldente.Changes
alias Aldente.{Management, Accounts}
graphql do
type :invitation
end
postgres do
table "invitations"
repo Aldente.Repo
end
actions do
defaults [:read, :destroy]
create :create do
primary? true
accept [:role]
end
update :accept do
argument :code, :uuid, allow_nil?: false
validate attribute_equals(:code, arg(:code))
validate attributes_absent(:member_id)
# TODO: make a new Member with `role` and `entity_id`, for current actor
# change relation(:member, %{user_id: actor(:id), role: attr(:role)}, type: :create)
end
end
multitenancy do
strategy :attribute
attribute :entity_id
end
attributes do
uuid_primary_key :id
attribute :code, :uuid, generated?: true, allow_nil?: false
attribute :role, Management.Role, allow_nil?: false
end
relationships do
belongs_to :entity, Management.Entity, allow_nil?: false, public?: true
belongs_to :member, Management.Member, public?: true
end
defp generate_code do
Ash.UUID.generate()
end
end