Ash FrameworkAF
Ash Framework8mo ago
23 replies
Vahagn

Add value to manage_relationship input from current resource

I have this resource
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

I am creating the :accept for invitation, which should create a Member where the role will be the same as the Invitation's role, something like arg or actor but an attr (it does not exist, I just invented it to show what I am looking for

(PS. relation function is just making a manage_relationship with passed arguments)
Was this page helpful?