Skip to content

Commit

Permalink
feat: show blocking alerts on the timetable even if they're low priority
Browse files Browse the repository at this point in the history
  • Loading branch information
paulswartz committed Jan 9, 2025
1 parent f0f8678 commit 8ff8ca0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/dotcom_web/templates/schedule/_timetable.html.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= DotcomWeb.AlertView.group(alerts: @alerts, route: @route, date_time: @date_time, priority_filter: :high) %>
<%= DotcomWeb.AlertView.group(alerts: @alerts, route: @route, date_time: @date_time, priority_filter: :high, always_show: List.wrap(@blocking_alert)) %>
<%= render "_trip_view_filters.html", assigns %>

<div class="calendar-covered m-timetable">
Expand Down
25 changes: 11 additions & 14 deletions lib/dotcom_web/views/alert_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule DotcomWeb.AlertView do
alias Routes.Route
alias Stops.Stop

@type priority_filter :: :any | Alerts.Priority.priority_level()

@doc """
Used to render a group of alerts.
Expand All @@ -28,6 +30,7 @@ defmodule DotcomWeb.AlertView do
opts
|> Keyword.fetch!(:alerts)
|> Enum.filter(&filter_by_priority(priority_filter, &1))
|> then(&(Keyword.get(opts, :always_show, []) ++ &1))
|> deduplicate()

case {alerts, show_empty?} do
Expand All @@ -46,17 +49,8 @@ defmodule DotcomWeb.AlertView do
end
end

# Workaround handling duplicate Red Line alerts for JFK-Ashmont shuttle
defp deduplicate(alerts) do
alert_ids = Enum.map(alerts, & &1.id)
ashmont_shuttle_alert_ids = ["519314", "529291"]

if Enum.all?(ashmont_shuttle_alert_ids, &Enum.member?(alert_ids, &1)) do
# remove the second one
Enum.reject(alerts, &(&1.id == "529291"))
else
alerts
end
Enum.uniq_by(alerts, & &1.id)
end

@spec no_alerts_message(map, boolean, atom) :: iolist
Expand Down Expand Up @@ -129,14 +123,17 @@ defmodule DotcomWeb.AlertView do
" at this time."
]

@spec filter_by_priority(boolean, Alert.t()) :: boolean
defp filter_by_priority(:any, _), do: true
@spec filter_by_priority(
priority_filter,
Alert.t()
) :: boolean
defp filter_by_priority(:any, _alert), do: true

defp filter_by_priority(priority_filter, %{priority: priority})
when priority_filter == priority,
when is_atom(priority_filter) and priority_filter == priority,
do: true

defp filter_by_priority(_, _), do: false
defp filter_by_priority(_filter, _alert), do: false

def effect_name(%{lifecycle: lifecycle} = alert)
when lifecycle in [:new, :unknown] do
Expand Down
70 changes: 69 additions & 1 deletion test/dotcom_web/views/alert_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ defmodule DotcomWeb.AlertViewTest do

test "text for an CR alert which is blocking the timetable (but has a PDF available)" do
alert = %Alerts.Alert{
header: "Blocked timetable. View PDF Timetable on the MBTA website.",
header: "Blocked timetable. #{Dotcom.TimetableBlocking.pdf_available_text()}",
url: "https://www.mbta.com/pdf-timetable",
updated_at: DateTime.utc_now()
}
Expand All @@ -208,6 +208,74 @@ defmodule DotcomWeb.AlertViewTest do
assert text =~ ~s["https://www.mbta.com/pdf-timetable"]
assert text =~ ">View PDF Timetable</a>"
end

test "shows a blocking alert even if it's low priority" do
alert =
Alerts.Alert.new(
header: "Blocked timetable. #{Dotcom.TimetableBlocking.no_pdf_text()}",
priority: :low,
updated_at: DateTime.utc_now(),
informed_entity: [%Alerts.InformedEntity{route: @route.id}],
active_period: [{~U[2025-01-01T00:00:00Z], nil}]
)

response =
group(
alerts: [alert],
route: @route,
date_time: DateTime.utc_now(),
priority_filter: :high,
always_show: [alert]
)

text = safe_to_string(response)
assert text =~ "Blocked timetable."
end

test "shows a blocking alert only once" do
alert =
Alerts.Alert.new(
header: "Blocked timetable. #{Dotcom.TimetableBlocking.no_pdf_text()}",
priority: :low,
updated_at: DateTime.utc_now(),
informed_entity: [%Alerts.InformedEntity{route: @route.id}],
active_period: [{~U[2025-01-01T00:00:00Z], nil}]
)

response =
group(
alerts: [alert],
route: @route,
date_time: DateTime.utc_now(),
always_show: [alert]
)

text = safe_to_string(response)
# only one copy of the alert
assert [_] = Regex.scan(~r/Blocked timetable/, text)
end

test "does not show a blocking alert on the wrong date" do
alert =
Alerts.Alert.new(
header: "Blocked timetable. #{Dotcom.TimetableBlocking.no_pdf_text()}",
priority: :low,
updated_at: DateTime.utc_now(),
informed_entity: [%Alerts.InformedEntity{route: @route.id}],
active_period: [{~U[2025-02-02T00:00:00Z], nil}]
)

response =
group(
alerts: [alert],
route: @route,
date_time: DateTime.utc_now(),
priority_filter: :high,
always_show: []
)

assert response == ""
end
end

describe "no_alerts_message/3" do
Expand Down

0 comments on commit 8ff8ca0

Please sign in to comment.