Skip to content

Commit

Permalink
feat(api): finish up API for returning replacement service within a d…
Browse files Browse the repository at this point in the history
…ate range
  • Loading branch information
rudiejd committed Feb 10, 2025
1 parent d439ffc commit 6892dc3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 14 deletions.
31 changes: 22 additions & 9 deletions lib/arrow/disruptions/replacement_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ defmodule Arrow.Disruptions.ReplacementService do
shuttle: Shuttle.t() | Ecto.Association.NotLoaded.t()
}

@service_type_to_workbook_abbreviation %{
:weekday => "WKDY",
:sunday => "SUN",
:saturday => "SAT"
}

schema "replacement_services" do
field :reason, :string
field :start_date, :date
Expand Down Expand Up @@ -75,19 +81,26 @@ defmodule Arrow.Disruptions.ReplacementService do
end
end

@spec schedule_service_types() :: list(atom())
def schedule_service_types(), do: [:weekday, :saturday, :sunday]

def trips_with_times(replacement_service, :weekday), do: do_trips_with_times(replacement_service, "WKDY")
@spec schedule_service_types :: list(atom())
def schedule_service_types, do: [:weekday, :saturday, :sunday]

def trips_with_times(replacement_service, :saturday), do: do_trips_with_times(replacement_service, "SAT")
def trips_with_times(
%__MODULE__{source_workbook_data: workbook_data} = replacement_service,
service_type_atom
) do
service_type_abbreviation = Map.get(@service_type_to_workbook_abbreviation, service_type_atom)

def trips_with_times(replacement_service, :sunday), do: do_trips_with_times(replacement_service, "SUN")
if Map.has_key?(workbook_data, service_type_abbreviation) do
do_trips_with_times(replacement_service, service_type_abbreviation)
else
nil
end
end

defp do_trips_with_times(
%__MODULE__{source_workbook_data: source_workbook_data, shuttle: shuttle},
day_of_week
) do
%__MODULE__{source_workbook_data: source_workbook_data, shuttle: shuttle},
day_of_week
) do
day_of_week_data = Map.get(source_workbook_data, day_of_week <> " headways and runtimes")

{first_trips, last_trips, headway_periods} =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ defmodule ArrowWeb.API.ReplacementServiceController do
join: rs in assoc(sr, :route_stops),
left_join: gs in assoc(rs, :gtfs_stop),
left_join: st in assoc(rs, :stop),
where: r.start_date <= ^end_date and r.end_date >= ^start_date,
preload: [:disruption, shuttle: {s, routes: {sr, route_stops: [:gtfs_stop, :stop]}}]
)
|> Repo.all()
|> Enum.map(fn rs ->
Map.from_struct(rs)
|> Map.put(
:timetable,

ReplacementService.schedule_service_types()
|> Enum.map(fn service_type ->
{service_type, ReplacementService.trips_with_times(rs, service_type)}
end)
|> Enum.into(%{})
)
|> Map.put("foo", "bar")
end)
dbg()

render(conn, "index.json-api", data: data)
else
Expand Down
3 changes: 3 additions & 0 deletions lib/arrow_web/controllers/api/util.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ArrowWeb.API.Util do
@moduledoc false

@spec parse_date(String.t()) :: {:ok, Date.t()} | {:error, :invalid_date}
def parse_date(nil), do: {:error, :invalid_date}

def parse_date(date_string) do
case Date.from_iso8601(date_string) do
{:ok, date} -> {:ok, date}
Expand Down
2 changes: 1 addition & 1 deletion lib/arrow_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule ArrowWeb.Router do
scope "/api", as: :api, alias: ArrowWeb.API do
# pipe_through([:redirect_prod_http, :json_api, :authenticate_api])

get("/replacement_service", ReplacementServiceController, :index)
get("/replacement-service", ReplacementServiceController, :index)
get("/shuttles", ShuttleController, :index)
get("/limits", LimitController, :index)
resources("/disruptions", DisruptionController, only: [:index])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule ArrowWeb.API.ReplacementServiceControllerTest do
use ArrowWeb.ConnCase

import Arrow.Factory
import Arrow.ShuttlesFixtures

describe "index/2" do
@tag :authenticated
test "can be accessed by non-admin user", %{conn: conn} do
assert %{status: 200} =
get(conn, "/api/replacement-service", %{
start_date: Date.to_iso8601(~D[2025-01-01]),
end_date: Date.to_iso8601(~D[2025-01-02])
})
end

test "gives 400 for invalid date range", %{conn: conn} do
assert %{status: 400} =
get(conn, "/api/replacement-service", %{
start_date: Date.to_iso8601(~D[2025-01-01]),
end_date: Date.to_iso8601(~D[2024-12-31])
})
end

@tag :authenticated
test "only shows active replacement services", %{conn: conn} do
shuttle = shuttle_fixture(%{}, true, true)

_inactive_replacement =
replacement_service_factory(%{
start_date: ~D[2024-01-01],
end_date: ~D[2024-01-02]
})

active_start = ~D[2025-01-01]
active_end = ~D[2025-01-02]
expected_reason = "active today"

_active_replacement =
replacement_service_factory(%{
reason: expected_reason,
start_date: active_start,
end_date: active_end,
shuttle: shuttle
})
|> insert()

active_start_date_formatted = Date.to_iso8601(active_start)
active_end_date_formatted = Date.to_iso8601(active_end)

res =
get(
conn,
"/api/replacement-service?start_date=#{active_end_date_formatted}&end_date=#{active_end_date_formatted}"
)
|> json_response(200)

assert %{
"data" => [
%{
"attributes" => %{
"reason" => ^expected_reason,
"start_date" => ^active_start_date_formatted,
"end_date" => ^active_end_date_formatted,
"timetable" => %{"saturday" => _saturday, "sunday" => _sunday}
}
}
],
"included" => included_list,
"jsonapi" => _
} = res

refute Enum.empty?(included_list)

assert Enum.any?(included_list, &match?(%{"type" => "disruption_v2"}, &1))
assert Enum.any?(included_list, &match?(%{"type" => "shuttle"}, &1))
assert Enum.any?(included_list, &match?(%{"type" => "shuttle_route"}, &1))
assert Enum.any?(included_list, &match?(%{"type" => "shuttle_route_stop"}, &1))
end
end
end
3 changes: 2 additions & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ defmodule Arrow.Factory do
}
end

def replacement_service_factory do
def replacement_service_factory(attrs) do
%Arrow.Disruptions.ReplacementService{
reason: "Maintenance",
start_date: Date.utc_today(),
Expand All @@ -175,6 +175,7 @@ defmodule Arrow.Factory do
disruption: build(:disruption_v2),
shuttle: build(:shuttle)
}
|> merge_attributes(attrs)
end

def replacement_service_workbook_data_factory do
Expand Down

0 comments on commit 6892dc3

Please sign in to comment.