-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
countable_actions.ex
34 lines (31 loc) · 983 Bytes
/
countable_actions.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
defmodule Ash.Resource.Transformers.CountableActions do
@moduledoc """
Ensures that countable paginated actions do not exist for resources that are not countable
"""
use Ash.Dsl.Transformer
alias Ash.Dsl.Transformer
# sobelow_skip ["DOS.BinToAtom"]
def transform(resource, dsl_state) do
dsl_state
|> Transformer.get_entities([:actions])
|> Enum.filter(&(&1.type == :read))
|> Enum.filter(& &1.pagination)
|> Enum.filter(& &1.pagination.countable)
|> case do
[] ->
{:ok, dsl_state}
[action | _] ->
if Ash.DataLayer.data_layer_can?(resource, {:query_aggregate, :count}) do
{:ok, dsl_state}
else
{:error,
Ash.Error.Dsl.DslError.exception(
module: __MODULE__,
path: [:actions, action.name],
message:
"Action cannot be countable, as the datalayer does not support counting queries"
)}
end
end
end
end