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
Creator of Ash
Can you show how you are setting the tenant with PlugHelpers? Are you getting the tenant from a subdomain, for example?
Popular in Questions
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
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
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
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Other popular topics
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
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
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
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
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
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








