fix(member): order member list chronologically by custom :date fields
This commit is contained in:
parent
1aaa0ece5d
commit
6d4629ef5b
2 changed files with 66 additions and 16 deletions
|
|
@ -231,6 +231,63 @@ defmodule MvWeb.MemberLive.IndexCustomFieldsSortingTest do
|
|||
assert has_element?(view, "[data-testid='custom_field_#{field.id}'][aria-label='ascending']")
|
||||
end
|
||||
|
||||
test "sorts members chronologically by a :date custom field (ascending)", %{conn: conn} do
|
||||
system_actor = Mv.Helpers.SystemActor.get_system_actor()
|
||||
|
||||
# Dates chosen to expose the day-first term-ordering trap: term order of the
|
||||
# %Date{} structs compares day, then month, then year, which would place
|
||||
# 02.07.1986 (day 02) before 29.01.1981 (day 29). Chronologically 1981 < 1982 < 1986.
|
||||
members_and_dates = [
|
||||
{"EightySix", ~D[1986-07-02]},
|
||||
{"EightyOne", ~D[1981-01-29]},
|
||||
{"EightyTwo", ~D[1982-03-01]}
|
||||
]
|
||||
|
||||
{:ok, field} =
|
||||
CustomField
|
||||
|> Ash.Changeset.for_create(:create, %{
|
||||
name: "birth_date",
|
||||
value_type: :date,
|
||||
show_in_overview: true
|
||||
})
|
||||
|> Ash.create(actor: system_actor)
|
||||
|
||||
for {first_name, date} <- members_and_dates do
|
||||
{:ok, member} =
|
||||
Mv.Membership.create_member(
|
||||
%{
|
||||
first_name: first_name,
|
||||
last_name: "Test",
|
||||
email: "#{String.downcase(first_name)}@example.com"
|
||||
},
|
||||
actor: system_actor
|
||||
)
|
||||
|
||||
{:ok, _cfv} =
|
||||
CustomFieldValue
|
||||
|> Ash.Changeset.for_create(:create, %{
|
||||
member_id: member.id,
|
||||
custom_field_id: field.id,
|
||||
value: %{"_union_type" => "date", "_union_value" => Date.to_iso8601(date)}
|
||||
})
|
||||
|> Ash.create(actor: system_actor)
|
||||
end
|
||||
|
||||
conn = conn_with_oidc_user(conn)
|
||||
|
||||
{:ok, view, _html} =
|
||||
live(conn, "/members?query=&sort_field=custom_field_#{field.id}&sort_order=asc")
|
||||
|
||||
html = render(view)
|
||||
|
||||
{one_idx, _} = :binary.match(html, "EightyOne")
|
||||
{two_idx, _} = :binary.match(html, "EightyTwo")
|
||||
{six_idx, _} = :binary.match(html, "EightySix")
|
||||
|
||||
assert one_idx < two_idx, "29.01.1981 must come before 01.03.1982 in ascending order"
|
||||
assert two_idx < six_idx, "01.03.1982 must come before 02.07.1986 in ascending order"
|
||||
end
|
||||
|
||||
test "NULL values and empty strings are always sorted last (ASC)", %{conn: conn} do
|
||||
system_actor = Mv.Helpers.SystemActor.get_system_actor()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue