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

Refactor trip request to trip #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion fleeto_backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ gem 'dotenv-rails'
gem 'active_model_serializers', '~> 0.10.0'
gem 'delayed_job_active_record'
gem "pundit"

gem "rubycritic", :require => false

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
53 changes: 53 additions & 0 deletions fleeto_backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,33 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
arel (7.1.4)
ast (2.3.0)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
bcrypt (3.1.11)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (9.0.6)
codeclimate-engine-rb (0.4.0)
virtus (~> 1.0)
coderay (1.1.1)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
delayed_job (4.1.2)
activesupport (>= 3.0, < 5.1)
delayed_job_active_record (4.1.1)
activerecord (>= 3.0, < 5.1)
delayed_job (>= 3.0, < 5)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
devise (4.2.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand All @@ -70,6 +83,7 @@ GEM
dotenv-rails (2.1.1)
dotenv (= 2.1.1)
railties (>= 4.0, < 5.1)
equalizer (0.0.11)
erubis (2.7.0)
factory_girl (4.7.0)
activesupport (>= 3.0.0)
Expand All @@ -79,12 +93,24 @@ GEM
faker (1.6.6)
i18n (~> 0.5)
ffi (1.9.14)
flay (2.8.1)
erubis (~> 2.7.0)
path_expander (~> 1.0)
ruby_parser (~> 3.0)
sexp_processor (~> 4.0)
flog (4.4.0)
path_expander (~> 1.0)
ruby_parser (~> 3.1, > 3.1.0)
sexp_processor (~> 4.4)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
ice_nine (0.11.2)
json (1.8.3)
jsonapi (0.1.1.beta2)
json (~> 1.8)
launchy (2.4.3)
addressable (~> 2.3)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -103,12 +129,16 @@ GEM
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
orm_adapter (0.5.0)
parser (2.3.3.1)
ast (~> 2.2)
path_expander (1.0.1)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
public_suffix (2.0.5)
puma (3.6.2)
pundit (1.1.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -143,13 +173,30 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.2.1)
rake (11.3.0)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
redis (3.3.2)
reek (4.5.3)
codeclimate-engine-rb (~> 0.4.0)
parser (~> 2.3.1, >= 2.3.1.2)
rainbow (~> 2.0)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
ruby_parser (3.8.3)
sexp_processor (~> 4.1)
rubycritic (3.1.3)
flay (~> 2.8)
flog (~> 4.4)
launchy (= 2.4.3)
parser (= 2.3.3.1)
rainbow (~> 2.1)
reek (~> 4.4)
ruby_parser (~> 3.8)
virtus (~> 1.0)
sexp_processor (4.7.0)
simplecov (0.12.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -177,6 +224,11 @@ GEM
activesupport (>= 3)
railties (>= 3)
yard (~> 0.7)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
warden (1.2.6)
rack (>= 1.0)
websocket-driver (0.6.4)
Expand Down Expand Up @@ -206,6 +258,7 @@ DEPENDENCIES
rails (~> 5.0.0, >= 5.0.0.1)
rails-controller-testing
redis (~> 3.0)
rubycritic
simplecov
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
10 changes: 0 additions & 10 deletions fleeto_backend/app/channels/trip_requests_channel.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def distance_matrix_to_drop_off_location

def distance_to_arriving_driver
trip = @current_customer.current_trip
if trip.try(:status) == Trip::WAITING_FOR_DRIVER
if trip.try(:status) == Trip::WAITING_FOR_DRIVER_ARRIVAL
driver = trip.driver
customer_location = Customers::LocationService.new(@current_customer).get_location
driver_location = Drivers::LocationService.new(driver).get_location
Expand All @@ -30,7 +30,7 @@ def distance_to_arriving_driver

def locate_near_drivers
customer_location = Customers::LocationService.new(current_customer).get_location
drivers_information = Drivers::LocationService.get_drivers_locations_within(params[:distance] || TripRequest::DISTANCE_IN_KM_TO_SEARCH_DRIVERS_WITHIN, customer_location)
drivers_information = Drivers::LocationService.get_drivers_locations_within(params[:distance] || Trip::DISTANCE_IN_KM_TO_SEARCH_DRIVERS_WITHIN, customer_location)
drivers_locations = drivers_information.select{|driver_info| !driver_info[:driver].in_a_trip?}.map{|driver_info| driver_info[:location]}

servicClasss = Map::Factory.new_service_adaptor(:distance_matrix)
Expand Down

This file was deleted.

18 changes: 17 additions & 1 deletion fleeto_backend/app/controllers/customer/v1/trips_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
class Customer::V1::TripsController < Customer::V1::BaseController
before_action :set_trip, only: [:show, :feedback]

def create
customer_location = Customers::LocationService.new(current_customer).get_location
@trip = Customers::Trips::CreateService.new(current_customer, trip_params.merge(from_latitude: customer_location[:latitude], from_longitude: customer_location[:longitude])).execute
if @trip.persisted?
Customers::Trips::SendToNearDriversService.new(@trip).execute
render json: "Trip created succesfully", status: :created
else
render json: @trip.errors.full_messages.to_sentence, status: :unprocessable_entity
end
end

def show
authorize @trip
render json: @trip, serializer: Customer::TripSerializer
end

Expand All @@ -20,6 +32,10 @@ def feedback
private

def set_trip
@trip = current_user.trips.find(params[:id])
@trip = Trip.find(params[:id])
end

def trip_params
params[:trip].permit(:to_latitude, :to_longitude)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Driver::V1::LocationsController < Driver::V1::BaseController

def set_location
Drivers::LocationService.new(current_driver).set_location(longitude: params[:longitude], latitude: params[:latitude])
if current_driver.in_a_trip? && current_driver.current_trip.waiting_for_driver?
if current_driver.in_a_trip? && current_driver.current_trip.WAITING_FOR_DRIVER_ARRIVAL?
current_trip = current_driver.current_trip
driver_location = Drivers::LocationService.new(current_driver).get_location
customer_location = Customers::LocationService.new(current_trip.customer).get_location
Expand Down

This file was deleted.

11 changes: 9 additions & 2 deletions fleeto_backend/app/controllers/driver/v1/trips_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
class Driver::V1::TripsController < Driver::V1::BaseController
before_action :set_trip, only: [:show]
before_action :set_trip, only: [:show, :accept]

def accept
authorize @trip
Drivers::Trips::AcceptService.new(@current_driver, @trip).execute
head :no_content
end

def show
authorize @trip
render json: @trip, serializer: Driver::TripSerializer
end

Expand All @@ -12,6 +19,6 @@ def index
private

def set_trip
@trip = current_user.trips.find(params[:id])
@trip = Trip.find(params[:id])
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Customers::SendArrivingDriverInfoJob < ApplicationJob
queue_as :drivers_trip_requests
queue_as :drivers_trips

def perform(customer_id, driver_id, arriving_duration)
customer = Customer.find(customer_id)
Expand Down
13 changes: 13 additions & 0 deletions fleeto_backend/app/jobs/customers/send_trip_event_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Customers::SendTripEventJob < ApplicationJob
queue_as :drivers_trips

def perform(customer_id, trip_id, event)
customer = Customer.find(customer_id)
trip = Trip.find(trip_id)
CustomersChannel.broadcast_to(customer, {
key: event,
data: trip.as_json(only: [:id, :from_latitude, :from_longitude, :to_latitude, :to_longitude, :status])
})
end

end
13 changes: 0 additions & 13 deletions fleeto_backend/app/jobs/customers/send_trip_request_event_job.rb

This file was deleted.

13 changes: 13 additions & 0 deletions fleeto_backend/app/jobs/drivers/send_trip_event_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Drivers::SendTripEventJob < ApplicationJob
queue_as :drivers_trips

def perform(driver_id, trip_id, event)
driver = Driver.find(driver_id)
trip = Trip.find(trip_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add extra empty line here.

DriversChannel.broadcast_to(driver, {
key: event,
data: trip.as_json(only: [:id, :from_latitude, :from_longitude, :to_latitude, :to_longitude, :status])
})
end

end
13 changes: 0 additions & 13 deletions fleeto_backend/app/jobs/drivers/send_trip_request_event_job.rb

This file was deleted.

8 changes: 4 additions & 4 deletions fleeto_backend/app/models/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Driver < ActiveRecord::Base
include DeviseTokenAuth::Concerns::User

has_many :trips
has_and_belongs_to_many :trip_requests
has_and_belongs_to_many :trips_notified_about, class_name: "Trip"

def current_trip
trips.where.not(status: Trip::COMPLETED).first
Expand All @@ -16,13 +16,13 @@ def in_a_trip?
trips.where.not(status: Trip::COMPLETED).any? # better not to use current trip as it will load the trip object
end

def has_pending_trip_request?
self.trip_requests.where(status: TripRequest::PENDING).any?
def has_pending_trip?
trips_notified_about.any?
end

def available?
# a driver may have only one pending trip request at a time
!(in_a_trip? || has_pending_trip_request?)
!(in_a_trip? || has_pending_trip?)
end

end
Loading