1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-21 15:32:32 +00:00

fix(back): event.ends_on can be nil

This commit is contained in:
setop 2024-12-06 12:49:08 +01:00
parent 4489743853
commit 255c8fda67
2 changed files with 21 additions and 7 deletions

View file

@ -374,7 +374,7 @@ defmodule Mobilizon.Events do
atom,
boolean,
boolean | nil,
string | nil,
String.t() | nil,
float | nil
) :: Page.t(Event.t())
def list_events(
@ -2029,11 +2029,20 @@ defmodule Mobilizon.Events do
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)
where(
query,
[e],
(is_nil(e.ends_on) and e.begins_on >= ^after_datetime) or
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)
where(
query,
[e],
(is_nil(e.ends_on) and e.begins_on <= ^before_datetime) or e.ends_on <= ^before_datetime
)
end
defp event_filter_ends_on(
@ -2042,8 +2051,8 @@ defmodule Mobilizon.Events do
%DateTime{} = before_datetime
) do
query
|> where([e], e.ends_on < ^before_datetime)
|> where([e], e.ends_on > ^after_datetime)
|> event_filter_ends_on(after_datetime, nil)
|> event_filter_ends_on(nil, before_datetime)
end
defp event_order_by(query, order_by, direction)
@ -2145,6 +2154,10 @@ defmodule Mobilizon.Events do
# Handling the case where Repo.XXXX() return nil
def with_virtual_fields(nil), do: nil
# if envent has no end date, it can not be a long events
def with_virtual_fields(%Event{} = event) when is_nil(event.ends_on),
do: %{event | long_event: false}
# Handling the case where there is an event
# Using Repo.one(), for example
def with_virtual_fields(%Event{} = event) do

View file

@ -115,11 +115,12 @@ defmodule Mobilizon.Web.FeedControllerTest do
begins_on: DateTime.add(DateTime.utc_now(), 4, :day)
)
event3 =
event_in_the_past =
insert(:event,
organizer_actor: actor,
attributed_to: group,
title: "Event Three",
ends_on: DateTime.add(DateTime.utc_now(), -3, :day),
begins_on: DateTime.add(DateTime.utc_now(), -2, :day)
)
@ -135,7 +136,7 @@ defmodule Mobilizon.Web.FeedControllerTest do
Enum.each(entries, fn entry ->
assert entry.summary in [event1.title, event2.title]
refute entry.summary == event3.title
refute entry.summary == event_in_the_past.title
end)
assert entry1.categories == [tag1.title]