defmodule TribeOne.TribesPlugin.Supertest.Case do @moduledoc """ Parent resource used by live supertest scenarios. """ 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_cases") repo(Tribes.Repo) custom_indexes do index([:run_id]) index([:run_id, :name], unique: true, where: "deleted_at IS NULL") end end nostr_sync do namespace("supertest.case") 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, :run_id, :name, :counter, :status, :payload]) change(AshNostrSync.PublishChange) end create :sync_upsert do accept([:id, :run_id, :name, :counter, :status, :payload]) upsert?(true) end update :update do require_atomic?(false) accept([:name, :counter, :status, :payload]) change(AshNostrSync.PublishChange) end destroy :destroy do require_atomic?(false) soft?(true) soft_delete() change(AshNostrSync.PublishChange) end end relationships do has_many :events, TribeOne.TribesPlugin.Supertest.Event do destination_attribute(:case_id) 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 :name, :string do allow_nil?(false) public?(true) end attribute :counter, :integer do allow_nil?(false) default(0) public?(true) end attribute :status, :atom do constraints(one_of: [:new, :active, :done]) allow_nil?(false) default(:new) public?(true) end attribute :payload, :map do allow_nil?(false) default(%{}) public?(true) end timestamps(type: :utc_datetime) end end