Ash FrameworkAF
Ash Frameworkβ€’3mo agoβ€’
7 replies
Shahryar

Problem in using manage_relationship and Ash events with tenant

Hi, this is my update action, that i use for normal user which has tenant and site_id
update :update do
  primary? true
  require_atomic? false
  transaction? true
  accept @admin_fields |> List.delete(:site_id)

  argument :tag_ids, {:array, :uuid} do
    description "List of tag IDs to assign to this section"
  end

  change slugify(:slug, into: :slug, ignore: [".", "!", "?"])

  change manage_relationship(:tag_ids, :tags,
            type: :append_and_remove,
            on_no_match: :error,
            on_match: :ignore
          )

  # Add metadata for event tracking
  change fn changeset, context ->
    Ash.Changeset.put_context(changeset, :ash_events_metadata, %{
      site_id: context.tenant || changeset.data.site_id,
      title: Ash.Changeset.get_attribute(changeset, :title) || changeset.data.title
    })
  end

  description "Updates section for tenant users - requires tenant context, prevents site_id changes"
end

so when i want to do for the user which is master (and no site_id) i do like this
action :master_update, :struct do
  description "Updates section for master users - requires nil tenant context, prevents site_id changes"
  constraints instance_of: MishkaDocument.Section

  argument :id, :uuid, allow_nil?: false
  arguments ......
  argument :tag_ids, {:array, :uuid}

  run fn input, _context ->
    id = input.arguments.id

    case Ash.get(MishkaDocument.Section, id, action: :master_get, authorize?: false) do
      {:ok, record} when not is_nil(record) ->
        tenant = record.site_id

        record
        |> Ash.Changeset.for_update(:update, input.arguments, tenant: tenant, authorize?: false)
        |> Ash.update()

      _ ->
        {:error, Ash.Error.Query.NotFound.exception(resource: MishkaDocument.Section)}
    end
  end
end

as you see i call the normal update action with
tenant
and it works. but when i use the ash events and add update action inside the only_actions it returns this warning πŸ‘‡πŸ‘‡πŸ‘‡
Solution
Please do!
Was this page helpful?