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

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement