diff --git a/lib/alerts/sort.ex b/lib/alerts/sort.ex index 44614a052d..d78347e58f 100644 --- a/lib/alerts/sort.ex +++ b/lib/alerts/sort.ex @@ -54,7 +54,7 @@ defmodule Alerts.Sort do lifecycle_index(alert.lifecycle), -alert.severity, -updated_at_date(alert.updated_at), - first_future_active_period_start(alert.active_period, now), + first_future_active_period_start(alert, now), alert.id } end @@ -91,26 +91,10 @@ defmodule Alerts.Sort do defp priority(%{priority: :high}), do: 0 defp priority(%{priority: :system}), do: 1 - # atoms are greater than any integer - defp first_future_active_period_start([], _now), do: :infinity - - defp first_future_active_period_start(periods, now) do - # first active period that's in the future - now_unix = DateTime.to_unix(now, :second) - - future_periods = - for {start, _} <- periods, - start, - # wrap in a list to avoid an Erlang 19.3 issue - unix <- [DateTime.to_unix(start)], - unix > now_unix do - unix - end - - if future_periods == [] do - :infinity - else - Enum.min(future_periods) + defp first_future_active_period_start(alert, now) do + case Dotcom.Alerts.StartTime.next_active_time(alert, now) do + {_, datetime} -> DateTime.to_unix(datetime) + :past -> :infinity end end end diff --git a/lib/dotcom/alerts/start_time.ex b/lib/dotcom/alerts/start_time.ex index 3b7c561472..db685001ea 100644 --- a/lib/dotcom/alerts/start_time.ex +++ b/lib/dotcom/alerts/start_time.ex @@ -41,9 +41,14 @@ defmodule Dotcom.Alerts.StartTime do time ) do cond do + # period already ended -- keep iterating ends_before?(end_time, time) -> next_active_period_active_time(rest_of_active_periods, time) + # period hasn't ended yet, but no defined start time -- use the current time + is_nil(start_time) -> + {:current, time} + DateTime.before?(start_time, time) -> {:current, start_time} diff --git a/lib/dotcom_web/controllers/stop_controller.ex b/lib/dotcom_web/controllers/stop_controller.ex index f6f27b38cd..1bbeda6d1e 100644 --- a/lib/dotcom_web/controllers/stop_controller.ex +++ b/lib/dotcom_web/controllers/stop_controller.ex @@ -31,7 +31,6 @@ defmodule DotcomWeb.StopController do routes: [route_with_directions] } - plug(:alerts) plug(DotcomWeb.Plugs.DateTime) plug(DotcomWeb.Plugs.AlertsByTimeframe) @@ -81,6 +80,7 @@ defmodule DotcomWeb.StopController do conn |> assign(:breadcrumbs, breadcrumbs(stop, routes_by_stop)) |> meta_description(stop, routes_by_stop) + |> assign_alerts() |> render("show.html", %{ stop: stop, amenity_param: Map.get(params, "amenity", "") |> String.to_atom(), @@ -347,11 +347,11 @@ defmodule DotcomWeb.StopController do @spec includes_predictions?(TransitNearMe.headsign_data()) :: boolean defp includes_predictions?(%{times: times}), do: Enum.any?(times, &(&1.prediction != nil)) - defp alerts(%{assigns: %{alerts: alerts}} = conn, _opts) do + defp assign_alerts(%{assigns: %{alerts: alerts}} = conn) do assign(conn, :all_alerts_count, length(alerts)) end - defp alerts(%{path_params: %{"id" => id}} = conn, _opts) do + defp assign_alerts(%{path_params: %{"id" => id}} = conn) do stop_id = URI.decode_www_form(id) alerts = @@ -364,7 +364,7 @@ defmodule DotcomWeb.StopController do |> assign(:all_alerts_count, length(alerts)) end - defp alerts(conn, _opts) do + defp assign_alerts(conn) do assign(conn, :alerts, AlertsRepo.all(conn.assigns.date_time)) end diff --git a/test/dotcom/alerts/start_time_test.exs b/test/dotcom/alerts/start_time_test.exs index 2ee691d3d7..5ca5887078 100644 --- a/test/dotcom/alerts/start_time_test.exs +++ b/test/dotcom/alerts/start_time_test.exs @@ -66,6 +66,15 @@ defmodule Dotcom.Alerts.StartTimeTest do assert next_active_time(alert, time) == :past end + + test "returns :current if active period has nil start time but ends in the future" do + time = Test.Support.Generators.DateTime.random_date_time() + end_time = Test.Support.Generators.DateTime.random_date_time_after(time) + + alert = Alert.build(:alert, active_period: [{nil, end_time}]) + + assert next_active_time(alert, time) == {:current, time} + end end describe "active?/2" do