From 5a137709f1753331b0a86964602f26237130cccc Mon Sep 17 00:00:00 2001 From: Phil Darnowsky Date: Thu, 16 Jan 2020 16:55:50 -0500 Subject: [PATCH] Handle added trips in finder_api FinderApi.departures implicitly assumed that all trips have associated schedules, which is not true of added trips. This assumption led to a nil creeping into an unexpected place, leading ultimately to an UndefinedFunctionError. This case is now accounted for. --- .../controllers/schedule/finder_api.ex | 27 ++++++++++++------- .../controllers/schedule/finder_api_test.exs | 20 ++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/apps/site/lib/site_web/controllers/schedule/finder_api.ex b/apps/site/lib/site_web/controllers/schedule/finder_api.ex index b1b7a7373a..54df4e200f 100644 --- a/apps/site/lib/site_web/controllers/schedule/finder_api.ex +++ b/apps/site/lib/site_web/controllers/schedule/finder_api.ex @@ -161,12 +161,11 @@ defmodule SiteWeb.ScheduleController.FinderApi do @spec enhance_journeys(Journey.t()) :: map defp enhance_journeys(%{departure: departure} = journey) do now = Timex.now() - diff = DateTime.diff(Timex.now(), departure.schedule.time) time_map = departure |> TransitNearMe.build_time_map(now: now) - |> recent_departure(departure.prediction, diff) + |> recent_departure(departure, now) Map.put(journey, :realtime, time_map) end @@ -177,13 +176,23 @@ defmodule SiteWeb.ScheduleController.FinderApi do # a prediction's status and limit recent trips to a certain time range. # NOTE: Only works for N/S Station and Back Bay, and predictions will # drop off (become nil) anytime during the duration of the trip. - defp recent_departure({_, details}, %{status: "Departed"} = prediction, time_elapsed) - when time_elapsed <= @recent_departure_max_age do - Map.put(details, :prediction, %{ - time: details.scheduled_time, - track: prediction.track, - status: "Departed" - }) + defp recent_departure( + {_, details}, + %{schedule: schedule, prediction: %{status: "Departed"} = prediction}, + now + ) + when not is_nil(schedule) do + time_elapsed = DateTime.diff(now, schedule.time) + + if time_elapsed <= @recent_departure_max_age do + Map.put(details, :prediction, %{ + time: details.scheduled_time, + track: prediction.track, + status: "Departed" + }) + else + details + end end defp recent_departure({_, details}, _, _), do: details diff --git a/apps/site/test/site_web/controllers/schedule/finder_api_test.exs b/apps/site/test/site_web/controllers/schedule/finder_api_test.exs index b358861375..4eb8aff8f9 100644 --- a/apps/site/test/site_web/controllers/schedule/finder_api_test.exs +++ b/apps/site/test/site_web/controllers/schedule/finder_api_test.exs @@ -153,6 +153,26 @@ defmodule SiteWeb.ScheduleController.FinderApiTest do } ] = response end + + test "can handle added trips", %{conn: conn} do + added_prediction = %Prediction{@prediction | schedule_relationship: :added} + + path = + finder_api_path(conn, :departures, %{ + id: "CR-Providence", + direction: "0", + stop: "place-sstat" + }) + + response = + conn + |> assign(:schedules_fn, fn _, _ -> [] end) + |> assign(:predictions_fn, fn _ -> [added_prediction] end) + |> get(path) + |> json_response(200) + + assert [%{"departure" => %{"prediction" => added_prediction, "schedule" => nil}}] = response + end end describe "trip/2" do