refactor: add typespecs and module constants
- Add @spec for public functions in Member and UserLive.Form - Replace magic numbers with module constants: - @member_search_limit = 10 - @default_similarity_threshold = 0.2 - Add comprehensive @doc for filter_by_email_match and fuzzy_search
This commit is contained in:
parent
078809981d
commit
9a03485604
2 changed files with 83 additions and 17 deletions
|
|
@ -38,6 +38,10 @@ defmodule Mv.Membership.Member do
|
|||
require Ash.Query
|
||||
import Ash.Expr
|
||||
|
||||
# Module constants
|
||||
@member_search_limit 10
|
||||
@default_similarity_threshold 0.2
|
||||
|
||||
postgres do
|
||||
table "members"
|
||||
repo Mv.Repo
|
||||
|
|
@ -152,9 +156,10 @@ defmodule Mv.Membership.Member do
|
|||
prepare fn query, _ctx ->
|
||||
q = Ash.Query.get_argument(query, :query) || ""
|
||||
|
||||
# 0.2 as similarity threshold (recommended)
|
||||
# Lower value can lead to more results but also to more unspecific results
|
||||
threshold = Ash.Query.get_argument(query, :similarity_threshold) || 0.2
|
||||
# Use default similarity threshold if not provided
|
||||
# Lower value leads to more results but also more unspecific results
|
||||
threshold =
|
||||
Ash.Query.get_argument(query, :similarity_threshold) || @default_similarity_threshold
|
||||
|
||||
if is_binary(q) and String.trim(q) != "" do
|
||||
q2 = String.trim(q)
|
||||
|
|
@ -226,28 +231,58 @@ defmodule Mv.Membership.Member do
|
|||
fragment("? % first_name", ^trimmed) or
|
||||
fragment("? % last_name", ^trimmed) or
|
||||
fragment("word_similarity(?, first_name) > 0.2", ^trimmed) or
|
||||
fragment("word_similarity(?, last_name) > 0.2", ^trimmed) or
|
||||
fragment("similarity(first_name, ?) > 0.2", ^trimmed) or
|
||||
fragment("similarity(last_name, ?) > 0.2", ^trimmed) or
|
||||
fragment(
|
||||
"word_similarity(?, last_name) > ?",
|
||||
^trimmed,
|
||||
^@default_similarity_threshold
|
||||
) or
|
||||
fragment(
|
||||
"similarity(first_name, ?) > ?",
|
||||
^trimmed,
|
||||
^@default_similarity_threshold
|
||||
) or
|
||||
fragment("similarity(last_name, ?) > ?", ^trimmed, ^@default_similarity_threshold) or
|
||||
contains(email, ^trimmed)
|
||||
)
|
||||
)
|
||||
|> Ash.Query.limit(10)
|
||||
|> Ash.Query.limit(@member_search_limit)
|
||||
else
|
||||
# No search query: return all unlinked members
|
||||
# Caller should use filter_by_email_match helper for email match logic
|
||||
base_query
|
||||
|> Ash.Query.limit(10)
|
||||
|> Ash.Query.limit(@member_search_limit)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Public helper function to apply email match logic after query execution
|
||||
# This should be called after using :available_for_linking with user_email argument
|
||||
#
|
||||
# If a member with matching email exists, returns only that member
|
||||
# Otherwise returns all members (no filtering)
|
||||
@doc """
|
||||
Filters members list to return only email match if exists.
|
||||
|
||||
If a member with matching email exists in the list, returns only that member.
|
||||
Otherwise returns all members unchanged (no filtering).
|
||||
|
||||
This is typically used after calling `:available_for_linking` action with
|
||||
a user_email argument to apply email-match priority logic.
|
||||
|
||||
## Parameters
|
||||
- `members` - List of Member structs to filter
|
||||
- `user_email` - Email string to match against member emails
|
||||
|
||||
## Returns
|
||||
- List of Member structs (either single match or all members)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> members = [%Member{email: "test@example.com"}, %Member{email: "other@example.com"}]
|
||||
iex> filter_by_email_match(members, "test@example.com")
|
||||
[%Member{email: "test@example.com"}]
|
||||
|
||||
iex> filter_by_email_match(members, "nomatch@example.com")
|
||||
[%Member{email: "test@example.com"}, %Member{email: "other@example.com"}]
|
||||
|
||||
"""
|
||||
@spec filter_by_email_match([t()], String.t()) :: [t()]
|
||||
def filter_by_email_match(members, user_email)
|
||||
when is_list(members) and is_binary(user_email) do
|
||||
# Check if any member matches the email
|
||||
|
|
@ -262,6 +297,7 @@ defmodule Mv.Membership.Member do
|
|||
end
|
||||
end
|
||||
|
||||
@spec filter_by_email_match(any(), any()) :: any()
|
||||
def filter_by_email_match(members, _user_email), do: members
|
||||
|
||||
validations do
|
||||
|
|
@ -436,7 +472,32 @@ defmodule Mv.Membership.Member do
|
|||
identity :unique_email, [:email]
|
||||
end
|
||||
|
||||
# Fuzzy Search function that can be called by live view and calls search action
|
||||
@doc """
|
||||
Performs fuzzy search on members using PostgreSQL trigram similarity.
|
||||
|
||||
Wraps the `:search` action with convenient opts-based argument passing.
|
||||
Searches across first_name, last_name, email, and other text fields using
|
||||
full-text search combined with trigram similarity.
|
||||
|
||||
## Parameters
|
||||
- `query` - Ash.Query.t() to apply search to
|
||||
- `opts` - Keyword list or map with search options:
|
||||
- `:query` or `"query"` - Search string
|
||||
- `:fields` or `"fields"` - Optional field restrictions
|
||||
|
||||
## Returns
|
||||
- Modified Ash.Query.t() with search filters applied
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Member |> fuzzy_search(%{query: "Greta"}) |> Ash.read!()
|
||||
[%Member{first_name: "Greta", ...}]
|
||||
|
||||
iex> Member |> fuzzy_search(%{query: "gre"}) |> Ash.read!() # typo-tolerant
|
||||
[%Member{first_name: "Greta", ...}]
|
||||
|
||||
"""
|
||||
@spec fuzzy_search(Ash.Query.t(), keyword() | map()) :: Ash.Query.t()
|
||||
def fuzzy_search(query, opts) do
|
||||
q = (opts[:query] || opts["query"] || "") |> to_string()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue