feat(date-filter): introduce DateFilter module with URL codec and Ash query expressions
This commit is contained in:
parent
143c0c5c24
commit
ddd4a9a878
7 changed files with 1195 additions and 0 deletions
|
|
@ -26,6 +26,18 @@ defmodule Mv.Constants do
|
|||
|
||||
@fee_type_filter_prefix "fee_type_"
|
||||
|
||||
@join_date_from_param "jd_from"
|
||||
|
||||
@join_date_to_param "jd_to"
|
||||
|
||||
@exit_date_mode_param "ed_mode"
|
||||
|
||||
@exit_date_from_param "ed_from"
|
||||
|
||||
@exit_date_to_param "ed_to"
|
||||
|
||||
@custom_date_filter_prefix "cdf_"
|
||||
|
||||
@max_boolean_filters 50
|
||||
|
||||
@max_uuid_length 36
|
||||
|
|
@ -84,6 +96,70 @@ defmodule Mv.Constants do
|
|||
"""
|
||||
def fee_type_filter_prefix, do: @fee_type_filter_prefix
|
||||
|
||||
@doc """
|
||||
Returns the URL parameter name for the join_date lower bound filter.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.join_date_from_param()
|
||||
"jd_from"
|
||||
"""
|
||||
def join_date_from_param, do: @join_date_from_param
|
||||
|
||||
@doc """
|
||||
Returns the URL parameter name for the join_date upper bound filter.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.join_date_to_param()
|
||||
"jd_to"
|
||||
"""
|
||||
def join_date_to_param, do: @join_date_to_param
|
||||
|
||||
@doc """
|
||||
Returns the URL parameter name for the exit_date filter mode
|
||||
(`active_only` | `inactive_only` | `all` | `custom`).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.exit_date_mode_param()
|
||||
"ed_mode"
|
||||
"""
|
||||
def exit_date_mode_param, do: @exit_date_mode_param
|
||||
|
||||
@doc """
|
||||
Returns the URL parameter name for the exit_date lower bound filter
|
||||
(only relevant when ed_mode=custom).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.exit_date_from_param()
|
||||
"ed_from"
|
||||
"""
|
||||
def exit_date_from_param, do: @exit_date_from_param
|
||||
|
||||
@doc """
|
||||
Returns the URL parameter name for the exit_date upper bound filter
|
||||
(only relevant when ed_mode=custom).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.exit_date_to_param()
|
||||
"ed_to"
|
||||
"""
|
||||
def exit_date_to_param, do: @exit_date_to_param
|
||||
|
||||
@doc """
|
||||
Returns the prefix for custom date field filter URL parameters
|
||||
(e.g. cdf_<uuid>_from / cdf_<uuid>_to).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.custom_date_filter_prefix()
|
||||
"cdf_"
|
||||
"""
|
||||
def custom_date_filter_prefix, do: @custom_date_filter_prefix
|
||||
|
||||
@doc """
|
||||
Returns the maximum number of boolean custom field filters allowed per request.
|
||||
|
||||
|
|
|
|||
402
lib/mv_web/live/member_live/index/date_filter.ex
Normal file
402
lib/mv_web/live/member_live/index/date_filter.ex
Normal file
|
|
@ -0,0 +1,402 @@
|
|||
defmodule MvWeb.MemberLive.Index.DateFilter do
|
||||
@moduledoc """
|
||||
Encapsulates the complete lifecycle of date-range filters used on the
|
||||
member overview page.
|
||||
|
||||
Owns:
|
||||
|
||||
- the default filter state (active members only)
|
||||
- URL encoding / decoding of filter state
|
||||
- DB-level Ash expression construction for built-in date fields
|
||||
(`join_date`, `exit_date`)
|
||||
- in-memory predicates for custom date-typed custom fields
|
||||
|
||||
## Filter state shape
|
||||
|
||||
%{
|
||||
join_date: %{from: nil | %Date{}, to: nil | %Date{}},
|
||||
exit_date: %{
|
||||
mode: :active_only | :inactive_only | :all | :custom,
|
||||
from: nil | %Date{},
|
||||
to: nil | %Date{}
|
||||
},
|
||||
# optional custom date field entries (UUID string keys):
|
||||
"<uuid>" => %{from: nil | %Date{}, to: nil | %Date{}}
|
||||
}
|
||||
|
||||
The default mode for `exit_date` is `:active_only`, which means
|
||||
`exit_date IS NULL OR exit_date > today` — a member who left today is hidden.
|
||||
"""
|
||||
|
||||
require Ash.Query
|
||||
import Ash.Expr
|
||||
|
||||
@join_date_from_param Mv.Constants.join_date_from_param()
|
||||
@join_date_to_param Mv.Constants.join_date_to_param()
|
||||
@exit_date_mode_param Mv.Constants.exit_date_mode_param()
|
||||
@exit_date_from_param Mv.Constants.exit_date_from_param()
|
||||
@exit_date_to_param Mv.Constants.exit_date_to_param()
|
||||
@custom_date_filter_prefix Mv.Constants.custom_date_filter_prefix()
|
||||
|
||||
@doc """
|
||||
Returns the default date filter state used on fresh page load and after
|
||||
"Clear filters". `exit_date` is set to `:active_only`; all other bounds are nil.
|
||||
"""
|
||||
@spec default() :: %{
|
||||
join_date: %{from: nil, to: nil},
|
||||
exit_date: %{mode: :active_only, from: nil, to: nil}
|
||||
}
|
||||
def default do
|
||||
%{
|
||||
join_date: %{from: nil, to: nil},
|
||||
exit_date: %{mode: :active_only, from: nil, to: nil}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes URL params into a date filter state map.
|
||||
|
||||
Recognized keys:
|
||||
|
||||
* `"jd_from"` / `"jd_to"` — join_date bounds (ISO-8601 dates)
|
||||
* `"ed_mode"` — exit_date mode (`"active_only"` | `"inactive_only"` |
|
||||
`"all"` | `"custom"`); absent or unknown values fall back to
|
||||
`:active_only`
|
||||
* `"ed_from"` / `"ed_to"` — exit_date bounds (ISO-8601 dates, used when
|
||||
`ed_mode=custom`)
|
||||
* `"cdf_<uuid>_from"` / `"cdf_<uuid>_to"` — custom date field bounds;
|
||||
the UUID must appear (by `to_string/1` on its `:id`) in
|
||||
`date_custom_fields`, otherwise the entry is dropped
|
||||
|
||||
Malformed ISO-8601 strings are silently discarded; the corresponding bound
|
||||
stays `nil`. No exception is raised for any malformed input.
|
||||
"""
|
||||
@spec from_params(map(), list()) :: map()
|
||||
def from_params(params, date_custom_fields)
|
||||
when is_map(params) and is_list(date_custom_fields) do
|
||||
base = %{
|
||||
join_date: %{
|
||||
from: parse_date(Map.get(params, @join_date_from_param)),
|
||||
to: parse_date(Map.get(params, @join_date_to_param))
|
||||
},
|
||||
exit_date: %{
|
||||
mode: parse_exit_date_mode(Map.get(params, @exit_date_mode_param)),
|
||||
from: parse_date(Map.get(params, @exit_date_from_param)),
|
||||
to: parse_date(Map.get(params, @exit_date_to_param))
|
||||
}
|
||||
}
|
||||
|
||||
parse_custom_date_filters(params, date_custom_fields, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a date filter state map into a URL params map (string keys, string
|
||||
values).
|
||||
|
||||
Encoding rules:
|
||||
|
||||
* `join_date` from/to → `"jd_from"` / `"jd_to"` (omitted when nil)
|
||||
* `exit_date` mode →
|
||||
- `:active_only` is the default and is omitted entirely (no `ed_mode`,
|
||||
no bounds — a fresh URL is the canonical representation of the default
|
||||
state)
|
||||
- `:all` / `:inactive_only` → `"ed_mode"` only; bounds are omitted
|
||||
- `:custom` → `"ed_mode" => "custom"` plus `"ed_from"` / `"ed_to"`
|
||||
when those bounds are set
|
||||
* custom date field entries (UUID string keys) → `"cdf_<uuid>_from"` /
|
||||
`"cdf_<uuid>_to"`; each bound is included only when non-nil; an entry
|
||||
with both bounds nil produces no params
|
||||
|
||||
All dates are serialized via `Date.to_iso8601/1`.
|
||||
"""
|
||||
@spec to_params(map()) :: %{optional(String.t()) => String.t()}
|
||||
def to_params(filters) when is_map(filters) do
|
||||
%{}
|
||||
|> put_join_date_params(Map.get(filters, :join_date, %{}))
|
||||
|> put_exit_date_params(Map.get(filters, :exit_date, %{}))
|
||||
|> put_custom_date_params(filters)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Applies the DB-level portion of the date filter — `join_date` and
|
||||
`exit_date` constraints — to the given Ash query.
|
||||
|
||||
Exit_date semantics by mode:
|
||||
|
||||
* `:active_only` → `is_nil(exit_date) or exit_date > today`
|
||||
* `:inactive_only` → `not is_nil(exit_date) and exit_date <= today`
|
||||
* `:all` → no filter added for exit_date
|
||||
* `:custom` → `not is_nil(exit_date)` plus the active bounds; if both
|
||||
bounds are nil, no filter is added (the user picked "custom" but
|
||||
entered nothing)
|
||||
|
||||
Join_date is purely a range filter — nil join_date is always excluded when
|
||||
any bound is set:
|
||||
|
||||
* `from` set → `not is_nil(join_date) and join_date >= from`
|
||||
* `to` set → `not is_nil(join_date) and join_date <= to`
|
||||
* neither set → no filter
|
||||
|
||||
Today's date is captured via `Date.utc_today/0`; callers needing a frozen
|
||||
clock should wrap the call site, not this function.
|
||||
"""
|
||||
@spec apply_ash_filter(Ash.Query.t() | module(), map()) :: Ash.Query.t()
|
||||
def apply_ash_filter(query_or_resource, filters) when is_map(filters) do
|
||||
query_or_resource
|
||||
|> Ash.Query.new()
|
||||
|> apply_exit_date_filter(Map.get(filters, :exit_date, %{}))
|
||||
|> apply_join_date_filter(Map.get(filters, :join_date, %{}))
|
||||
end
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :all}), do: query
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :active_only}) do
|
||||
today = Date.utc_today()
|
||||
Ash.Query.filter(query, expr(is_nil(exit_date) or exit_date > ^today))
|
||||
end
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :inactive_only}) do
|
||||
today = Date.utc_today()
|
||||
Ash.Query.filter(query, expr(not is_nil(exit_date) and exit_date <= ^today))
|
||||
end
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :custom, from: nil, to: nil}), do: query
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :custom, from: from, to: nil}) do
|
||||
Ash.Query.filter(query, expr(not is_nil(exit_date) and exit_date >= ^from))
|
||||
end
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :custom, from: nil, to: to}) do
|
||||
Ash.Query.filter(query, expr(not is_nil(exit_date) and exit_date <= ^to))
|
||||
end
|
||||
|
||||
defp apply_exit_date_filter(query, %{mode: :custom, from: from, to: to}) do
|
||||
Ash.Query.filter(
|
||||
query,
|
||||
expr(not is_nil(exit_date) and exit_date >= ^from and exit_date <= ^to)
|
||||
)
|
||||
end
|
||||
|
||||
defp apply_exit_date_filter(query, _), do: query
|
||||
|
||||
defp apply_join_date_filter(query, %{from: nil, to: nil}), do: query
|
||||
|
||||
defp apply_join_date_filter(query, %{from: from, to: nil}) when not is_nil(from) do
|
||||
Ash.Query.filter(query, expr(not is_nil(join_date) and join_date >= ^from))
|
||||
end
|
||||
|
||||
defp apply_join_date_filter(query, %{from: nil, to: to}) when not is_nil(to) do
|
||||
Ash.Query.filter(query, expr(not is_nil(join_date) and join_date <= ^to))
|
||||
end
|
||||
|
||||
defp apply_join_date_filter(query, %{from: from, to: to})
|
||||
when not is_nil(from) and not is_nil(to) do
|
||||
Ash.Query.filter(
|
||||
query,
|
||||
expr(not is_nil(join_date) and join_date >= ^from and join_date <= ^to)
|
||||
)
|
||||
end
|
||||
|
||||
defp apply_join_date_filter(query, _), do: query
|
||||
|
||||
@doc """
|
||||
Applies the in-memory portion of the date filter — custom date fields
|
||||
whose values live in JSONB-backed `custom_field_values`.
|
||||
|
||||
Behavior:
|
||||
|
||||
* Only entries whose UUID key matches a `date_custom_fields` entry
|
||||
(by `to_string(field.id)` and `value_type == :date`) are considered.
|
||||
* Entries with both bounds nil add no constraint.
|
||||
* For an active entry, a member is kept iff its custom field value is
|
||||
present AND the value (unwrapped from `%Ash.Union{type: :date}`)
|
||||
satisfies `value >= from` (when from set) AND `value <= to`
|
||||
(when to set).
|
||||
* Members with `custom_field_values` nil, `%Ash.NotLoaded{}`, an empty
|
||||
list, or no entry for the active field — are excluded.
|
||||
* Non-date `Ash.Union` types are treated as "no value" and exclude the
|
||||
member.
|
||||
|
||||
Returns the filtered list of members (order preserved).
|
||||
"""
|
||||
@spec apply_in_memory([map()], map(), [map()]) :: [map()]
|
||||
def apply_in_memory(members, filters, date_custom_fields)
|
||||
when is_list(members) and is_map(filters) and is_list(date_custom_fields) do
|
||||
active_filters = active_custom_date_filters(filters, date_custom_fields)
|
||||
|
||||
if active_filters == [] do
|
||||
members
|
||||
else
|
||||
Enum.filter(members, &matches_all_custom_dates?(&1, active_filters))
|
||||
end
|
||||
end
|
||||
|
||||
defp matches_all_custom_dates?(member, active_filters) do
|
||||
Enum.all?(active_filters, fn {id, bounds} ->
|
||||
member_matches_custom_date?(member, id, bounds)
|
||||
end)
|
||||
end
|
||||
|
||||
defp active_custom_date_filters(filters, date_custom_fields) do
|
||||
valid_ids =
|
||||
date_custom_fields
|
||||
|> Enum.filter(&date_field?/1)
|
||||
|> MapSet.new(&to_string(field_id(&1)))
|
||||
|
||||
filters
|
||||
|> Enum.filter(fn
|
||||
{key, %{from: from, to: to}} when is_binary(key) ->
|
||||
MapSet.member?(valid_ids, key) and (not is_nil(from) or not is_nil(to))
|
||||
|
||||
_ ->
|
||||
false
|
||||
end)
|
||||
end
|
||||
|
||||
defp member_matches_custom_date?(member, custom_field_id, %{from: from, to: to}) do
|
||||
case extract_member_date(member, custom_field_id) do
|
||||
%Date{} = date -> within_bounds?(date, from, to)
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_member_date(member, custom_field_id) do
|
||||
case Map.get(member, :custom_field_values) do
|
||||
values when is_list(values) ->
|
||||
values
|
||||
|> Enum.find(&cfv_matches_id?(&1, custom_field_id))
|
||||
|> extract_date_from_cfv()
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp cfv_matches_id?(%{custom_field_id: cfid}, id) when not is_nil(cfid),
|
||||
do: to_string(cfid) == id
|
||||
|
||||
defp cfv_matches_id?(%{custom_field: %{id: cfid}}, id) when not is_nil(cfid),
|
||||
do: to_string(cfid) == id
|
||||
|
||||
defp cfv_matches_id?(_, _), do: false
|
||||
|
||||
defp extract_date_from_cfv(nil), do: nil
|
||||
|
||||
defp extract_date_from_cfv(%{value: value}), do: extract_date_value(value)
|
||||
|
||||
defp extract_date_from_cfv(_), do: nil
|
||||
|
||||
defp extract_date_value(%Ash.Union{value: %Date{} = date, type: :date}), do: date
|
||||
defp extract_date_value(_), do: nil
|
||||
|
||||
defp within_bounds?(%Date{} = date, from, to) do
|
||||
from_ok? = is_nil(from) or Date.compare(date, from) != :lt
|
||||
to_ok? = is_nil(to) or Date.compare(date, to) != :gt
|
||||
from_ok? and to_ok?
|
||||
end
|
||||
|
||||
defp put_join_date_params(params, %{from: from, to: to}) do
|
||||
params
|
||||
|> maybe_put_date(@join_date_from_param, from)
|
||||
|> maybe_put_date(@join_date_to_param, to)
|
||||
end
|
||||
|
||||
defp put_join_date_params(params, _), do: params
|
||||
|
||||
defp put_exit_date_params(params, %{mode: :active_only}), do: params
|
||||
|
||||
defp put_exit_date_params(params, %{mode: mode})
|
||||
when mode in [:all, :inactive_only] do
|
||||
Map.put(params, @exit_date_mode_param, Atom.to_string(mode))
|
||||
end
|
||||
|
||||
defp put_exit_date_params(params, %{mode: :custom, from: from, to: to}) do
|
||||
params
|
||||
|> Map.put(@exit_date_mode_param, "custom")
|
||||
|> maybe_put_date(@exit_date_from_param, from)
|
||||
|> maybe_put_date(@exit_date_to_param, to)
|
||||
end
|
||||
|
||||
defp put_exit_date_params(params, _), do: params
|
||||
|
||||
defp put_custom_date_params(params, filters) do
|
||||
prefix = @custom_date_filter_prefix
|
||||
|
||||
filters
|
||||
|> Enum.filter(fn {key, _value} -> is_binary(key) end)
|
||||
|> Enum.reduce(params, fn {id, %{from: from, to: to}}, acc ->
|
||||
acc
|
||||
|> maybe_put_date("#{prefix}#{id}_from", from)
|
||||
|> maybe_put_date("#{prefix}#{id}_to", to)
|
||||
end)
|
||||
end
|
||||
|
||||
defp maybe_put_date(params, _key, nil), do: params
|
||||
|
||||
defp maybe_put_date(params, key, %Date{} = date),
|
||||
do: Map.put(params, key, Date.to_iso8601(date))
|
||||
|
||||
defp parse_date(nil), do: nil
|
||||
|
||||
defp parse_date(value) when is_binary(value) do
|
||||
case Date.from_iso8601(String.trim(value)) do
|
||||
{:ok, date} -> date
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_date(_), do: nil
|
||||
|
||||
defp parse_exit_date_mode("all"), do: :all
|
||||
defp parse_exit_date_mode("inactive_only"), do: :inactive_only
|
||||
defp parse_exit_date_mode("custom"), do: :custom
|
||||
defp parse_exit_date_mode("active_only"), do: :active_only
|
||||
defp parse_exit_date_mode(_), do: :active_only
|
||||
|
||||
defp parse_custom_date_filters(params, date_custom_fields, base) do
|
||||
valid_ids =
|
||||
date_custom_fields
|
||||
|> Enum.filter(&date_field?/1)
|
||||
|> MapSet.new(&to_string(field_id(&1)))
|
||||
|
||||
prefix = @custom_date_filter_prefix
|
||||
|
||||
params
|
||||
|> Enum.reduce(base, fn {key, value}, acc ->
|
||||
with true <- is_binary(key),
|
||||
true <- String.starts_with?(key, prefix),
|
||||
{id, bound} <- split_custom_date_key(key, prefix),
|
||||
true <- MapSet.member?(valid_ids, id),
|
||||
%Date{} = date <- parse_date(value) do
|
||||
update_custom_date_entry(acc, id, bound, date)
|
||||
else
|
||||
_ -> acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp date_field?(%{value_type: :date}), do: true
|
||||
defp date_field?(_), do: false
|
||||
|
||||
defp field_id(%{id: id}), do: id
|
||||
|
||||
defp split_custom_date_key(key, prefix) do
|
||||
rest = String.slice(key, String.length(prefix), String.length(key))
|
||||
|
||||
cond do
|
||||
String.ends_with?(rest, "_from") ->
|
||||
{String.slice(rest, 0, String.length(rest) - 5), :from}
|
||||
|
||||
String.ends_with?(rest, "_to") ->
|
||||
{String.slice(rest, 0, String.length(rest) - 3), :to}
|
||||
|
||||
true ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp update_custom_date_entry(acc, id, bound, date) do
|
||||
current = Map.get(acc, id, %{from: nil, to: nil})
|
||||
Map.put(acc, id, Map.put(current, bound, date))
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue