Skip to content

Commit

Permalink
Merge from occ-pa-qa2 master
Browse files Browse the repository at this point in the history
  • Loading branch information
tmahtab committed Dec 5, 2019
2 parents 813394c + 5e79125 commit 923935c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app/controllers/api/v1/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ def create
ecolane_ambassador = EcolaneAmbassador.new({county: county, dob: dob, ecolane_id: ecolane_id})
@user = ecolane_ambassador.user
if @user

@user.sync
#Last Trip
last_trip = @user.trips.order('created_at').last
#If this is a round trip, return the first part instead of the last part
if last_trip and last_trip.previous_trip
last_trip = last_trip.previous_trip
end
if last_trip and last_trip.origin and last_trip.destination
last_origin = last_trip.origin.google_place_hash
last_destination = last_trip.destination.google_place_hash
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def future_trips(count=nil)
sync
trips.selected.future.limit(count)
end

# Returns an unordered collection of the traveler's waypoints
def waypoints
trips.waypoints
Expand Down
60 changes: 57 additions & 3 deletions app/services/external_api_ambassadors/ecolane_ambassador.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,17 @@ def sync
(arrayify(fetch_customer_orders(options).try(:with_indifferent_access).try(:[], :orders).try(:[], :order))).each do |order|
occ_trip_from_ecolane_trip order
end

# For trips that are round trips, make sure that they point to each other.
link_trips

end

# Books Trip (funding_source and sponsor must be specified)
def book
new_order
booking = new_order
sync
return booking
end

def cancel
Expand Down Expand Up @@ -367,7 +373,10 @@ def occ_trip_from_ecolane_trip eco_trip
booking = itinerary.booking
booking.update(occ_booking_hash(eco_trip))
if booking.status == "canceled"
itinerary.unselect
trip = itinerary.trip
trip.selected_itinerary = nil
trip.save
# For some reason itinerary.unselect doesn't work here.
end
booking.save
itinerary.update!(occ_itinerary_hash_from_eco_trip(eco_trip))
Expand Down Expand Up @@ -746,12 +755,57 @@ def arrayify thing
if thing.is_a? Array
return thing
else
return [thing]
if thing.nil?
return []
else
return [thing]
end
end
end

def yes_or_no value
value.to_bool ? true : false
end

# If we synced trips from Ecolane, we need to identify which trips are actually round trips.
# They are delivered to us as individual trips, and we will link them together.
# Linking these lets users cancel them as a group, it also lets us pre-fill the most recent addresses in the user interface
def link_trips

# First do the past trips, and then do the future 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
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
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 #times.each
end #link_trips

end

0 comments on commit 923935c

Please sign in to comment.