diff --git a/apps/site/lib/site/transit_near_me.ex b/apps/site/lib/site/transit_near_me.ex index 78474043ca..9ad8f82476 100644 --- a/apps/site/lib/site/transit_near_me.ex +++ b/apps/site/lib/site/transit_near_me.ex @@ -408,16 +408,25 @@ defmodule Site.TransitNearMe do date = Keyword.get(opts, :date, Util.service_date()) schedules_fn = Keyword.get(opts, :schedules_fn, &Schedules.Repo.by_route_ids/2) - route_id - |> expand_route_id() - |> schedules_fn.(direction_id: direction_id, date: date) - |> get_predicted_schedules([route: route_id, direction_id: direction_id], opts) - |> Enum.group_by(&PredictedSchedule.route(&1).id) - |> Enum.flat_map(&schedules_for_route(&1, %{}, [], opts).stops_with_directions) - |> convert_route_time_data_to_map() - |> Map.new(fn {stop_id, time_data} -> - {stop_id, limit_route_time_data(time_data)} - end) + schedule_data = + route_id + |> expand_route_id() + |> schedules_fn.(direction_id: direction_id, date: date) + + case schedule_data do + {:error, [%JsonApi.Error{code: "no_service"}]} -> + %{} + + _ -> + schedule_data + |> get_predicted_schedules([route: route_id, direction_id: direction_id], opts) + |> Enum.group_by(&PredictedSchedule.route(&1).id) + |> Enum.flat_map(&schedules_for_route(&1, %{}, [], opts).stops_with_directions) + |> convert_route_time_data_to_map() + |> Map.new(fn {stop_id, time_data} -> + {stop_id, limit_route_time_data(time_data)} + end) + end end @spec convert_route_time_data_to_map([stop_with_data]) :: %{Stop.id_t() => [headsign_data]} diff --git a/apps/site/test/site/transit_near_me_test.exs b/apps/site/test/site/transit_near_me_test.exs index 824c4bb9b4..7f6b589367 100644 --- a/apps/site/test/site/transit_near_me_test.exs +++ b/apps/site/test/site/transit_near_me_test.exs @@ -461,5 +461,34 @@ defmodule Site.TransitNearMeTest do assert %{"place-wimnl" => [%{}]} = actual end + + test "return neither schedules nor predictions if date is outside rating" do + predictions_fn = fn _ -> [@prediction] end + + schedules_fn = fn _, _ -> + {:error, + [ + %JsonApi.Error{ + code: "no_service", + detail: nil, + meta: %{ + "end_date" => "2020-03-14", + "start_date" => "2019-12-06", + "version" => "Winter 2020, 2019-12-13T17:29:50+00:00, version D" + }, + source: %{"parameter" => "date"} + } + ]} + end + + actual = + TransitNearMe.time_data_for_route_by_stop(@route.id, 1, + schedules_fn: schedules_fn, + predictions_fn: predictions_fn, + now: @now + ) + + assert actual == %{} + end end end