Skip to content

Commit

Permalink
feat: apply kind filters to listed disruptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Feb 26, 2025
1 parent 8d0c58d commit 06fa946
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/arrow_web/controllers/disruption_v2_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ defmodule ArrowWeb.DisruptionV2Controller do
def index(conn, params) do
filters = Filters.from_params(params)

disruptions = Filters.apply_to_disruptions(Disruptions.list_disruptionsv2(), filters)

render(conn, "index.html",
user: conn.assigns.current_user,
disruptions: Disruptions.list_disruptionsv2(),
disruptions: disruptions,
filters: filters
)
end
Expand Down
33 changes: 32 additions & 1 deletion lib/arrow_web/controllers/disruption_v2_controller/filters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule ArrowWeb.DisruptionV2Controller.Filters do
alias __MODULE__.{Calendar, Table}
import __MODULE__.Helpers

alias Arrow.Disruptions.DisruptionV2

@empty_set MapSet.new()

defmodule Behaviour do
Expand All @@ -20,10 +22,22 @@ defmodule ArrowWeb.DisruptionV2Controller.Filters do

@behaviour Behaviour

@type t :: %__MODULE__{view: Calendar.t() | Table.t()}
@type t :: %__MODULE__{kinds: MapSet.t(atom()), view: Calendar.t() | Table.t()}

defstruct kinds: @empty_set, only_approved?: false, search: nil, view: %Table{}

@disruption_kind_routes %{
blue_line: ["Blue"],
orange_line: ["Orange"],
red_line: ["Red"],
mattapan_line: ["Mattapan"],
green_line: ["Green-B", "Green-C", "Green-D", "Green-E"],
green_line_b: ["Green-B"],
green_line_c: ["Green-C"],
green_line_d: ["Green-D"],
green_line_e: ["Green-E"]
}

@spec calendar?(%__MODULE__{}) :: boolean
def calendar?(%__MODULE__{view: %Calendar{}}), do: true
def calendar?(%__MODULE__{view: %Table{}}), do: false
Expand Down Expand Up @@ -88,4 +102,21 @@ defmodule ArrowWeb.DisruptionV2Controller.Filters do
{key, value} -> [{key, value}]
end)
end

@spec apply_to_disruptions([DisruptionV2.t()], t()) :: [DisruptionV2.t()]
def apply_to_disruptions(disruptions, filters) do
disruptions
|> Enum.filter(fn disruption ->
apply_kinds_filter(disruption, filters)
end)
end

defp apply_kinds_filter(_disruption, %__MODULE__{kinds: kinds}) when kinds == @empty_set,
do: true

defp apply_kinds_filter(disruption, %__MODULE__{kinds: kinds}) do
kind_routes = kinds |> Enum.map(&@disruption_kind_routes[&1]) |> List.flatten()

Enum.any?(disruption.limits, fn limit -> limit.route.id in kind_routes end)
end
end
43 changes: 43 additions & 0 deletions test/arrow_web/controllers/disruption_v2_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule ArrowWeb.DisruptionV2ControllerTest do
use ArrowWeb.ConnCase

import Arrow.Factory

describe "index/2" do
@tag :authenticated
test "lists disruptions", %{conn: conn} do
insert(:limit,
disruption: build(:disruption_v2, title: "Test disruption"),
route: build(:gtfs_route, id: "Red")
)

resp = conn |> get(~p"/disruptionsv2") |> html_response(200)

assert resp =~ "Test disruption"
end

@tag :authenticated
test "lists disruptions that match a filter", %{conn: conn} do
insert(:limit,
disruption: build(:disruption_v2, title: "Test disruption"),
route: build(:gtfs_route, id: "Red")
)

resp = conn |> get(~p"/disruptionsv2?kinds[]=red_line") |> html_response(200)

assert resp =~ "Test disruption"
end

@tag :authenticated
test "doesn't list disruptions that don't match a filter", %{conn: conn} do
insert(:limit,
disruption: build(:disruption_v2, title: "Test disruption"),
route: build(:gtfs_route, id: "Red")
)

resp = conn |> get(~p"/disruptionsv2?kinds[]=orange_line") |> html_response(200)

refute resp =~ "Test disruption"
end
end
end

0 comments on commit 06fa946

Please sign in to comment.