From f3a64a0a2a72008e7f2b83974d7232d0358e2920 Mon Sep 17 00:00:00 2001 From: tmahtab Date: Thu, 2 Dec 2021 19:13:31 -0800 Subject: [PATCH] Fixed same day trip comparison when linking trips.(PAMF-775) --- .../ecolane_ambassador.rb | 68 ++++++++++++------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/app/services/external_api_ambassadors/ecolane_ambassador.rb b/app/services/external_api_ambassadors/ecolane_ambassador.rb index 75e5e5840..69c7b606d 100644 --- a/app/services/external_api_ambassadors/ecolane_ambassador.rb +++ b/app/services/external_api_ambassadors/ecolane_ambassador.rb @@ -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