Well I was removing part of the code and miss-click, but yeah, properties_normalized_full_address_gin_trm_ops is a custom index
defmodule Marketplace.Markets.Property do
@moduledoc false
# alias ...
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
extensions: [AshGraphql.Resource, AshRbac],
notifiers: [Ash.Notifier.PubSub]
require Ash.Query
code_interface do
define_for Marketplace.Markets
# define ...
end
attributes do
alias Marketplace.Ash.Types.Currency
uuid_primary_key :id
attribute :street, :string, allow_nil?: false
attribute :house_number, :string, allow_nil?: false
attribute :city, :string, allow_nil?: false
attribute :county, :string, allow_nil?: false
attribute :state, :string do
allow_nil? false
constraints max_length: 2, min_length: 2
end
attribute :zip, :string, allow_nil?: false
attribute :country, :string do
allow_nil? false
default "USA"
end
attribute :normalized_full_address, :string, allow_nil?: false, generated?: true
attribute :view_count, :integer, default: 0
attribute :showing, Showing, allow_nil?: false, default: %{}
attribute :over_under_paid, Currency
attribute :adj_mao, Currency
attribute :opened_at, :utc_datetime, default: &DateTime.utc_now/0
timestamps(private?: false)
end
calculations do
alias Markets.Offer
calculate :full_address,
:string,
expr(
house_number <> " " <> street <> " - " <> city <> ", " <> state <> " - " <> zip
)
calculate :offeror_current_offer, Offer, Calculations.GetCurrentOfferor
calculate :favorite?,
:boolean,
expr(
exists(property_user_activities, user_id == ^actor(:id) and favorited? == true)
)
calculate :total_unique_offers, Offer, Calculations.TotalUniqueOffers
end
preparations do
prepare build(load: [:full_address, :favorite?])
end
aggregates do
first :last_offer, :offers, :updated_at do
sort updated_at: :desc
end
count :total_valid_offer, :offers do
filter expr(status in [:submitted, :evaluating, :accepted])
end
count :favorite_count, :user_activities
count :total_offers, :offers do
filter expr(status in [:submitted, :evaluating, :accepted])
end
max :max_offer, :offers, :price do
filter expr(status in [:submitted, :evaluating, :accepted])
end
end
relationships do
alias Markets.{Offer, Organization, PropertyUserActivity, User}
has_many :offers, Offer
belongs_to :disposition_agent, User do
allow_nil? true
attribute_writable? true
end
belongs_to :organization, Organization do
allow_nil? false
attribute_writable? true
end
many_to_many :user_activities, User do
through PropertyUserActivity
join_relationship :property_user_activities
source_attribute_on_join_resource :property_id
destination_attribute_on_join_resource :user_id
end
end
rbac do
#...
end
policies do
#...
end
changes do
alias Marketplace.Common.Property.Actions.Changes
change {Marketplace.Ash.Changes.Audit, meta: %{actor: actor(:id)}},
on: [:create, :update, :destroy]
change Actions.Common.Changes.SendTelemetryEvent,
on: [:create, :update, :destroy]
change Changes.CalculateOverUnderPaid, on: [:create, :update, :destroy]
end
graphql do
type :property
field_names favorite?: :is_favorite
queries do
get :get_valid_property, :get_valid
list :list_valid_properties, :read_valids
list :list_favorites, :list_user_favorites
end
mutations do
update :set_as_favorite, :set_as_favorite
update :remove_from_favorite, :remove_from_favorite
end
end
pub_sub do
module MarketplaceWeb.Endpoint
prefix "property"
publish :update, [:id], event: "property_updated"
publish :update_images, [:id], event: "property_updated"
end
postgres do
table "properties"
repo Marketplace.Repo
migration_defaults images: "[]", metadata: "%{}", showing: "%{}"
migration_ignore_attributes [:normalized_full_address]
custom_indexes do
index ["normalized_full_address gin_trgm_ops"],
using: "gin",
name: "properties_normalized_full_address_gin_trgm_ops"
index [:view_count]
end
custom_statements do
statement :create_normalized_full_address_field do
up """
alter table properties add column normalized_full_address varchar (255) generated always as
(
lower(
btrim(
regexp_replace(
coalesce(house_number, '') || ' ' ||
coalesce(street, '') || ' ' ||
coalesce(city, '') || ' ' ||
coalesce(county, '') || ' ' ||
state || ' ' ||
country || ' ' ||
coalesce(zip, ''),
'\\s+', ' ', 'g'
)
)
)
)
stored;
"""
down "alter table properties drop column normalized_full_address;"
end
end
end
actions do
alias Marketplace.Markets.Common.Changelog
defaults [:read]
create :create do
alias Actions.Create.Changes
primary? true
argument :uploaded_images, {:array, :map}, allow_nil?: false
argument :removed_images, {:array, :map}, allow_nil?: false
argument :disposition_agent, :uuid, allow_nil?: false
change Changes.MaybeCreateProcessImagesJob
change Changes.MaybeIgnoreOffMarket
change Changes.AddOrganizationFromActor
change Changes.AddDispositionAgent
change load([:full_address, :last_offer, :total_valid_offer])
end
update :update do
alias Actions.Update.{Changes, Validations}
primary? true
argument :uploaded_images, {:array, :map}, allow_nil?: false, default: []
argument :removed_images, {:array, :map}, allow_nil?: false, default: []
argument :disposition_agent, :uuid, allow_nil?: true
validate Validations.IsNotProcessing
change Changes.MaybeCreateProcessImagesJob
change Changes.MaybeIgnoreOffMarket
change Changes.MaybeUpdateProForma
change Changes.MaybeUpdateOverUnderPaid
change Changes.AddDispositionAgent
change load([:full_address, :last_offer, :total_valid_offer])
end
update :update_view_count do
argument :view_count, :integer, allow_nil?: false
change set_attribute(:view_count, arg(:view_count))
end
end
end