defmodule TribeOne.TribesPlugin.Supertest.Event do @moduledoc """ Child resource linked to a supertest case. """ use Ash.Resource, otp_app: :tribe_one_supertest, domain: TribeOne.TribesPlugin.Supertest, data_layer: AshPostgres.DataLayer, extensions: [AshNostrSync] import Ash.Expr postgres do table("supertest_events") repo(Tribes.Repo) references do reference(:case, on_delete: :delete, on_update: :update) end custom_indexes do index([:run_id]) index([:case_id]) index([:case_id, :sequence], unique: true, where: "deleted_at IS NULL") end end nostr_sync do namespace("supertest.event") lane(:control) publish?(true) consume?(true) end actions do defaults([:read]) read :get_by_id do get?(true) get_by(:id) end read :by_run do argument :run_id, :string do allow_nil?(false) end prepare(build(filter: expr(run_id == ^arg(:run_id)), sort: [inserted_at: :asc])) end create :create do accept([:id, :case_id, :run_id, :sequence, :body, :payload]) change(AshNostrSync.PublishChange) end create :sync_upsert do accept([:id, :case_id, :run_id, :sequence, :body, :payload]) upsert?(true) end destroy :destroy do require_atomic?(false) soft?(true) soft_delete() change(AshNostrSync.PublishChange) end end relationships do belongs_to :case, TribeOne.TribesPlugin.Supertest.Case do allow_nil?(false) public?(true) end end attributes do attribute :id, :uuid do allow_nil?(false) primary_key?(true) public?(true) writable?(true) default(&Ash.UUID.generate/0) end attribute :run_id, :string do allow_nil?(false) public?(true) end attribute :sequence, :integer do allow_nil?(false) public?(true) end attribute :body, :string do allow_nil?(false) public?(true) end attribute :payload, :map do allow_nil?(false) default(%{}) public?(true) end timestamps(type: :utc_datetime) end end