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