Skip to content

Commit

Permalink
fix: don't mix-match route name and id!
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahpurcell committed Jan 6, 2025
1 parent e269c7d commit 18605d4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
12 changes: 6 additions & 6 deletions lib/skate/detours/detours.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Skate.Detours.Detours do
def active_detours_by_route(route_id) do
list_detours()
|> Enum.filter(fn detour ->
categorize_detour(detour) == :active and get_detour_route(detour) == route_id
categorize_detour(detour) == :active and get_detour_route_id(detour) == route_id
end)
|> Enum.map(fn detour -> db_detour_to_detour(detour) end)
end
Expand Down Expand Up @@ -131,9 +131,9 @@ defmodule Skate.Detours.Detours do

def categorize_detour(_detour_context), do: :draft

@spec get_detour_route(detour :: map()) :: String.t()
defp get_detour_route(%{state: %{"context" => %{"route" => %{"name" => route_name}}}}),
do: route_name
@spec get_detour_route_id(detour :: map()) :: String.t()
defp get_detour_route_id(%{state: %{"context" => %{"route" => %{"id" => route_id}}}}),
do: route_id

@doc """
Gets a single detour.
Expand Down Expand Up @@ -264,7 +264,7 @@ defmodule Skate.Detours.Detours do
|> User.get_by_id!()
|> Map.get(:uuid)

route_id = get_detour_route(detour)
route_id = get_detour_route_id(detour)

Phoenix.PubSub.broadcast(
Skate.PubSub,
Expand All @@ -286,7 +286,7 @@ defmodule Skate.Detours.Detours do
end

defp broadcast_detour(:past, detour, _author_id) do
route_id = get_detour_route(detour)
route_id = get_detour_route_id(detour)

Phoenix.PubSub.broadcast(
Skate.PubSub,
Expand Down
4 changes: 2 additions & 2 deletions test/skate_web/channels/detours_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ defmodule SkateWeb.DetoursChannelTest do
draft_detour = :detour_snapshot |> build() |> with_id(1)

active_detour_one =
:detour_snapshot |> build() |> activated |> with_id(2) |> with_route_name("57")
:detour_snapshot |> build() |> activated |> with_id(2) |> with_route("57")

active_detour_two =
:detour_snapshot |> build() |> activated |> with_id(3) |> with_route_name("66")
:detour_snapshot |> build() |> activated |> with_id(3) |> with_route("66")

past_detour = :detour_snapshot |> build() |> deactivated |> with_id(4)

Expand Down
6 changes: 6 additions & 0 deletions test/skate_web/controllers/detours_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ defmodule SkateWeb.DetoursControllerTest do
"snapshot" => %{
"context" => %{
"route" => %{
"id" => "23",
"name" => "23",
"directionNames" => %{
"0" => "Outbound",
Expand All @@ -154,6 +155,7 @@ defmodule SkateWeb.DetoursControllerTest do
"snapshot" => %{
"context" => %{
"route" => %{
"id" => "47",
"name" => "47",
"directionNames" => %{
"0" => "Outbound",
Expand All @@ -176,6 +178,7 @@ defmodule SkateWeb.DetoursControllerTest do
"snapshot" => %{
"context" => %{
"route" => %{
"id" => "75",
"name" => "75",
"directionNames" => %{
"0" => "Outbound",
Expand Down Expand Up @@ -358,6 +361,7 @@ defmodule SkateWeb.DetoursControllerTest do
Detours.upsert_from_snapshot(other_user.id, %{
"context" => %{
"route" => %{
# "id" => "23",
"name" => "23",
"directionNames" => %{
"0" => "Outbound",
Expand Down Expand Up @@ -423,6 +427,7 @@ defmodule SkateWeb.DetoursControllerTest do
"snapshot" => %{
"context" => %{
"route" => %{
"id" => "23",
"name" => "23",
"directionNames" => %{
"0" => "Outbound",
Expand All @@ -444,6 +449,7 @@ defmodule SkateWeb.DetoursControllerTest do
"snapshot" => %{
"context" => %{
"route" => %{
"id" => "23",
"name" => "23"
},
"routePattern" => %{
Expand Down
20 changes: 16 additions & 4 deletions test/support/factories/detour_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Skate.DetourFactory do
"context" => %{
"uuid" => nil,
"route" => %{
"id" => sequence("detour_route_id:"),
"name" => sequence("detour_route_name:"),
"directionNames" => %{
"0" => "Outbound",
Expand Down Expand Up @@ -74,12 +75,23 @@ defmodule Skate.DetourFactory do
put_in(state["value"], %{"Detour Drawing" => "Past"})
end

def with_route_name(%Skate.Detours.Db.Detour{} = detour, route_name) do
%{detour | state: with_route_name(detour.state, route_name)}
def with_route(%Skate.Detours.Db.Detour{} = detour, route) do
%{detour | state: with_route(detour.state, route)}
end

def with_route_name(%{"context" => %{"route" => %{"name" => _}}} = state, route_name) do
put_in(state["context"]["route"]["name"], route_name)
def with_route(
%{
"context" => %{
"route" => %{"name" => _, "id" => _, "directionNames" => direction_names}
}
} = state,
route
) do
put_in(state["context"]["route"], %{
"name" => route,
"id" => route,
"directionNames" => direction_names
})
end

def with_direction(%Skate.Detours.Db.Detour{} = detour, direction) do
Expand Down

0 comments on commit 18605d4

Please sign in to comment.