Issue #1567 : use ends_on for comparaisons to get future and ongoing events

This commit is contained in:
Massedil 2024-11-22 18:45:11 +01:00 committed by setop
parent 04fe44f72b
commit f67bd900f7

View file

@ -530,7 +530,9 @@ defmodule Mobilizon.Events do
group_id
|> event_for_group_query()
|> event_filter_visibility(visibility)
|> event_filter_begins_on(after_datetime, before_datetime)
# We want future and ongoing events, so we use ends_on
# See issue #1567
|> event_filter_ends_on(after_datetime, before_datetime)
|> event_order_by(order_by, order_direction)
|> preload_for_event()
|> Page.build_page(page, limit)
@ -2024,6 +2026,26 @@ defmodule Mobilizon.Events do
|> where([e], e.begins_on > ^after_datetime)
end
defp event_filter_ends_on(query, nil, nil), do: query
defp event_filter_ends_on(query, %DateTime{} = after_datetime, nil) do
where(query, [e], e.ends_on > ^after_datetime)
end
defp event_filter_ends_on(query, nil, %DateTime{} = before_datetime) do
where(query, [e], e.ends_on < ^before_datetime)
end
defp event_filter_ends_on(
query,
%DateTime{} = after_datetime,
%DateTime{} = before_datetime
) do
query
|> where([e], e.ends_on < ^before_datetime)
|> where([e], e.ends_on > ^after_datetime)
end
defp event_order_by(query, order_by, direction)
when order_by in [:begins_on, :inserted_at, :updated_at] and direction in [:asc, :desc] do
order_by_instruction = Keyword.new([{direction, order_by}])