2023-11-20 17:48:38 +13:00
|
|
|
defmodule AshPostgres.Test.ComplexCalculations.DMChannel do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:52:28 +13:00
|
|
|
domain: AshPostgres.Test.ComplexCalculations.Domain,
|
2023-11-20 17:48:38 +13:00
|
|
|
data_layer: AshPostgres.DataLayer,
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
require Ash.Expr
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:52:28 +13:00
|
|
|
default_accept(:*)
|
|
|
|
|
2023-11-20 17:48:38 +13:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
|
|
|
|
2024-03-28 09:52:28 +13:00
|
|
|
create_timestamp(:created_at, public?: true)
|
|
|
|
update_timestamp(:updated_at, public?: true)
|
2023-11-20 17:48:38 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
postgres do
|
|
|
|
table "complex_calculations_channels"
|
|
|
|
repo(AshPostgres.TestRepo)
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
has_many :channel_members, AshPostgres.Test.ComplexCalculations.ChannelMember do
|
2024-03-28 09:52:28 +13:00
|
|
|
public?(true)
|
2023-11-20 17:48:38 +13:00
|
|
|
destination_attribute(:channel_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
has_one :first_member, AshPostgres.Test.ComplexCalculations.ChannelMember do
|
2024-03-28 09:52:28 +13:00
|
|
|
public?(true)
|
2023-11-20 17:48:38 +13:00
|
|
|
destination_attribute(:channel_id)
|
|
|
|
from_many?(true)
|
|
|
|
sort(created_at: :asc)
|
|
|
|
end
|
|
|
|
|
|
|
|
has_one :second_member, AshPostgres.Test.ComplexCalculations.ChannelMember do
|
2024-03-28 09:52:28 +13:00
|
|
|
public?(true)
|
2023-11-20 17:48:38 +13:00
|
|
|
destination_attribute(:channel_id)
|
|
|
|
from_many?(true)
|
|
|
|
sort(created_at: :desc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregates do
|
|
|
|
first(:first_member_name, [:first_member, :user], :name)
|
|
|
|
first(:second_member_name, [:second_member, :user], :name)
|
|
|
|
end
|
|
|
|
|
|
|
|
calculations do
|
2023-12-20 12:14:12 +13:00
|
|
|
calculate(:foobar, :string, expr("foobar"))
|
|
|
|
|
2023-11-20 17:48:38 +13:00
|
|
|
calculate :name, :string do
|
|
|
|
calculation(
|
|
|
|
expr(
|
|
|
|
cond do
|
|
|
|
first_member.user_id == ^actor(:id) ->
|
|
|
|
first_member_name
|
|
|
|
|
|
|
|
second_member.user_id == ^actor(:id) ->
|
|
|
|
second_member_name
|
|
|
|
|
|
|
|
true ->
|
|
|
|
first_member_name <> ", " <> second_member_name
|
|
|
|
end
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
policies do
|
|
|
|
policy always() do
|
|
|
|
authorize_if(always())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|