Skip to content

Commit

Permalink
Fixed same day trip comparison when linking trips.(PAMF-775)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmahtab committed Dec 3, 2021
1 parent ad32dd3 commit f3a64a0
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions app/services/external_api_ambassadors/ecolane_ambassador.rb
Original file line number Diff line number Diff line change
Expand Up @@ -839,35 +839,53 @@ def link_trips
[:future, :past].each do |times|
if times == :future # Get all future trips
trips = @user.trips.selected.future
else # Get the 10 most recent past trps
trips = @user.trips.selected.past.limit(10).reverse
else # Get the most recent past 14 days of trips
trips = @user.trips.selected.past.past_14_days.reverse
end

trips.each_cons(2) do |trip, next_trip|

#If this is already a round trip, keep moving.
if trip.previous_trip or trip.next_trip
next
end

#Are these trips on the same day?
unless trip.trip_time.to_date == next_trip.trip_time.to_date
next
# Group trips on same day.
trips_by_date = trips.group_by {|trip| trip.trip_time.in_time_zone('UTC').to_date}
trips_by_date.each do |trip_date, same_day_trips|
# Reset links on existing trips, unless trip has been created directly in 1click.
same_day_trips.each do |trip|
if trip.previous_trip and !trip&.selected_itinerary&.booking&.created_in_1click
trip.previous_trip = nil
trip.save
end
end

#Does these trips have inverted origins/destinations?
unless trip.origin.lat == next_trip.destination.lat and trip.origin.lng == next_trip.destination.lng
next
end
unless trip.destination.lat == next_trip.origin.lat and trip.destination.lng == next_trip.origin.lng
next
end

#Ok these trips passed all the tests, combine them into one trip
next_trip.previous_trip = trip
next_trip.save

end #trips.each
# Compare combinations of same day trips.
same_day_trips.pluck(:id).combination(2).each do |trip_id, next_trip_id|
trip = Trip.find_by(id: trip_id)
next_trip = Trip.find_by(id: next_trip_id)

# If this is already a round trip, and if trip has been created directly in 1click,
# don't try to re-link. Otherwise, continue.
if (trip.previous_trip or trip.next_trip) and trip&.selected_itinerary&.booking&.created_in_1click
next
end

# Are these trips on the same day?
# The trip_time is saved in the database as Eastern time in a UTC data type.
# Therefore we need to specify time zone to return as UTC in order to get trip_time back as Eastern time.
unless trip.trip_time.in_time_zone('UTC').to_date == next_trip.trip_time.in_time_zone('UTC').to_date
next
end

#Does these trips have inverted origins/destinations?
unless trip.origin.lat == next_trip.destination.lat and trip.origin.lng == next_trip.destination.lng
next
end
unless trip.destination.lat == next_trip.origin.lat and trip.destination.lng == next_trip.origin.lng
next
end

#Ok these trips passed all the tests, combine them into one trip
next_trip.previous_trip = trip
next_trip.save

end #trips.each
end #trips_by_date.each
end #times.each
end #link_trips

Expand Down

0 comments on commit f3a64a0

Please sign in to comment.