-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take trip planner query params into account in the new trip planner (#…
…2218) * run query params through an anti corruption layer * dont account for time selections * add handle change back in * docs * mix format * function doc * format * alias * submit the form if form values are present * rename function and add tests * rearrange assert * test a given false mode
- Loading branch information
1 parent
24ddd2a
commit ae240e0
Showing
4 changed files
with
162 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Dotcom.TripPlan.AntiCorruptionLayer do | ||
@moduledoc """ | ||
This anti-corruption layer is responsible for converting the data from the old trip planner query params to the new trip planner form values. | ||
Currently, not all modes are sent. So, we default to setting them to 'false' if they aren't set. | ||
Likewise with wheelchair, we default to 'false' if it isn't set because the old trip planner worked by just not setting it rather than setting it to 'false'. | ||
We ignore datetime_type and datetime and allow those to be set to 'now' and the current time respectively. | ||
""" | ||
|
||
@doc """ | ||
Given the params from the old trip planner, convert them to the new trip planner form values. | ||
If no plan is given, then we default to empty form values. | ||
""" | ||
def convert_old_params(%{"plan" => params}) do | ||
%{ | ||
"from" => %{ | ||
"latitude" => Map.get(params, "from_latitude"), | ||
"longitude" => Map.get(params, "from_longitude"), | ||
"name" => Map.get(params, "from"), | ||
"stop_id" => Map.get(params, "from_stop_id", "") | ||
}, | ||
"modes" => Map.get(params, "modes") |> convert_modes(), | ||
"to" => %{ | ||
"latitude" => Map.get(params, "to_latitude"), | ||
"longitude" => Map.get(params, "to_longitude"), | ||
"name" => Map.get(params, "to"), | ||
"stop_id" => Map.get(params, "to_stop_id", "") | ||
}, | ||
"wheelchair" => Map.get(params, "wheelchair") || "false" | ||
} | ||
end | ||
|
||
def convert_old_params(_), do: convert_old_params(%{"plan" => %{}}) | ||
|
||
defp convert_modes(modes) when is_map(modes) do | ||
default_modes = | ||
for {k, _} <- Dotcom.TripPlan.InputForm.initial_modes(), into: %{}, do: {k, "false"} | ||
|
||
modes | ||
|> Enum.reduce(default_modes, fn {key, value}, acc -> | ||
Map.put(acc, String.upcase(key), value) | ||
end) | ||
end | ||
|
||
defp convert_modes(_), do: Dotcom.TripPlan.InputForm.initial_modes() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Dotcom.TripPlan.AntiCorruptionLayerTest do | ||
use ExUnit.Case | ||
|
||
import Dotcom.TripPlan.AntiCorruptionLayer, only: [convert_old_params: 1] | ||
|
||
describe "convert_old_params/1" do | ||
test "returns all defaults when no params are given" do | ||
assert convert_old_params(%{}) == convert_old_params(%{"plan" => %{}}) | ||
end | ||
|
||
test "sets all modes to true when no modes are given" do | ||
old_params = %{ | ||
"plan" => %{} | ||
} | ||
|
||
new_params = convert_old_params(old_params) | ||
|
||
assert new_params["modes"] == Dotcom.TripPlan.InputForm.initial_modes() | ||
end | ||
|
||
test "sets given modes as given and all non-given as false" do | ||
old_params = %{ | ||
"plan" => %{ | ||
"modes" => %{ | ||
"bus" => "true", | ||
"ferry" => "false", | ||
"subway" => "true" | ||
} | ||
} | ||
} | ||
|
||
new_params = convert_old_params(old_params) | ||
|
||
assert new_params["modes"]["BUS"] == "true" | ||
assert new_params["modes"]["FERRY"] == "false" | ||
assert new_params["modes"]["RAIL"] == "false" | ||
assert new_params["modes"]["SUBWAY"] == "true" | ||
end | ||
|
||
test "defaults to wheelchair false when no wheelchair is given" do | ||
old_params = %{ | ||
"plan" => %{} | ||
} | ||
|
||
new_params = convert_old_params(old_params) | ||
|
||
assert new_params["wheelchair"] == "false" | ||
end | ||
end | ||
end |