Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Fares): remove obsolete logic #2119

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions lib/dotcom_web/controllers/schedule/finder_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule DotcomWeb.ScheduleController.FinderApi do

use DotcomWeb, :controller

import DotcomWeb.ScheduleController.ScheduleApi, only: [format_time: 1, fares_for_service: 4]
import DotcomWeb.ScheduleController.ScheduleApi, only: [format_time: 1, fares_for_service: 3]

require Logger

Expand All @@ -20,10 +20,6 @@ defmodule DotcomWeb.ScheduleController.FinderApi do
alias Routes.Route
alias Schedules.{Schedule, Trip}

import DotcomWeb.ScheduleController.ScheduleApi, only: [format_time: 1, fares_for_service: 4]

require Logger

@predictions_repo Application.compile_env!(:dotcom, :repo_modules)[:predictions]
# How many seconds a departure is considered recent
@recent_departure_max_age 600
Expand Down Expand Up @@ -413,11 +409,9 @@ defmodule DotcomWeb.ScheduleController.FinderApi do

defp add_computed_fares_to_trip_info(trip_info, route) do
origin = List.first(trip_info.times)
trip = PredictedSchedule.trip(origin)
stop = PredictedSchedule.stop(origin)

fare_params = %{
trip: trip,
route: route,
origin: stop.id,
destination: trip_info.destination_id
Expand Down Expand Up @@ -449,7 +443,6 @@ defmodule DotcomWeb.ScheduleController.FinderApi do
defp compute_fare(fare_params) do
fares_for_service(
fare_params.route,
fare_params.trip,
fare_params.origin,
fare_params.destination
)
Expand Down
8 changes: 4 additions & 4 deletions lib/dotcom_web/controllers/schedule/schedule_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule DotcomWeb.ScheduleController.ScheduleApi do
|> Enum.map(
&Map.merge(
&1,
fares_for_service(origin.route, origin.trip, origin.stop.id, &1.stop.id)
fares_for_service(origin.route, origin.stop.id, &1.stop.id)
)
)
end
Expand Down Expand Up @@ -149,10 +149,10 @@ defmodule DotcomWeb.ScheduleController.ScheduleApi do
%{schedules: time_formatted_schedules, duration: duration}
end

@spec fares_for_service(map, map, String.t(), String.t()) :: map
def fares_for_service(route, trip, origin, destination) do
@spec fares_for_service(map, String.t(), String.t()) :: map
def fares_for_service(route, origin, destination) do
%{
price: route |> OneWay.recommended_fare(trip, origin, destination) |> Format.price(),
price: route |> OneWay.recommended_fare(origin, destination) |> Format.price(),
fare_link:
fare_link(
Route.type_atom(route.type),
Expand Down
19 changes: 9 additions & 10 deletions lib/dotcom_web/controllers/trip_plan_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,14 @@ defmodule DotcomWeb.TripPlanController do

defp leg_with_fares(%Leg{mode: %TransitDetail{}} = leg) do
route = @routes_repo.get(leg.mode.route_id)
trip = Schedules.Repo.trip(leg.mode.trip_id)
origin_id = leg.from.stop_id
destination_id = leg.to.stop_id

fares =
if Leg.fare_complete_transit_leg?(leg) do
recommended_fare = OneWay.recommended_fare(route, trip, origin_id, destination_id)
base_fare = OneWay.base_fare(route, trip, origin_id, destination_id)
reduced_fare = OneWay.reduced_fare(route, trip, origin_id, destination_id)
recommended_fare = OneWay.recommended_fare(route, origin_id, destination_id)
base_fare = OneWay.base_fare(route, origin_id, destination_id)
reduced_fare = OneWay.reduced_fare(route, origin_id, destination_id)

%{
highest_one_way_fare: base_fare,
Expand Down Expand Up @@ -339,13 +338,13 @@ defmodule DotcomWeb.TripPlanController do

defp highest_month_pass(
%Leg{
mode: %TransitDetail{route_id: route_id, trip_id: trip_id},
mode: %TransitDetail{route_id: route_id},
from: %NamedPosition{stop_id: origin_id},
to: %NamedPosition{stop_id: destination_id}
} = leg
) do
if Leg.fare_complete_transit_leg?(leg) do
Month.base_pass(route_id, trip_id, origin_id, destination_id)
Month.base_pass(route_id, origin_id, destination_id)
else
nil
end
Expand All @@ -356,13 +355,13 @@ defmodule DotcomWeb.TripPlanController do

defp lowest_month_pass(
%Leg{
mode: %TransitDetail{route_id: route_id, trip_id: trip_id},
mode: %TransitDetail{route_id: route_id},
from: %NamedPosition{stop_id: origin_id},
to: %NamedPosition{stop_id: destination_id}
} = leg
) do
if Leg.fare_complete_transit_leg?(leg) do
Month.recommended_pass(route_id, trip_id, origin_id, destination_id)
Month.recommended_pass(route_id, origin_id, destination_id)
else
nil
end
Expand All @@ -373,13 +372,13 @@ defmodule DotcomWeb.TripPlanController do

defp reduced_pass(
%Leg{
mode: %TransitDetail{route_id: route_id, trip_id: trip_id},
mode: %TransitDetail{route_id: route_id},
from: %NamedPosition{stop_id: origin_id},
to: %NamedPosition{stop_id: destination_id}
} = leg
) do
if Leg.fare_complete_transit_leg?(leg) do
Month.reduced_pass(route_id, trip_id, origin_id, destination_id)
Month.reduced_pass(route_id, origin_id, destination_id)
else
nil
end
Expand Down
91 changes: 8 additions & 83 deletions lib/fares/fares.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Fares do
"""

alias Routes.Route
alias Schedules.Trip
alias Stops.Stop

@routes_repo Application.compile_env!(:dotcom, :repo_modules)[:routes]
Expand All @@ -18,11 +17,6 @@ defmodule Fares do
@express_routes ~w(170 325 326 352 354 426 428 434 450 459 501 502 503 504 505)
@express_route_set MapSet.new(@express_routes)

@foxboro_reverse_commute ~w(741 743 745 750 752 754 756)
@foxboro_reverse_commute_set MapSet.new(@foxboro_reverse_commute)

@terminus_stops ["place-sstat", "place-north", "North Station", "South Station"]

# For the time being, these stops are ONLY served by the Winthrop Ferry
@winthrop_ferry_stops ["Boat-Aquarium", "Boat-Fan", "Boat-Quincy", "Boat-Winthrop"]

Expand All @@ -41,47 +35,30 @@ defmodule Fares do

@doc """
Calculate the fare between a pair of stops.

NB: origin and destination can be Stop IDs or names.
"""
@spec fare_for_stops(
:commuter_rail | :ferry,
String.t(),
String.t(),
String.t() | Trip.t() | nil
Stop.id_t(),
Stop.id_t()
) ::
{:ok, Fares.Fare.fare_name()}
| :error
def fare_for_stops(route_type_atom, origin, destination, trip_details \\ nil)

def fare_for_stops(:commuter_rail, origin, destination, trip_details) do
with origin_zone when not is_nil(origin_zone) <- zone_for_stop(origin),
dest_zone when not is_nil(dest_zone) <- zone_for_stop(destination) do
cond do
foxboro_pilot?(trip_details) ->
{:ok, calculate_foxboro_zones(origin_zone, dest_zone)}

combo_zone?(origin_zone) ->
{:ok, calculate_combo(origin_zone, dest_zone, destination)}

combo_zone?(dest_zone) ->
{:ok, calculate_combo(dest_zone, origin_zone, origin)}

true ->
{:ok, calculate_commuter_rail(origin_zone, dest_zone)}
end
def fare_for_stops(:commuter_rail, origin_id, destination_id) do
with origin_zone when not is_nil(origin_zone) <- zone_for_stop(origin_id),
dest_zone when not is_nil(dest_zone) <- zone_for_stop(destination_id) do
{:ok, calculate_commuter_rail(origin_zone, dest_zone)}
else
_ -> :error
end
end

def fare_for_stops(:ferry, origin, destination, _) do
def fare_for_stops(:ferry, origin, destination) do
{:ok, calculate_ferry(origin, destination)}
end

defp zone_for_stop(stop_id) do
case @stops_repo.get(stop_id) do
%{zone: zone} -> zone
%Stops.Stop{zone: zone} -> zone
_ -> nil
end
end
Expand All @@ -102,30 +79,6 @@ defmodule Fares do
{:interzone, "#{total_zones}"}
end

@spec calculate_combo(zone, zone, Stop.id_t()) :: {:interzone, binary} | {:zone, any}
defp calculate_combo(combo_zone, _other_zone, other_stop_id)
when other_stop_id in @terminus_stops do
{:zone, terminus_zone(combo_zone)}
end

defp calculate_combo(combo_zone, other_zone, _other_stop_id) do
general_combo_zone = general_zone(combo_zone)
general_other_zone = general_zone(other_zone)
calculate_commuter_rail(general_combo_zone, general_other_zone)
end

def calculate_foxboro_zones(start_zone, "1A") when start_zone != "1A" do
calculate_commuter_rail(start_zone, "1")
end

def calculate_foxboro_zones("1A", end_zone) when end_zone != "1A" do
calculate_commuter_rail("1", end_zone)
end

def calculate_foxboro_zones(start_zone, end_zone) do
calculate_commuter_rail(start_zone, end_zone)
end

@spec calculate_ferry(String.t(), String.t()) :: ferry_name
defp calculate_ferry(origin, destination)
when "Boat-George" in [origin, destination] do
Expand Down Expand Up @@ -171,12 +124,6 @@ defmodule Fares do
:commuter_ferry
end

@spec foxboro_pilot?(Trip.t() | nil) :: boolean
defp foxboro_pilot?(%Trip{name: id, id: "CR-Weekday-Fall-19" <> _}),
do: id in @foxboro_reverse_commute_set

defp foxboro_pilot?(_), do: false

@spec silver_line_rapid_transit?(Route.id_t()) :: boolean
def silver_line_rapid_transit?(<<id::binary>>),
do: id in @silver_line_rapid_transit_set
Expand Down Expand Up @@ -230,26 +177,4 @@ defmodule Fares do
Access.key(fare_type)
])
end

defp combo_zone?(zone), do: String.contains?(zone, "-")

defp terminus_zone(zone) do
if combo_zone?(zone) do
zone
|> String.split("-")
|> List.first()
else
zone
end
end

defp general_zone(zone) do
if combo_zone?(zone) do
zone
|> String.split("-")
|> List.last()
else
zone
end
end
end
Loading
Loading