diff --git a/lib/services/service.ex b/lib/services/service.ex index dad1c0a330..1e8619b614 100644 --- a/lib/services/service.ex +++ b/lib/services/service.ex @@ -184,18 +184,19 @@ defmodule Services.Service do |> Interval.new() |> Enum.map(& &1) end + |> Enum.map(&Timex.to_date/1) removed_dates = parse_listed_dates(removed_dates) - (dates ++ parse_listed_dates(added_dates)) - |> Enum.reject(fn date -> - # if valid_days is an empty array, the service's dates are likely those in - # added_dates, so we can ignore evaluating the day of the week here - Enum.member?(removed_dates, date) || - if valid_days != [], do: Timex.weekday(date) not in valid_days - end) - |> Enum.map(&Timex.to_date/1) - |> Enum.uniq() + explicitly_added_dates = parse_listed_dates(added_dates) + + valid_dates = + dates + |> Stream.reject(fn date -> Enum.member?(removed_dates, date) end) + |> Stream.reject(fn date -> Timex.weekday(date) not in valid_days end) + |> Enum.to_list() + + Enum.uniq(explicitly_added_dates ++ valid_dates) end @spec parse_listed_dates([String.t()]) :: [NaiveDateTime.t()] @@ -204,5 +205,6 @@ defmodule Services.Service do |> Enum.map(&Timex.parse(&1, "{ISOdate}")) |> Enum.filter(&(elem(&1, 0) == :ok)) |> Enum.map(&elem(&1, 1)) + |> Enum.map(&Timex.to_date/1) end end diff --git a/test/dotcom_web/controllers/stop_controller_test.exs b/test/dotcom_web/controllers/stop_controller_test.exs index 35f54c3498..2109e1293b 100644 --- a/test/dotcom_web/controllers/stop_controller_test.exs +++ b/test/dotcom_web/controllers/stop_controller_test.exs @@ -166,7 +166,8 @@ defmodule DotcomWeb.StopControllerTest do %JsonApi.Item{ id: "", attributes: %{ - "schedule_type" => "Weekday" + "schedule_type" => "Weekday", + "valid_days" => [1, 2, 3, 4, 5, 6, 7] }, relationships: %{}, type: "service" diff --git a/test/services/service_test.exs b/test/services/service_test.exs index 282e3486e5..c4dfdec226 100644 --- a/test/services/service_test.exs +++ b/test/services/service_test.exs @@ -90,30 +90,31 @@ defmodule Services.ServiceTest do end describe "serves_date?/2" do - test "computes if the date is covered by a Service" do - # date in added_dates + test "returns true if the date is in added_dates" do assert Service.serves_date?( %Service{ - added_dates: ["2022-12-15", "2022-12-14"], - start_date: ~D[2022-12-14], - end_date: ~D[2022-12-15], - valid_days: [] + added_dates: ["2022-12-15"], + start_date: ~D[2022-12-01], + end_date: ~D[2022-12-14], + valid_days: [1, 2, 3, 5] }, ~D[2022-12-15] ) + end - # date in removed_dates + test "returns false if the date is in removed_dates" do refute Service.serves_date?( %Service{ removed_dates: ["2022-12-15", "2022-12-14"], start_date: ~D[2022-12-11], end_date: ~D[2022-12-22], - valid_days: [] + valid_days: [1, 2, 3, 4, 5, 6, 7] }, ~D[2022-12-15] ) + end - # date is on right valid_days + test "returns true if the date is on a 'valid_days' day of the week" do assert Service.serves_date?( %Service{ start_date: ~D[2022-12-11], @@ -122,8 +123,9 @@ defmodule Services.ServiceTest do }, ~D[2022-12-15] ) + end - # date not on right valid_days + test "returns false if the date is not on a 'valid_days' day of the week" do refute Service.serves_date?( %Service{ start_date: ~D[2022-12-11],