Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule ApiWeb.ScheduleController do

@filters ~w(date direction_id max_time min_time route stop stop_sequence route_type trip)s
@pagination_opts ~w(offset limit order_by)a
@includes ~w(stop trip prediction route)
@includes ~w(stop trip prediction route added_routes)

def state_module, do: State.Schedule

Expand Down Expand Up @@ -368,6 +368,7 @@ defmodule ApiWeb.ScheduleController do
relationship(:trip)
relationship(:stop)
relationship(:prediction)
relationship(:added_routes, type: :has_many)
end,
Schedules: page(:ScheduleResource)
}
Expand Down
13 changes: 12 additions & 1 deletion apps/api_web/lib/api_web/views/schedule_view.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ApiWeb.ScheduleView do
use ApiWeb.Web, :api_view
alias ApiWeb.Plugs.Deadline
alias JaSerializer.Relationship.HasOne
alias JaSerializer.Relationship.{HasMany, HasOne}

def relationships(_, _) do
%{
Expand All @@ -15,6 +15,13 @@ defmodule ApiWeb.ScheduleView do
serializer: ApiWeb.PredictionView,
identifiers: :when_included,
include: false
},
added_routes: %HasMany{
type: :route,
name: :added_routes,
data: :added_routes,
serializer: ApiWeb.RouteView,
include: true
}
}
end
Expand Down Expand Up @@ -92,6 +99,10 @@ defmodule ApiWeb.ScheduleView do
State.Prediction.prediction_for(schedule, date)
end

def added_routes(%{added_route_ids: added_route_ids}, conn) do
optional_relationship("added_route", added_route_ids, &State.Route.by_ids/1, conn)
end

defp format_time(seconds, conn) when is_integer(seconds) do
conn.assigns
|> case do
Expand Down
30 changes: 29 additions & 1 deletion apps/api_web/test/api_web/views/schedule_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule ApiWeb.ScheduleViewTest do
pickup_type: 2,
drop_off_type: 3,
timepoint?: true,
stop_headsign: "headsign"
stop_headsign: "headsign",
added_route_ids: []
}

test "renders the dates properly", %{conn: conn} do
Expand Down Expand Up @@ -155,4 +156,31 @@ defmodule ApiWeb.ScheduleViewTest do
refute prediction
end
end

describe "added_routes" do
test "empty if not present", %{conn: conn} do
conn = assign(conn, :date, ~D[2026-03-31])
rendered = render(ApiWeb.ScheduleView, "index.json-api", data: @schedule, conn: conn)
assert %{"added_routes" => %{"data" => []}} = rendered["data"]["relationships"]
end

test "populated if present", %{conn: conn} do
conn = assign(conn, :date, ~D[2026-03-31])

rendered =
render(ApiWeb.ScheduleView, "index.json-api",
data: %{@schedule | added_route_ids: ["route1", "route2"]},
conn: conn
)

assert %{
"added_routes" => %{
"data" => [
%{"type" => "route", "id" => "route1"},
%{"type" => "route", "id" => "route2"}
]
}
} = rendered["data"]["relationships"]
end
end
end
4 changes: 3 additions & 1 deletion apps/model/lib/model/schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule Model.Schedule do
:route_id,
:direction_id,
:service_id,
:timepoint?
:timepoint?,
added_route_ids: []
]

@typedoc """
Expand Down Expand Up @@ -73,6 +74,7 @@ defmodule Model.Schedule do
[GTFS `trips.txt` `route_id`](https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md#tripstxt)
* `:service_id` - The service that `trip_id` is following to determine when it is active. See
[GTFS `trips.txt` `service_id`](https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md#tripstxxt)
* `:added_route_ids` - Routes that this trip has been added to via [`multi_route_trips.txt`](https://github.com/mbta/gtfs-documentation/blob/master/reference/gtfs.md#multi_route_tripstxt)
* `:stop_id` - The stop being arrived at and departed from. See
[GTFS `stop_times.txt` `stop_id`](https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md#stop_timestxt)
* `:stop_sequence` - The sequence the `stop_id` is arrived at during the `trip_id`. See
Expand Down
6 changes: 4 additions & 2 deletions apps/model/lib/model/trip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ defmodule Model.Trip do
:route_type,
:bikes_allowed,
:route_pattern_id,
revenue: :REVENUE
revenue: :REVENUE,
added_route_ids: []
]

@type id :: String.t()
Expand All @@ -36,7 +37,8 @@ defmodule Model.Trip do
alternate_route: boolean | nil,
bikes_allowed: 0..2,
route_pattern_id: Model.RoutePattern.id() | nil,
revenue: :REVENUE | :NON_REVENUE
revenue: :REVENUE | :NON_REVENUE,
added_route_ids: [Model.Route.id()]
}

@doc """
Expand Down
4 changes: 3 additions & 1 deletion apps/parse/lib/parse/stop_times.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ defmodule Parse.StopTimes do
stop_headsign: optional_copy(row["stop_headsign"]),
pickup_type: pick_drop_type(row["pickup_type"]),
drop_off_type: pick_drop_type(row["drop_off_type"]),
timepoint?: row["timepoint"] != "0"
timepoint?: row["timepoint"] != "0",
added_route_ids: []
}
end

Expand Down Expand Up @@ -72,6 +73,7 @@ defmodule Parse.StopTimes do
&%{
&1
| route_id: trip.route_id,
added_route_ids: trip.added_route_ids,
direction_id: trip.direction_id,
service_id: trip.service_id
}
Expand Down
2 changes: 1 addition & 1 deletion apps/state/lib/state/trip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule State.Trip do
%{trip | route_id: route_id, alternate_route: true}
end

new_trip = %{trip | alternate_route: false}
new_trip = %{trip | alternate_route: false, added_route_ids: alternate_routes}
[new_trip | new_alternates]
end

Expand Down
11 changes: 4 additions & 7 deletions apps/state/test/state/schedule_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,18 @@ defmodule State.ScheduleTest do
]

schedules = [
@schedule
%{@schedule | added_route_ids: [other_route_id]}
]

State.Route.new_state(routes)
State.Trip.new_state(trips)
Schedule.new_state(schedules)
State.RoutesPatternsAtStop.update!()

assert Enum.sort(Schedule.filter_by(%{stops: [@stop.id], routes: [@route.id]})) == [
@schedule
]
assert Enum.sort(Schedule.filter_by(%{stops: [@stop.id], routes: [@route.id]})) == schedules

assert Enum.sort(Schedule.filter_by(%{stops: [@stop.id], routes: [other_route_id]})) == [
@schedule
]
assert Enum.sort(Schedule.filter_by(%{stops: [@stop.id], routes: [other_route_id]})) ==
schedules
end

test "filters on :stops and :routes" do
Expand Down
Loading