From 3f0a2f8feca0bf8b5bd507db3f5f16abe3e93b52 Mon Sep 17 00:00:00 2001 From: Loai Ghoraba Date: Thu, 5 Jan 2017 11:50:58 +0200 Subject: [PATCH 1/2] Refactor trip request to trip --- fleeto_backend/Gemfile | 2 +- fleeto_backend/Gemfile.lock | 53 +++++++++ .../app/channels/trip_requests_channel.rb | 10 -- .../customer/v1/locations_controller.rb | 4 +- .../customer/v1/trip_requests_controller.rb | 31 ------ .../customer/v1/trips_controller.rb | 18 ++- .../driver/v1/locations_controller.rb | 2 +- .../driver/v1/trip_requests_controller.rb | 16 --- .../controllers/driver/v1/trips_controller.rb | 11 +- .../send_arriving_driver_info_job.rb | 2 +- .../app/jobs/customers/send_trip_event_job.rb | 13 +++ .../customers/send_trip_request_event_job.rb | 13 --- .../app/jobs/drivers/send_trip_event_job.rb | 13 +++ .../drivers/send_trip_request_event_job.rb | 13 --- fleeto_backend/app/models/driver.rb | 8 +- fleeto_backend/app/models/trip.rb | 19 +++- fleeto_backend/app/models/trip_request.rb | 34 ------ fleeto_backend/app/policies/trip_policy.rb | 22 ++++ .../app/policies/trip_request_policy.rb | 18 --- .../serializers/customer/trip_serializer.rb | 2 +- .../app/serializers/driver/trip_serializer.rb | 2 +- .../customers/trip_requests/cancel_service.rb | 23 ---- .../send_to_near_drivers_service.rb | 23 ---- .../customers/trips/cancel_service.rb | 23 ++++ .../create_service.rb | 6 +- .../trips/send_to_near_drivers_service.rb | 23 ++++ .../drivers/trip_requests/accept_service.rb | 37 ------- .../services/drivers/trips/accept_service.rb | 29 +++++ fleeto_backend/config/routes.rb | 12 +- .../20161213133328_create_trip_requests.rb | 2 +- ...161215141550_add_status_to_trip_request.rb | 2 +- ...20161215141735_add_trip_request_to_trip.rb | 2 +- ...61219063545_create_drivers_trip_request.rb | 2 +- ...0103120018_merge_trip_request_and_trips.rb | 14 +++ fleeto_backend/db/schema.rb | 30 ++--- .../customer/v1/locations_controller_test.rb | 2 +- .../v1/trip_requests_controller_test.rb | 100 ----------------- .../customer/v1/trips_controller_test.rb | 103 ++++++++++++++++-- .../v1/trip_requests_controller_test.rb | 47 -------- .../driver/v1/trips_controller_test.rb | 44 +++++++- fleeto_backend/test/factories/trip.rb | 3 +- fleeto_backend/test/factories/trip_request.rb | 10 -- .../test/fixtures/trip_requests.yml | 15 --- .../test/models/trip_request_test.rb | 7 -- 44 files changed, 392 insertions(+), 473 deletions(-) delete mode 100644 fleeto_backend/app/channels/trip_requests_channel.rb delete mode 100644 fleeto_backend/app/controllers/customer/v1/trip_requests_controller.rb delete mode 100644 fleeto_backend/app/controllers/driver/v1/trip_requests_controller.rb create mode 100644 fleeto_backend/app/jobs/customers/send_trip_event_job.rb delete mode 100644 fleeto_backend/app/jobs/customers/send_trip_request_event_job.rb create mode 100644 fleeto_backend/app/jobs/drivers/send_trip_event_job.rb delete mode 100644 fleeto_backend/app/jobs/drivers/send_trip_request_event_job.rb delete mode 100644 fleeto_backend/app/models/trip_request.rb create mode 100644 fleeto_backend/app/policies/trip_policy.rb delete mode 100644 fleeto_backend/app/policies/trip_request_policy.rb delete mode 100644 fleeto_backend/app/services/customers/trip_requests/cancel_service.rb delete mode 100644 fleeto_backend/app/services/customers/trip_requests/send_to_near_drivers_service.rb create mode 100644 fleeto_backend/app/services/customers/trips/cancel_service.rb rename fleeto_backend/app/services/customers/{trip_requests => trips}/create_service.rb (53%) create mode 100644 fleeto_backend/app/services/customers/trips/send_to_near_drivers_service.rb delete mode 100644 fleeto_backend/app/services/drivers/trip_requests/accept_service.rb create mode 100644 fleeto_backend/app/services/drivers/trips/accept_service.rb create mode 100644 fleeto_backend/db/migrate/20170103120018_merge_trip_request_and_trips.rb delete mode 100644 fleeto_backend/test/controllers/customer/v1/trip_requests_controller_test.rb delete mode 100644 fleeto_backend/test/controllers/driver/v1/trip_requests_controller_test.rb delete mode 100644 fleeto_backend/test/factories/trip_request.rb delete mode 100644 fleeto_backend/test/fixtures/trip_requests.yml delete mode 100644 fleeto_backend/test/models/trip_request_test.rb diff --git a/fleeto_backend/Gemfile b/fleeto_backend/Gemfile index f1f9da9..39bbd32 100644 --- a/fleeto_backend/Gemfile +++ b/fleeto_backend/Gemfile @@ -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 diff --git a/fleeto_backend/Gemfile.lock b/fleeto_backend/Gemfile.lock index a43f9fe..68e208e 100644 --- a/fleeto_backend/Gemfile.lock +++ b/fleeto_backend/Gemfile.lock @@ -42,13 +42,24 @@ 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) @@ -56,6 +67,8 @@ GEM 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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/fleeto_backend/app/channels/trip_requests_channel.rb b/fleeto_backend/app/channels/trip_requests_channel.rb deleted file mode 100644 index 1225182..0000000 --- a/fleeto_backend/app/channels/trip_requests_channel.rb +++ /dev/null @@ -1,10 +0,0 @@ -class TripRequestsChannel < ApplicationCable::Channel - def subscribed - trip_request = TripRequest.find(params[:id]) - stream_for trip_request - end - - def unsubscribed - stop_all_streams - end -end diff --git a/fleeto_backend/app/controllers/customer/v1/locations_controller.rb b/fleeto_backend/app/controllers/customer/v1/locations_controller.rb index fc5a538..3c36436 100644 --- a/fleeto_backend/app/controllers/customer/v1/locations_controller.rb +++ b/fleeto_backend/app/controllers/customer/v1/locations_controller.rb @@ -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 @@ -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) diff --git a/fleeto_backend/app/controllers/customer/v1/trip_requests_controller.rb b/fleeto_backend/app/controllers/customer/v1/trip_requests_controller.rb deleted file mode 100644 index 634e6c6..0000000 --- a/fleeto_backend/app/controllers/customer/v1/trip_requests_controller.rb +++ /dev/null @@ -1,31 +0,0 @@ -class Customer::V1::TripRequestsController < Customer::V1::BaseController - - before_action :set_trip_request, only: [:cancel] - - def create - customer_location = Customers::LocationService.new(current_customer).get_location - @trip_request = Customers::TripRequests::CreateService.new(current_customer, trip_request_params.merge(from_latitude: customer_location[:latitude], from_longitude: customer_location[:longitude])).execute - if @trip_request.persisted? - Customers::TripRequests::SendToNearDriversService.new(@trip_request).execute - render json: "Trip request created succesfully", status: :created - else - render json: @trip_request.errors.full_messages.to_sentence, status: :unprocessable_entity - end - end - - def cancel - authorize @trip_request - Customers::TripRequests::CancelService.new(@trip_request).execute - head :no_content - end - - private - - def set_trip_request - @trip_request = TripRequest.find(params[:id]) - end - - def trip_request_params - params[:trip_request].permit(:to_latitude, :to_longitude) - end -end \ No newline at end of file diff --git a/fleeto_backend/app/controllers/customer/v1/trips_controller.rb b/fleeto_backend/app/controllers/customer/v1/trips_controller.rb index 7c875d6..44231bb 100644 --- a/fleeto_backend/app/controllers/customer/v1/trips_controller.rb +++ b/fleeto_backend/app/controllers/customer/v1/trips_controller.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/fleeto_backend/app/controllers/driver/v1/locations_controller.rb b/fleeto_backend/app/controllers/driver/v1/locations_controller.rb index 8f2c46f..b320e1a 100644 --- a/fleeto_backend/app/controllers/driver/v1/locations_controller.rb +++ b/fleeto_backend/app/controllers/driver/v1/locations_controller.rb @@ -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 diff --git a/fleeto_backend/app/controllers/driver/v1/trip_requests_controller.rb b/fleeto_backend/app/controllers/driver/v1/trip_requests_controller.rb deleted file mode 100644 index dc6c08e..0000000 --- a/fleeto_backend/app/controllers/driver/v1/trip_requests_controller.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Driver::V1::TripRequestsController < Driver::V1::BaseController - - before_action :set_trip_request, only: [:accept] - - def accept - authorize @trip_request - Drivers::TripRequests::AcceptService.new(@current_driver, @trip_request).execute - head :no_content - end - private - - def set_trip_request - @trip_request = TripRequest.find(params[:id]) - end - -end \ No newline at end of file diff --git a/fleeto_backend/app/controllers/driver/v1/trips_controller.rb b/fleeto_backend/app/controllers/driver/v1/trips_controller.rb index 1dbde3f..a03388c 100644 --- a/fleeto_backend/app/controllers/driver/v1/trips_controller.rb +++ b/fleeto_backend/app/controllers/driver/v1/trips_controller.rb @@ -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 @@ -12,6 +19,6 @@ def index private def set_trip - @trip = current_user.trips.find(params[:id]) + @trip = Trip.find(params[:id]) end end diff --git a/fleeto_backend/app/jobs/customers/send_arriving_driver_info_job.rb b/fleeto_backend/app/jobs/customers/send_arriving_driver_info_job.rb index ff41fc5..dbaa621 100644 --- a/fleeto_backend/app/jobs/customers/send_arriving_driver_info_job.rb +++ b/fleeto_backend/app/jobs/customers/send_arriving_driver_info_job.rb @@ -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) diff --git a/fleeto_backend/app/jobs/customers/send_trip_event_job.rb b/fleeto_backend/app/jobs/customers/send_trip_event_job.rb new file mode 100644 index 0000000..b2a5349 --- /dev/null +++ b/fleeto_backend/app/jobs/customers/send_trip_event_job.rb @@ -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 diff --git a/fleeto_backend/app/jobs/customers/send_trip_request_event_job.rb b/fleeto_backend/app/jobs/customers/send_trip_request_event_job.rb deleted file mode 100644 index c7de7f1..0000000 --- a/fleeto_backend/app/jobs/customers/send_trip_request_event_job.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Customers::SendTripRequestEventJob < ApplicationJob - queue_as :drivers_trip_requests - - def perform(customer_id, trip_request_id, event) - customer = Customer.find(customer_id) - trip_request = TripRequest.find(trip_request_id) - CustomersChannel.broadcast_to(customer, { - key: event, - data: trip_request.as_json(only: [:id, :from_latitude, :from_longitude, :to_latitude, :to_longitude, :status]) - }) - end - -end diff --git a/fleeto_backend/app/jobs/drivers/send_trip_event_job.rb b/fleeto_backend/app/jobs/drivers/send_trip_event_job.rb new file mode 100644 index 0000000..bfcc409 --- /dev/null +++ b/fleeto_backend/app/jobs/drivers/send_trip_event_job.rb @@ -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) + DriversChannel.broadcast_to(driver, { + key: event, + data: trip.as_json(only: [:id, :from_latitude, :from_longitude, :to_latitude, :to_longitude, :status]) + }) + end + +end diff --git a/fleeto_backend/app/jobs/drivers/send_trip_request_event_job.rb b/fleeto_backend/app/jobs/drivers/send_trip_request_event_job.rb deleted file mode 100644 index 4fba7af..0000000 --- a/fleeto_backend/app/jobs/drivers/send_trip_request_event_job.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Drivers::SendTripRequestEventJob < ApplicationJob - queue_as :drivers_trip_requests - - def perform(driver_id, trip_request_id, event) - driver = Driver.find(driver_id) - trip_request = TripRequest.find(trip_request_id) - DriversChannel.broadcast_to(driver, { - key: event, - data: trip_request.as_json(only: [:id, :from_latitude, :from_longitude, :to_latitude, :to_longitude, :status]) - }) - end - -end diff --git a/fleeto_backend/app/models/driver.rb b/fleeto_backend/app/models/driver.rb index ac0ee51..4db182c 100644 --- a/fleeto_backend/app/models/driver.rb +++ b/fleeto_backend/app/models/driver.rb @@ -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 @@ -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 diff --git a/fleeto_backend/app/models/trip.rb b/fleeto_backend/app/models/trip.rb index 18b8fd5..de394e2 100644 --- a/fleeto_backend/app/models/trip.rb +++ b/fleeto_backend/app/models/trip.rb @@ -1,13 +1,17 @@ class Trip < ApplicationRecord belongs_to :customer - belongs_to :driver - belongs_to :trip_request + belongs_to :driver # the driver who accepted the trip + + has_and_belongs_to_many :notified_drivers, class_name: "Driver" # all drivers who received the trip request has_one :customer_trip_feedback - WAITING_FOR_DRIVER, ONGOING, COMPLETED = STATUSES = %w(waiting_for_driver ongoing completed) + PENDING, WAITING_FOR_DRIVER_ARRIVAL, ONGOING, COMPLETED = STATUSES = %w(pending waiting_for_driver_arrival ongoing completed) + DISTANCE_IN_KM_TO_SEARCH_DRIVERS_WITHIN = 1000 enum_string :status, STATUSES validates :status, inclusion: STATUSES + validates :from_latitude, :from_longitude, presence: true + validate :customer_is_not_in_a_trip, on: :create FeedbackError = Class.new(StandardError) @@ -17,4 +21,13 @@ def create_customer_feedback(comment, rating) create_customer_trip_feedback!(comment: comment, rating: rating) end + + private + + def customer_is_not_in_a_trip + if self.customer.in_a_trip? + self.errors.add(:base, "Can't requset a trip when still in another trip") + end + end + end diff --git a/fleeto_backend/app/models/trip_request.rb b/fleeto_backend/app/models/trip_request.rb deleted file mode 100644 index e0a96de..0000000 --- a/fleeto_backend/app/models/trip_request.rb +++ /dev/null @@ -1,34 +0,0 @@ -class TripRequest < ApplicationRecord - belongs_to :customer - has_and_belongs_to_many :drivers - - has_one :trip - - validates :from_latitude, :from_longitude, presence: true - - validate :customer_is_not_in_a_trip - - PENDING, CANCELED, ACCEPTED, EXPIERED = STATUSES = %w(pending canceled accepted expired) - enum_string :status, STATUSES - - # TODO this should be configurable from the admin - DISTANCE_IN_KM_TO_SEARCH_DRIVERS_WITHIN = 1000 - - def accept! - update_columns(status: ACCEPTED) - end - - def cancel! - update_columns(status: CANCELED) - end - - private - - def customer_is_not_in_a_trip - if self.customer.in_a_trip? - self.errors.add(:base, "Can't requset a trip when still in another trip") - end - end - - -end diff --git a/fleeto_backend/app/policies/trip_policy.rb b/fleeto_backend/app/policies/trip_policy.rb new file mode 100644 index 0000000..74d30e3 --- /dev/null +++ b/fleeto_backend/app/policies/trip_policy.rb @@ -0,0 +1,22 @@ +class TripPolicy < ApplicationPolicy + + attr_reader :user, :trip + + def initialize(user, trip) + @user = user + @trip = trip + end + + # def cancel? + # trip.customer == user + # end + + def show? + trip.customer == @user || trip.driver == @user + end + + def accept? + driver = @user + trip.pending? && trip.notified_drivers.where(id: driver.id).any? + end +end diff --git a/fleeto_backend/app/policies/trip_request_policy.rb b/fleeto_backend/app/policies/trip_request_policy.rb deleted file mode 100644 index f8f4a5c..0000000 --- a/fleeto_backend/app/policies/trip_request_policy.rb +++ /dev/null @@ -1,18 +0,0 @@ -class TripRequestPolicy < ApplicationPolicy - - attr_reader :user, :trip_request - - def initialize(user, trip_request) - @user = user - @trip_request = trip_request - end - - def cancel? - trip_request.customer == user - end - - def accept? - driver = user - trip_request.pending? && driver.trip_requests.where(id: trip_request.id).any? - end -end diff --git a/fleeto_backend/app/serializers/customer/trip_serializer.rb b/fleeto_backend/app/serializers/customer/trip_serializer.rb index 7e32cb5..ba236d8 100644 --- a/fleeto_backend/app/serializers/customer/trip_serializer.rb +++ b/fleeto_backend/app/serializers/customer/trip_serializer.rb @@ -1,3 +1,3 @@ class Customer::TripSerializer < ActiveModel::Serializer - attributes :id, :driver_id, :from_long, :from_lat, :to_long, :to_lat, :started_at, :ended_at, :status + attributes :id, :driver_id, :from_longitude, :from_latitude, :to_longitude, :to_latitude, :started_at, :ended_at, :status end \ No newline at end of file diff --git a/fleeto_backend/app/serializers/driver/trip_serializer.rb b/fleeto_backend/app/serializers/driver/trip_serializer.rb index 7ff50c7..3a8cd28 100644 --- a/fleeto_backend/app/serializers/driver/trip_serializer.rb +++ b/fleeto_backend/app/serializers/driver/trip_serializer.rb @@ -1,3 +1,3 @@ class Driver::TripSerializer < ActiveModel::Serializer - attributes :id, :customer_id, :from_long, :from_lat, :to_long, :to_lat, :started_at, :ended_at, :status + attributes :id, :customer_id, :from_longitude, :from_latitude, :to_longitude, :to_latitude, :started_at, :ended_at, :status end \ No newline at end of file diff --git a/fleeto_backend/app/services/customers/trip_requests/cancel_service.rb b/fleeto_backend/app/services/customers/trip_requests/cancel_service.rb deleted file mode 100644 index bf4bd14..0000000 --- a/fleeto_backend/app/services/customers/trip_requests/cancel_service.rb +++ /dev/null @@ -1,23 +0,0 @@ -class Customers::TripRequests::CancelService - - attr_accessor :customer, :trip_request - - def initialize(trip_request) - @trip_request = trip_request - end - - - def execute - @trip_request.cancel! - notify_drivers - end - - - private - - def notify_drivers - @trip_request.drivers.each do |driver| - Drivers::SendTripRequestEventJob.perform_later(driver.id, trip_request.id, "trip_request_canceled") - end - end -end \ No newline at end of file diff --git a/fleeto_backend/app/services/customers/trip_requests/send_to_near_drivers_service.rb b/fleeto_backend/app/services/customers/trip_requests/send_to_near_drivers_service.rb deleted file mode 100644 index 2382e6b..0000000 --- a/fleeto_backend/app/services/customers/trip_requests/send_to_near_drivers_service.rb +++ /dev/null @@ -1,23 +0,0 @@ -class Customers::TripRequests::SendToNearDriversService - - attr_accessor :trip_request - - def initialize(trip_request) - @trip_request = trip_request - end - - def execute - from_location = { - longitude: @trip_request.from_longitude, - latitude: @trip_request.from_latitude - } - drivers_information = Drivers::LocationService.get_drivers_locations_within(TripRequest::DISTANCE_IN_KM_TO_SEARCH_DRIVERS_WITHIN, from_location) - drivers = drivers_information.select{|driver_info| driver_info[:driver].available?}.map{|driver_info| driver_info[:driver]} - drivers.each do |driver| - @trip_request.drivers << driver - Drivers::SendTripRequestEventJob.perform_later(driver.id, @trip_request.id, "trip_request_created") - end - end - -end - diff --git a/fleeto_backend/app/services/customers/trips/cancel_service.rb b/fleeto_backend/app/services/customers/trips/cancel_service.rb new file mode 100644 index 0000000..cf68098 --- /dev/null +++ b/fleeto_backend/app/services/customers/trips/cancel_service.rb @@ -0,0 +1,23 @@ +class Customers::Trips::CancelService + + attr_accessor :customer, :trip + + def initialize(trip) + @trip = trip + end + + + def execute + @trip.cancel! + notify_drivers + end + + + private + + def notify_drivers + @trip.drivers.each do |driver| + Drivers::SendTripEventJob.perform_later(driver.id, trip.id, "trip_canceled") + end + end +end \ No newline at end of file diff --git a/fleeto_backend/app/services/customers/trip_requests/create_service.rb b/fleeto_backend/app/services/customers/trips/create_service.rb similarity index 53% rename from fleeto_backend/app/services/customers/trip_requests/create_service.rb rename to fleeto_backend/app/services/customers/trips/create_service.rb index 7e805c8..253d3ab 100644 --- a/fleeto_backend/app/services/customers/trip_requests/create_service.rb +++ b/fleeto_backend/app/services/customers/trips/create_service.rb @@ -1,4 +1,4 @@ -class Customers::TripRequests::CreateService +class Customers::Trips::CreateService attr_accessor :customer, :params @@ -8,8 +8,8 @@ def initialize(customer, params) end def execute - trip_request = TripRequest.create(params.merge(customer: customer)) - trip_request + trip = Trip.create(params.merge(customer: customer, status: Trip::PENDING)) + trip end end \ No newline at end of file diff --git a/fleeto_backend/app/services/customers/trips/send_to_near_drivers_service.rb b/fleeto_backend/app/services/customers/trips/send_to_near_drivers_service.rb new file mode 100644 index 0000000..2207f6c --- /dev/null +++ b/fleeto_backend/app/services/customers/trips/send_to_near_drivers_service.rb @@ -0,0 +1,23 @@ +class Customers::Trips::SendToNearDriversService + + attr_accessor :trip + + def initialize(trip) + @trip = trip + end + + def execute + from_location = { + longitude: @trip.from_longitude, + latitude: @trip.from_latitude + } + drivers_information = Drivers::LocationService.get_drivers_locations_within(Trip::DISTANCE_IN_KM_TO_SEARCH_DRIVERS_WITHIN, from_location) + drivers = drivers_information.select{|driver_info| driver_info[:driver].available?}.map{|driver_info| driver_info[:driver]} + drivers.each do |driver| + @trip.notified_drivers << driver + Drivers::SendTripEventJob.perform_later(driver.id, @trip.id, "trip_created") + end + end + +end + diff --git a/fleeto_backend/app/services/drivers/trip_requests/accept_service.rb b/fleeto_backend/app/services/drivers/trip_requests/accept_service.rb deleted file mode 100644 index ee651e2..0000000 --- a/fleeto_backend/app/services/drivers/trip_requests/accept_service.rb +++ /dev/null @@ -1,37 +0,0 @@ -class Drivers::TripRequests::AcceptService - - attr_accessor :driver, :trip_request - - def initialize(driver, trip_request) - @driver = driver - @trip_request = trip_request - end - - def execute - @trip_request.accept! - Trip.create!( - customer: @trip_request.customer, - driver: @driver, - trip_request: @trip_request, - from_longitude: @trip_request.from_longitude, - from_latitude: @trip_request.from_latitude, - status: Trip::WAITING_FOR_DRIVER - ) - - notify_other_drivers - notify_customer - end - - private - - def notify_other_drivers - @trip_request.drivers.where.not(id: driver.id).each do |driver| - Drivers::SendTripRequestEventJob.perform_later(driver.id, trip_request.id, "trip_request_accepted") - end - end - - def notify_customer - customer = trip_request.customer - Customers::SendTripRequestEventJob.perform_later(customer.id, trip_request.id, "trip_request_accepted") - end -end \ No newline at end of file diff --git a/fleeto_backend/app/services/drivers/trips/accept_service.rb b/fleeto_backend/app/services/drivers/trips/accept_service.rb new file mode 100644 index 0000000..e054f51 --- /dev/null +++ b/fleeto_backend/app/services/drivers/trips/accept_service.rb @@ -0,0 +1,29 @@ +class Drivers::Trips::AcceptService + + attr_accessor :driver, :trip + + def initialize(driver, trip) + @driver = driver + @trip = trip + end + + def execute + @trip.update(status: Trip::WAITING_FOR_DRIVER_ARRIVAL, driver: @driver) + + notify_other_drivers + notify_customer + end + + private + + def notify_other_drivers + @trip.notified_drivers.where.not(id: driver.id).each do |driver| + Drivers::SendTripEventJob.perform_later(driver.id, trip.id, "trip_accepted") + end + end + + def notify_customer + customer = trip.customer + Customers::SendTripEventJob.perform_later(customer.id, trip.id, "trip_accepted") + end +end \ No newline at end of file diff --git a/fleeto_backend/config/routes.rb b/fleeto_backend/config/routes.rb index 5ebd5ab..98d8bc1 100644 --- a/fleeto_backend/config/routes.rb +++ b/fleeto_backend/config/routes.rb @@ -28,13 +28,7 @@ end end - resources :trip_requests, only: [:create] do - member do - post :cancel - end - end - - resources :trips, only: [:index, :show] do + resources :trips, only: [:index, :create, :show] do member do post :feedback end @@ -57,13 +51,11 @@ end end - resources :trip_requests, only: [] do + resources :trips, only: [:index, :show] do member do post :accept end end - - resources :trips, only: [:index, :show] end end diff --git a/fleeto_backend/db/migrate/20161213133328_create_trip_requests.rb b/fleeto_backend/db/migrate/20161213133328_create_trip_requests.rb index aa47b6b..8cba04e 100644 --- a/fleeto_backend/db/migrate/20161213133328_create_trip_requests.rb +++ b/fleeto_backend/db/migrate/20161213133328_create_trip_requests.rb @@ -1,4 +1,4 @@ -class CreateTripRequests < ActiveRecord::Migration[5.0] +class CreateTrips < ActiveRecord::Migration[5.0] def change create_table :trip_requests do |t| t.belongs_to :customer, index: true diff --git a/fleeto_backend/db/migrate/20161215141550_add_status_to_trip_request.rb b/fleeto_backend/db/migrate/20161215141550_add_status_to_trip_request.rb index 293355d..0b9e725 100644 --- a/fleeto_backend/db/migrate/20161215141550_add_status_to_trip_request.rb +++ b/fleeto_backend/db/migrate/20161215141550_add_status_to_trip_request.rb @@ -1,4 +1,4 @@ -class AddStatusToTripRequest < ActiveRecord::Migration[5.0] +class AddStatusToTrip < ActiveRecord::Migration[5.0] def change add_column :trip_requests, :status, :string end diff --git a/fleeto_backend/db/migrate/20161215141735_add_trip_request_to_trip.rb b/fleeto_backend/db/migrate/20161215141735_add_trip_request_to_trip.rb index c35b204..44c007a 100644 --- a/fleeto_backend/db/migrate/20161215141735_add_trip_request_to_trip.rb +++ b/fleeto_backend/db/migrate/20161215141735_add_trip_request_to_trip.rb @@ -1,4 +1,4 @@ -class AddTripRequestToTrip < ActiveRecord::Migration[5.0] +class AddTripToTrip < ActiveRecord::Migration[5.0] def change add_reference :trips, :trip_request, index: true end diff --git a/fleeto_backend/db/migrate/20161219063545_create_drivers_trip_request.rb b/fleeto_backend/db/migrate/20161219063545_create_drivers_trip_request.rb index 2630e90..a5e2f95 100644 --- a/fleeto_backend/db/migrate/20161219063545_create_drivers_trip_request.rb +++ b/fleeto_backend/db/migrate/20161219063545_create_drivers_trip_request.rb @@ -1,4 +1,4 @@ -class CreateDriversTripRequest < ActiveRecord::Migration[5.0] +class CreateDriversTrip < ActiveRecord::Migration[5.0] def change create_table :drivers_trip_requests do |t| t.belongs_to :trip_request, index: true diff --git a/fleeto_backend/db/migrate/20170103120018_merge_trip_request_and_trips.rb b/fleeto_backend/db/migrate/20170103120018_merge_trip_request_and_trips.rb new file mode 100644 index 0000000..4ae2b06 --- /dev/null +++ b/fleeto_backend/db/migrate/20170103120018_merge_trip_request_and_trips.rb @@ -0,0 +1,14 @@ +class MergeTripAndTrips < ActiveRecord::Migration[5.0] + def change + remove_index :trips, :trip_request_id + drop_table :trip_requests + drop_table :drivers_trip_requests + + create_table :drivers_trips do |t| + t.belongs_to :trip, index: true + t.belongs_to :driver, index: true + + t.timestamps + end + end +end diff --git a/fleeto_backend/db/schema.rb b/fleeto_backend/db/schema.rb index 003f95d..4c0ea25 100644 --- a/fleeto_backend/db/schema.rb +++ b/fleeto_backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161226090648) do +ActiveRecord::Schema.define(version: 20170103120018) do create_table "customer_trip_feedbacks", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.integer "trip_id" @@ -97,30 +97,17 @@ t.index ["uid", "provider"], name: "index_drivers_on_uid_and_provider", unique: true, using: :btree end - create_table "drivers_trip_requests", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| - t.integer "trip_request_id" + create_table "drivers_trips", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.integer "trip_id" t.integer "driver_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["driver_id"], name: "index_drivers_trip_requests_on_driver_id", using: :btree - t.index ["trip_request_id"], name: "index_drivers_trip_requests_on_trip_request_id", using: :btree - end - - create_table "trip_requests", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| - t.integer "customer_id" - t.float "from_latitude", limit: 24 - t.float "from_longitude", limit: 24 - t.float "to_latitude", limit: 24 - t.float "to_longitude", limit: 24 - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "status" - t.index ["customer_id"], name: "index_trip_requests_on_customer_id", using: :btree + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["driver_id"], name: "index_drivers_trips_on_driver_id", using: :btree + t.index ["trip_id"], name: "index_drivers_trips_on_trip_id", using: :btree end create_table "trips", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.integer "customer_id" - t.integer "driver_id" t.float "from_longitude", limit: 24 t.float "from_latitude", limit: 24 t.float "to_longitude", limit: 24 @@ -130,10 +117,9 @@ t.string "status" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "driver_id" t.integer "trip_request_id" t.index ["customer_id"], name: "index_trips_on_customer_id", using: :btree - t.index ["driver_id"], name: "index_trips_on_driver_id", using: :btree - t.index ["trip_request_id"], name: "index_trips_on_trip_request_id", using: :btree end add_foreign_key "customer_trip_feedbacks", "trips" diff --git a/fleeto_backend/test/controllers/customer/v1/locations_controller_test.rb b/fleeto_backend/test/controllers/customer/v1/locations_controller_test.rb index 92fee2e..a6decef 100644 --- a/fleeto_backend/test/controllers/customer/v1/locations_controller_test.rb +++ b/fleeto_backend/test/controllers/customer/v1/locations_controller_test.rb @@ -43,7 +43,7 @@ class Customer::V1::LocationsControllerTest < ActionDispatch::IntegrationTest customer_location = random_location Customers::LocationService.new(@current_customer).set_location(customer_location) - trip = create(:trip, status: Trip::WAITING_FOR_DRIVER,customer: @current_customer, from_longitude: customer_location[:longitude], from_latitude: customer_location[:latitude]) + trip = create(:trip, status: Trip::WAITING_FOR_DRIVER_ARRIVAL,customer: @current_customer, from_longitude: customer_location[:longitude], from_latitude: customer_location[:latitude]) driver = trip.driver driver_location = { diff --git a/fleeto_backend/test/controllers/customer/v1/trip_requests_controller_test.rb b/fleeto_backend/test/controllers/customer/v1/trip_requests_controller_test.rb deleted file mode 100644 index 97a6fbe..0000000 --- a/fleeto_backend/test/controllers/customer/v1/trip_requests_controller_test.rb +++ /dev/null @@ -1,100 +0,0 @@ -require 'test_helper' - - -class Customer::V1::TripRequestsControllerTest < ActionDispatch::IntegrationTest - include ActiveJob::TestHelper - - setup do - @current_customer = create(:customer, :verified) - @headers = sign_in(@current_customer) - end - - test "should create a trip request" do - trip_request = build(:trip_request) - - available_driver = create(:driver) - Drivers::LocationService.new(available_driver).set_location(longitude: trip_request.from_longitude, latitude: trip_request.from_latitude) - - driver_in_a_trip = create(:driver) - trip = create(:trip, status: Trip::ONGOING, driver: driver_in_a_trip) - Drivers::LocationService.new(driver_in_a_trip).set_location(longitude: trip_request.from_longitude, latitude: trip_request.from_latitude) - - driver_pending_trip_request = create(:driver) - pending_trip_request = create(:trip_request) - pending_trip_request.drivers << driver_pending_trip_request - Drivers::LocationService.new(driver_pending_trip_request).set_location(longitude: trip_request.from_longitude, latitude: trip_request.from_latitude) - - - assert_difference("TripRequest.count", 1) do - perform_enqueued_jobs do # to send request to drivers - post '/customer/v1/trip_requests', headers: @headers, params: { - trip_request: { - from_latitude: trip_request.from_latitude, - from_longitude: trip_request.from_longitude - } - } - end - end - - assert_response :created - - trip_request = assigns(:trip_request) - drivers_who_received_trip_request = trip_request.drivers - - assert drivers_who_received_trip_request.count == 1 - assert drivers_who_received_trip_request.first == available_driver - end - - test "should not create a trip request without mandatory fields" do - trip_request = build(:trip_request) - - assert_difference("TripRequest.count", 0) do - post '/customer/v1/trip_requests', headers: @headers, params: { - trip_request: { - dummy_params: "" - } - } - end - - assert_response :unprocessable_entity - end - - test "should not create a trip request when the customer is in another trip" do - create(:trip, customer: @current_customer, status: Trip::WAITING_FOR_DRIVER) - - trip_request = build(:trip_request) - assert_difference("TripRequest.count", 0) do - post '/customer/v1/trip_requests', headers: @headers, params: { - trip_request: { - from_latitude: trip_request.from_latitude, - from_longitude: trip_request.from_longitude - } - } - end - - assert_response :unprocessable_entity - end - - test "should cancel a trip request" do - trip_request = create(:trip_request, customer: @current_customer) - perform_enqueued_jobs do - post "/customer/v1/trip_requests/#{trip_request.id}/cancel", headers: @headers - end - - trip_request.reload - assert trip_request.canceled? - assert_response :no_content - end - - test "should not cancel a trip request when not authorized to" do - # only trip request customer(owner) can cancel it - trip_request = create(:trip_request) # creates a trip request with new customer - old_status = trip_request.status - post "/customer/v1/trip_requests/#{trip_request.id}/cancel", headers: @headers - trip_request.reload - - assert trip_request.status == old_status - assert_response :unauthorized - end - -end \ No newline at end of file diff --git a/fleeto_backend/test/controllers/customer/v1/trips_controller_test.rb b/fleeto_backend/test/controllers/customer/v1/trips_controller_test.rb index c3824b0..272b365 100644 --- a/fleeto_backend/test/controllers/customer/v1/trips_controller_test.rb +++ b/fleeto_backend/test/controllers/customer/v1/trips_controller_test.rb @@ -1,15 +1,16 @@ require 'test_helper' class Customer::V1::TripsControllerTest < ActionDispatch::IntegrationTest + include ActiveJob::TestHelper setup do - current_customer = create(:customer, :verified) - @headers = sign_in(current_customer) - @trip = create(:trip, customer: current_customer) - - @not_my_trip = create(:trip) + @current_customer = create(:customer, :verified) + @headers = sign_in(@current_customer) end test '#index' do + trip = create(:trip, customer: @current_customer) + not_my_trip = create(:trip) + get '/customer/v1/trips', headers: @headers assert_response :success @@ -17,28 +18,108 @@ class Customer::V1::TripsControllerTest < ActionDispatch::IntegrationTest json_response = JSON.parse(response.body) assert_not_empty json_response['trips'] - assert_equal(json_response['trips'].first['id'], @trip.id) + assert_equal(json_response['trips'].first['id'], trip.id) assert_not_includes( json_response['trips'].map{ |trip| trip['id'] }, - @not_my_trip.id + not_my_trip.id ) end + test "should create a trip request" do + trip = build(:trip) + Customers::LocationService.new(@current_customer).set_location(longitude: trip.from_longitude, latitude: trip.from_latitude) + available_driver = create(:driver) + Drivers::LocationService.new(available_driver).set_location(longitude: trip.from_longitude, latitude: trip.from_latitude) + + driver_in_a_trip = create(:driver) + trip = create(:trip, status: Trip::ONGOING, driver: driver_in_a_trip) + Drivers::LocationService.new(driver_in_a_trip).set_location(longitude: trip.from_longitude, latitude: trip.from_latitude) + + driver_pending_trip = create(:driver) + pending_trip = create(:trip, status: Trip::PENDING) + pending_trip.notified_drivers << driver_pending_trip + Drivers::LocationService.new(driver_pending_trip).set_location(longitude: trip.from_longitude, latitude: trip.from_latitude) + + assert_difference("Trip.count", 1) do + perform_enqueued_jobs do # to send request to drivers + post '/customer/v1/trips', headers: @headers, params: { + trip: { + from_latitude: trip.from_latitude, + from_longitude: trip.from_longitude + } + } + end + end + assert_response :created + + trip = assigns(:trip) + assert trip.pending? + + notified_drivers = trip.notified_drivers + + assert notified_drivers.count == 1 + assert notified_drivers.first == available_driver + end + + test "should not create a trip request when the customer is in another trip" do + create(:trip, customer: @current_customer, status: Trip::WAITING_FOR_DRIVER_ARRIVAL) + + trip = build(:trip) + assert_difference("Trip.count", 0) do + post '/customer/v1/trips', headers: @headers, params: { + trip: { + from_latitude: trip.from_latitude, + from_longitude: trip.from_longitude + } + } + end + + assert_response :unprocessable_entity + end + + # test "should cancel a trip request" do + # skip + # trip = create(:trip, customer: @current_customer) + # perform_enqueued_jobs do + # post "/customer/v1/trips/#{trip.id}/cancel", headers: @headers + # end + + # trip.reload + # assert trip.canceled? + # assert_response :no_content + # end + + # test "should not cancel a trip request when not authorized to" do + # skip + # # only trip request customer(owner) can cancel it + # trip = create(:trip) # creates a trip request with new customer + # old_status = trip.status + # post "/customer/v1/trips/#{trip.id}/cancel", headers: @headers + # trip.reload + + # assert trip.status == old_status + # assert_response :unauthorized + # end + + test '#show one of my trips' do - get "/customer/v1/trips/#{@trip.id}", headers: @headers + trip = create(:trip, customer: @current_customer) + + get "/customer/v1/trips/#{trip.id}", headers: @headers assert_response :success # TODO: Stop repeating me! json_response = JSON.parse(response.body) - assert_equal(json_response['trip']['id'], @trip.id) + assert_equal(json_response['trip']['id'], trip.id) end test '#show some trip that is not mine' do - get "/customer/v1/trips/#{@not_my_trip.id}", headers: @headers + not_my_trip = create(:trip) + get "/customer/v1/trips/#{not_my_trip.id}", headers: @headers - assert_response :not_found + assert_response :unauthorized end end \ No newline at end of file diff --git a/fleeto_backend/test/controllers/driver/v1/trip_requests_controller_test.rb b/fleeto_backend/test/controllers/driver/v1/trip_requests_controller_test.rb deleted file mode 100644 index e6a4fb6..0000000 --- a/fleeto_backend/test/controllers/driver/v1/trip_requests_controller_test.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'test_helper' - -class Drivers::V1::TripRequestsController < ActionDispatch::IntegrationTest - include ActiveJob::TestHelper - setup do - @current_driver = create(:driver) - @headers = sign_in(@current_driver) - end - - test "should accept a trip request" do - trip_request = create(:trip_request, status: TripRequest::PENDING) - trip_request.drivers << @current_driver - - assert_difference("Trip.count", 1) do - perform_enqueued_jobs do - post "/driver/v1/trip_requests/#{trip_request.id}/accept", headers: @headers - end - end - - assert_response :success - - trip_request.reload - assert trip_request.accepted? - - trip = trip_request.trip - assert trip.waiting_for_driver? - assert trip.driver == @current_driver - end - - test "should not accept a trip request if it is not pending" do - - end - - test "should not accept a trip request if it was not sent to current driver" do - trip_request = create(:trip_request, status: TripRequest::PENDING) - - assert_difference("Trip.count", 0) do - post "/driver/v1/trip_requests/#{trip_request.id}/accept", headers: @headers - end - - assert_response :unauthorized - - trip_request.reload - assert trip_request.pending? - - end -end \ No newline at end of file diff --git a/fleeto_backend/test/controllers/driver/v1/trips_controller_test.rb b/fleeto_backend/test/controllers/driver/v1/trips_controller_test.rb index 0cfe785..1f99e79 100644 --- a/fleeto_backend/test/controllers/driver/v1/trips_controller_test.rb +++ b/fleeto_backend/test/controllers/driver/v1/trips_controller_test.rb @@ -1,15 +1,16 @@ require 'test_helper' class Driver::V1::TripsControllerTest < ActionDispatch::IntegrationTest + include ActiveJob::TestHelper setup do - current_driver = create(:driver) - @headers = sign_in(current_driver) - @trip = create(:trip, driver: current_driver) - - @not_my_trip = create(:trip) + @current_driver = create(:driver) + @headers = sign_in(@current_driver) end test '#index' do + @trip = create(:trip, driver: @current_driver) + @not_my_trip = create(:trip) + get '/driver/v1/trips', headers: @headers assert_response :success @@ -25,7 +26,37 @@ class Driver::V1::TripsControllerTest < ActionDispatch::IntegrationTest ) end + test "should accept a trip request" do + trip = create(:trip, status: Trip::PENDING) + trip.notified_drivers << @current_driver + perform_enqueued_jobs do + post "/driver/v1/trips/#{trip.id}/accept", headers: @headers + end + + assert_response :success + + trip.reload + + assert trip.waiting_for_driver_arrival? + assert trip.driver == @current_driver + end + + test "should not accept a trip request if it is not pending" do + + end + + test "should not accept a trip request if it was not sent to current driver" do + trip = create(:trip, status: Trip::PENDING, driver: nil) + + post "/driver/v1/trips/#{trip.id}/accept", headers: @headers + assert_response :unauthorized + trip.reload + assert trip.pending? + + end + test '#show one of my trips' do + @trip = create(:trip, driver: @current_driver) get "/driver/v1/trips/#{@trip.id}", headers: @headers assert_response :success @@ -37,8 +68,9 @@ class Driver::V1::TripsControllerTest < ActionDispatch::IntegrationTest end test '#show some trip that is not mine' do + @not_my_trip = create(:trip) get "/driver/v1/trips/#{@not_my_trip.id}", headers: @headers - assert_response :not_found + assert_response :unauthorized end end \ No newline at end of file diff --git a/fleeto_backend/test/factories/trip.rb b/fleeto_backend/test/factories/trip.rb index 02db9b9..ae0f3e6 100644 --- a/fleeto_backend/test/factories/trip.rb +++ b/fleeto_backend/test/factories/trip.rb @@ -8,7 +8,6 @@ from_longitude {Faker::Address.longitude} to_latitude {Faker::Address.latitude} to_longitude {Faker::Address.longitude} - status {Trip::STATUSES.sample} - + status {Trip::PENDING} end end diff --git a/fleeto_backend/test/factories/trip_request.rb b/fleeto_backend/test/factories/trip_request.rb deleted file mode 100644 index ceba47f..0000000 --- a/fleeto_backend/test/factories/trip_request.rb +++ /dev/null @@ -1,10 +0,0 @@ -FactoryGirl.define do - - factory :trip_request do - - customer - from_latitude {Faker::Address.latitude} - from_longitude {Faker::Address.longitude} - status { TripRequest::PENDING} - end -end diff --git a/fleeto_backend/test/fixtures/trip_requests.yml b/fleeto_backend/test/fixtures/trip_requests.yml deleted file mode 100644 index f50ccf9..0000000 --- a/fleeto_backend/test/fixtures/trip_requests.yml +++ /dev/null @@ -1,15 +0,0 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - customer: one - from_lat: 1.5 - from_long: 1.5 - to_lat: 1.5 - to_long: 1.5 - -two: - customer: two - from_lat: 1.5 - from_long: 1.5 - to_lat: 1.5 - to_long: 1.5 diff --git a/fleeto_backend/test/models/trip_request_test.rb b/fleeto_backend/test/models/trip_request_test.rb deleted file mode 100644 index a5a920f..0000000 --- a/fleeto_backend/test/models/trip_request_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class TripRequestTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end From cd2d313a93947b5204d64f56f0e293c50d6c6535 Mon Sep 17 00:00:00 2001 From: Loai Ghoraba Date: Sun, 8 Jan 2017 14:41:49 +0200 Subject: [PATCH 2/2] refactor --- fleeto_mobile/screens/home.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fleeto_mobile/screens/home.js b/fleeto_mobile/screens/home.js index f9f5160..0e9bb46 100644 --- a/fleeto_mobile/screens/home.js +++ b/fleeto_mobile/screens/home.js @@ -152,7 +152,7 @@ export default class Home extends React.Component{ var headers = authHeaders; headers['Accept'] = 'application/json'; headers['Content-Type'] = 'application/json'; - return fetch(`${constants.BASE_URL}customer/v1/trip_requests`, { + return fetch(`${constants.BASE_URL}customer/v1/trips`, { method: "post", headers: headers }).then((response) => {