Skip to content

Commit

Permalink
Schedule Finder: remove duplicate services (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-mahoney authored Jul 22, 2019
1 parent cdb5505 commit 53beaf2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
18 changes: 17 additions & 1 deletion apps/site/lib/site_web/controllers/schedule/line_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions apps/site/test/site_web/controllers/schedule/line_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 53beaf2

Please sign in to comment.