From 53beaf2500bc30b77d22e6dd144dcf5d88b95a71 Mon Sep 17 00:00:00 2001 From: Ryan Mahoney Date: Mon, 22 Jul 2019 11:14:36 -0400 Subject: [PATCH] Schedule Finder: remove duplicate services (#105) --- .../controllers/schedule/line_controller.ex | 18 +++++++++++++++++- .../controllers/schedule/line_test.exs | 13 +++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/apps/site/lib/site_web/controllers/schedule/line_controller.ex b/apps/site/lib/site_web/controllers/schedule/line_controller.ex index 2a8883bb1a..5c679d4bf4 100644 --- a/apps/site/lib/site_web/controllers/schedule/line_controller.ex +++ b/apps/site/lib/site_web/controllers/schedule/line_controller.ex @@ -2,6 +2,7 @@ defmodule SiteWeb.ScheduleController.LineController do use SiteWeb, :controller alias Phoenix.HTML alias Routes.{Group, Route} + alias Services.Repo, as: ServicesRepo alias Services.Service alias Site.ScheduleNote alias SiteWeb.{ScheduleView, ViewHelpers} @@ -42,7 +43,11 @@ defmodule SiteWeb.ScheduleController.LineController do def assign_schedule_page_data(conn) do service_date = Util.service_date() - services = Services.Repo.by_route_id(conn.assigns.route.id) + + services = + conn.assigns.route.id + |> ServicesRepo.by_route_id() + |> dedup_services() assign( conn, @@ -82,6 +87,17 @@ defmodule SiteWeb.ScheduleController.LineController do ) end + @spec dedup_services([Service.t()]) :: [Service.t()] + def dedup_services(services) do + services + |> Enum.group_by(fn %{start_date: start_date, end_date: end_date, valid_days: valid_days} -> + {start_date, end_date, valid_days} + end) + |> Enum.map(fn {_key, [service | _rest]} -> + service + end) + end + def sort_services_by_date(%Service{typicality: :typical_service, type: :weekday} = service) do {1, Date.to_string(service.start_date)} end diff --git a/apps/site/test/site_web/controllers/schedule/line_test.exs b/apps/site/test/site_web/controllers/schedule/line_test.exs index 67f40253f4..bcaf67d29f 100644 --- a/apps/site/test/site_web/controllers/schedule/line_test.exs +++ b/apps/site/test/site_web/controllers/schedule/line_test.exs @@ -453,4 +453,17 @@ defmodule SiteWeb.ScheduleController.LineTest do assert connection_ids == subway_cr_ids end end + + describe "services" do + test "remove duplicates", %{conn: conn} do + conn = + conn + |> put_resp_cookie("schedule_redesign", "true") + |> get(line_path(conn, :show, "39")) + + services = Services.Repo.by_route_id("39") + + assert length(conn.assigns.schedule_page_data.services) < services + end + end end