raulintosh

raulintosh

How to set tenant parameter in GET call

I’m new to developing with Ash and I want to use AshJsonApi to interact with the application. I managed to configure multitenancy and run tests from IEX, and both actions and queries work correctly.

I’m trying to invoke the same operations from the REST client, but I keep encountering the error **(Ash.Error.Invalid.TenantRequired) Queries against the Helpdesk.Support.Ticket resource require a tenant to be specified.**

I tried creating a plug to set the tenant using Ash.PlugHelpers.set_tenant and a preparation, but I still can’t make any progress.

The resource:

defmodule Helpdesk.Support.Ticket do
  use Ash.Resource,
    domain: Helpdesk.Support,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshJsonApi.Resource]

    alias Helpdesk.Support.Ticket

  json_api do
    type "ticket"
  end

  postgres do
    table "tickets"
    repo Helpdesk.Repo
  end

  actions do
    defaults [:read, :destroy]

    read :list_tickets do
      get? true
    end

    read :ticket_by_tenant do
      argument :tenant, :string, allow_nil?: false
      get? true
     end

    create :open do
      accept [:subject]
    end

    update :update do
      accept [:subject]
    end

    update :close do
      accept []
      change set_attribute(:status, :close)
    end
  end

  # Tell Ash that this Resource must belong to a tenant
  multitenancy do
    # Every tenant will ha
    strategy :context
  end

  preparations do
    prepare Helpdesk.Support.TenantPreparation
  end

  attributes do
    uuid_primary_key :id
    attribute :subject, :string, allow_nil?: false, public?: true

    attribute :status, :atom do
      constraints one_of: [:open, :close]
      default :open
      allow_nil? false
    end

    timestamps()
  end
end

The domain:

defmodule Helpdesk.Support do
  use Ash.Domain, extensions: [AshJsonApi.Domain]
  alias Helpdesk.Support.Ticket

  resources do
    resource Ticket do
      define :open_ticket, action: :open
      define :list_tickets, action: :read
      define :update_ticket, action: :update
      define :destroy_ticket, action: :destroy
      define :close_ticket, action: :close
      define :get_ticket_by_tenant, args: [:tenant], action: :ticket_by_tenant
    end
  end

  json_api do
    routes do
      # in the domain `base_route` acts like a scope
      base_route "/tickets", Helpdesk.Support.Ticket do
        get :read
        index :read
        post :open
        get :list_tickets
        get :ticket_by_tenant, route: "get_ticket_by_tenant/:tenant"
      end
    end
  end

end

Thanks

First Post!

zachdaniel

zachdaniel

Creator of Ash

Can you show how you are setting the tenant with PlugHelpers? Are you getting the tenant from a subdomain, for example?

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement