-
Notifications
You must be signed in to change notification settings - Fork 9
GCW-3407 Gogox Gem Integration and Transport APIs #1195
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
Open
swatijadhav
wants to merge
20
commits into
master
Choose a base branch
from
GCW-3407
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a20b5fb
GCW-3407 initial changes
swatijadhav aadd4af
GCW-3407 updated transport_providers yml
swatijadhav 95d857a
Merge branch 'master' into GCW-3407
swatijadhav 16bd4e5
GCW-3407 updated transport apis
swatijadhav f8d67e1
GCW-3407 updated static details
swatijadhav b07afd7
GCW-3407 updated gemfile.lock
swatijadhav 2836828
GCW-3407 updated routes
swatijadhav 3bfc95a
GCW-3407 addressed suggestions and updated specs
swatijadhav f25a776
GCW-3407 renamed model to TransportOrder
swatijadhav 39ab917
GCW-3407 updated service specs
swatijadhav 54a32fd
GCW-3407 updated specs
swatijadhav a50cbfe
Merge branch 'master' into GCW-3407
swatijadhav 40c6d28
GCW-3407 updated db schema
swatijadhav ec9d674
Apply suggestions from code review
swatijadhav 45c4788
GCW-3407 addressed PR comments
swatijadhav b9af1db
Merge branch 'GCW-3407' of github.com:crossroads/api.goodcity into GC…
swatijadhav ea321a2
Merge branch 'master' into GCW-3407
swatijadhav 0b3166f
GCW-3407 fixed gogox-service spec
swatijadhav e1d3860
GCW-3407 updated acceptance of providers in transport params
swatijadhav 3835482
GCW-3407 fixed test cases
swatijadhav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ gem 'easyzpl', git: 'https://github.com/crossroads/easyzpl.git' | |
| gem 'factory_bot_rails' # used in rake db:seed in production | ||
| gem 'fake_email_validator' | ||
| gem 'ffaker' | ||
| gem 'go_go_van_api', git: '[email protected]:crossroads/go_go_van_api.git', branch: 'master' | ||
| gem 'go_go_van_api', git: '[email protected]:crossroads/go_go_van_api.git', branch: 'GCW-3407' | ||
| gem 'guid' | ||
| gem 'http_accept_language' | ||
| gem 'jwt', '~> 2.2.2' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| GIT | ||
| remote: [email protected]:crossroads/go_go_van_api.git | ||
| revision: 3cbc5590e2a11a7903483de276bc668be88843cb | ||
| branch: master | ||
| revision: 7c0794f1c9eebcf0d44b8a4341f26ea8bdf259c9 | ||
| branch: GCW-3407 | ||
| specs: | ||
| go_go_van_api (0.0.1) | ||
| nestful (~> 1) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| module Api | ||
| module V1 | ||
| class TransportsController < Api::V1::ApiController | ||
|
|
||
| skip_authorization_check | ||
|
|
||
| api :GET, '/v1/transports/providers', "List all GoodCity Tranports Options." | ||
| def providers | ||
| render json: TransportProvider.all.cached_json | ||
| end | ||
|
|
||
| api :POST, '/v1/transports/quotation', "Get provider quotation" | ||
| param :provider, String, desc: "Provider selected for transport" | ||
| param :vehicle_type, String, desc: "Transport vehicle-type" | ||
| param :offer_id, String, desc: "Id of the offer" | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| param :schedule_at, String, desc: "Scheduled time for delivery" | ||
| param :district_id, String, desc: "Id of the district" | ||
| def quotation | ||
| order_price = TransportService.new(transport_params.to_h).quotation | ||
| render json: order_price.to_json | ||
| end | ||
|
|
||
| api :POST, '/v1/transports/quotation', "Book transport" | ||
| param :provider, String, desc: "Provider selected for transport" | ||
| param :vehicle_type, String, desc: "Transport vehicle-type" | ||
| param :offer_id, String, desc: "Id of the offer" | ||
| param :schedule_at, String, desc: "Scheduled time for delivery" | ||
| param :district_id, String, desc: "Id of the district" | ||
| param :pickup_contact_name, String, desc: "Contact Person Name" | ||
| param :pickup_contact_phone, String, desc: "Contact Person Mobile" | ||
| param :pickup_street_address, Hash, desc: "Pickup Address" | ||
| def book | ||
| order_info = TransportService.new(transport_params.to_h).book | ||
swatijadhav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| render json: order_info.to_json | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| api :GET, '/v1/transports/:id/order_details', "Get GoodCity Tranport order details." | ||
| def order_details | ||
| order_info = TransportService.new({booking_id: params[:id]}).status | ||
| render json: order_info.to_json | ||
| end | ||
|
|
||
| api :POST, '/v1/transports/:id/cancel_order', "Cancel GoodCity Tranport order." | ||
| def cancel_order | ||
| order_info = TransportService.new({booking_id: params[:id]}).cancel | ||
| render json: order_info.to_json | ||
| end | ||
|
|
||
| api :POST, '/v1/transports/update_gogox_order', "Webhook to update GOGOX order status" | ||
| def update_gogox_order | ||
| # setup ngrok and inspect response | ||
| # response details are not yet available from Gogox Provider | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def transport_params | ||
| set_district_id unless params["district_id"].presence | ||
| params.permit([ | ||
| "scheduled_at", "district_id", "offer_id", "provider", "vehicle_type", | ||
| "pickup_street_address", "pickup_contact_name", "pickup_contact_phone" | ||
| ]) | ||
| end | ||
|
|
||
| def set_district_id | ||
| params["district_id"] = User.current_user.address.district_id | ||
| end | ||
|
|
||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class TransportProvider < ApplicationRecord | ||
| include CacheableJson | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| class TransportProviderOrder < ApplicationRecord | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module Api::V1 | ||
| class TransportProviderSerializer < ApplicationSerializer | ||
| attributes :id, :name, :logo, :description, :metadata | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| class Gogox | ||
swatijadhav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| attr_accessor :params, :time, :vehicle, :district_id | ||
|
|
||
| VEHICLE_TYPES = ["van", "mudou", "mudou9"] | ||
|
|
||
| def initialize(options = {}) | ||
| @params = options | ||
| @time = parse_pickup_time(options[:scheduled_at]) | ||
| @vehicle = options[:vehicle] | ||
| @district_id = options[:district_id] | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| # Rsponse | ||
| # { | ||
| # "uuid" => "2f859363-5c43-4fe2-9b91-6c6c43d610d2", | ||
| # "status" => "pending", | ||
| # "vehicle_type" => "van", | ||
| # "payment_method" => "prepaid_wallet", | ||
| # "courier" => {}, | ||
| # "pickup" => { | ||
| # "name" => "Swati J", | ||
| # "street_address" => "123", | ||
| # "floor_or_unit_number" => nil, | ||
| # "schedule_at" => 1609242940, | ||
| # "location" => {"lat" => 22.5029632, "lng" => 114.1277213}, | ||
| # "contact" => { | ||
| # "name" => "Swati J", | ||
| # "phone_number" => "+85251111113", | ||
| # "phone_extension" => nil | ||
| # } | ||
| # }, | ||
| # "destinations" => [{ | ||
| # "name" => "Steve Kenworthy", | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # "street_address" => "Castle Peak Rd (So Kwun Wat)", | ||
| # "floor_or_unit_number" => nil, | ||
| # "location" => {"lat" => 22.3748365, "lng" => 113.9931416}, | ||
| # "contact" => { | ||
| # "name" => "Steve Kenworthy", | ||
| # "phone_number" => "+85251111111" | ||
| # } | ||
| # }], | ||
| # "note_to_courier" => nil, | ||
| # "price" => {"amount" => 15000, "currency" => "HKD"}, | ||
| # "price_breakdown" => [{"key" => "fee", "amount" => 15000}] | ||
| # } | ||
| def book | ||
| GogoxApi::Transport.new(order_attributes).order | ||
| end | ||
|
|
||
| # Response: | ||
| # { | ||
| # "vehicle_type" => "van", | ||
| # "estimated_price" => {"amount" => 15000, "currency" => "HKD"}, | ||
| # "estimated_price_breakdown" => [{"key" => "fee", "amount" => 15000}] | ||
| # } | ||
| def quotation | ||
| GogoxApi::Transport.new(quotation_attributes).quotation | ||
| end | ||
|
|
||
| class << self | ||
|
|
||
| def transport_status(booking_id) | ||
| GogoxApi::Transport.new.status(booking_id) | ||
| end | ||
|
|
||
| # Response | ||
| # Response is nil on successful cancellation of GOGOX transport | ||
| def cancel_order(booking_id) | ||
| response = GogoxApi::Transport.new.cancel(booking_id) | ||
| if !response | ||
| { | ||
| order_uuid: booking_id, | ||
| status: "cancelled" | ||
| } | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| private | ||
|
|
||
| def order_attributes | ||
| { | ||
| 'vehicle_type': vehicle_type, | ||
| "pickup_location": params[:pickup_location], | ||
| "pickup_street_address": params[:pickup_street_address], | ||
| "scheduled_at": parse_time, | ||
| "pickup_contact_name": params[:pickup_contact_name], | ||
| "pickup_contact_phone": params[:pickup_contact_phone], | ||
| "destination_location": params[:destination_location], | ||
| "destination_street_address": params[:destination_street_address], | ||
| "destination_contact_name": params[:destination_contact_name], | ||
| "destination_contact_phone": params[:destination_contact_phone] | ||
| } | ||
| end | ||
|
|
||
| def quotation_attributes | ||
| { | ||
| 'vehicle_type': vehicle_type, | ||
| "scheduled_at": parse_time, | ||
| "pickup_location": params[:pickup_location], | ||
| "destination_location": params[:destination_location] | ||
| } | ||
| end | ||
|
|
||
| def vehicle_type | ||
| if vehicle.blank? || !VEHICLE_TYPES.include?(vehicle) | ||
| raise(ValueError, "vehicle should be from #{VEHICLE_TYPES.join(', ')}") | ||
| end | ||
| vehicle | ||
| end | ||
|
|
||
| def parse_pickup_time(time = nil) | ||
| return time if time.present? | ||
|
|
||
| # next available date within next 5 days | ||
| next_available_date = DateSet.new(5, 1).available_dates.first | ||
| (next_available_date.beginning_of_day + 12.hours) | ||
| end | ||
|
|
||
| def parse_time | ||
| DateTime.parse(@time.to_s).to_i | ||
| end | ||
|
|
||
| class ValueError < StandardError; end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of just value error defining here, create a error in errors.rb |
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| class TransportService | ||
|
|
||
| attr_accessor :provider_name, :params, :provider, :booking_id, :user, :district_id | ||
|
|
||
| def initialize(options={}) | ||
| @params = options | ||
| @provider_name = options && options["provider"] | ||
| @provider ||= Object::const_get(provider_name) | ||
swatijadhav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @booking_id = options && options["booking_id"] | ||
|
|
||
| fetch_user | ||
| fetch_district_id | ||
| end | ||
|
|
||
| def quotation | ||
| @provider.new(params).quotation | ||
| end | ||
|
|
||
| def book | ||
| response = @provider.new(params).book | ||
| storeOrderDetails(response) | ||
| end | ||
|
|
||
| def cancel | ||
| response = @provider.cancel_order(booking_id) | ||
| if response | ||
| updateOrderDetails({ | ||
| status: "cancelled", | ||
| order_uuid: booking_id | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| def status | ||
| response = @provider.transport_status(booking_id) | ||
| if response | ||
| updateOrderDetails({ | ||
| status: response["status"], | ||
| order_uuid: booking_id, | ||
| metadata: response | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def storeOrderDetails(response) | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| TransportProviderOrder.create( | ||
| transport_provider_id: TransportProvider.find_by(name: provider_name).try(:id), | ||
| order_uuid: response["uuid"], | ||
| status: response["status"], | ||
| schedule_at: response["pickup"]["schedule_at"], | ||
| metadata: response, | ||
| offer_id: @params["offer_id"] | ||
| ) | ||
| end | ||
|
|
||
| def updateOrderDetails(response) | ||
| order = TransportProviderOrder.find_by(order_uuid: response["order_uuid"]) | ||
| order.update_attributes(response) | ||
| end | ||
|
|
||
| def quotation_attributes | ||
| { | ||
| 'vehicle_type': @params["vehicle_type"], | ||
| "scheduled_at": @params["scheduled_at"], | ||
| "pickup_location": pickup_location, | ||
| "destination_location": destination_location | ||
| } | ||
| end | ||
|
|
||
| def pickup_location | ||
| pickup_district = District.find(@district_id) | ||
| [pickup_district.latitude, pickup_district.longitude] | ||
| end | ||
|
|
||
| # TODO: Update crossroads geolocation values | ||
| def destination_location | ||
| [22.3748365, 113.9931416] | ||
| end | ||
|
|
||
| # TODO: Change | ||
| def destination_street_address | ||
| "Castle Peak Rd (So Kwun Wat)" | ||
swatijadhav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| # TODO: Change | ||
| def destination_contact_name | ||
| "Steve Kenworthy" | ||
| end | ||
|
|
||
| # TODO: Change | ||
| def destination_contact_phone | ||
| "+85251111111" | ||
| end | ||
|
|
||
| def order_attributes | ||
| { | ||
| 'vehicle_type': vehicle_type, | ||
| "pickup_location": pickup_location, | ||
| "pickup_street_address": params[:pickup_street_address], | ||
| "scheduled_at": params[:schedule_at], | ||
| "pickup_contact_name": params[:pickup_contact_name] || @user.full_name, | ||
| "pickup_contact_phone": params[:pickup_contact_phone] || @user.mobile, | ||
| "destination_location": destination_location, | ||
| "destination_street_address": destination_street_address, | ||
| "destination_contact_name": destination_contact_name, | ||
| "destination_contact_phone": destination_contact_phone | ||
| } | ||
| end | ||
|
|
||
| def fetch_user | ||
| @user ||= if @params["user_id"].present? | ||
| User.find_by(id: @params["user_id"]) | ||
| else | ||
| User.current_user | ||
| end | ||
| end | ||
|
|
||
| def fetch_district_id | ||
| @district_id ||= @params["district_id"].presence || @user.address.district_id | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class CreateTransportProviders < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :transport_providers do |t| | ||
| t.string :name | ||
| t.string :logo | ||
| t.text :description | ||
| t.jsonb :metadata, default: '{}' | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20210111115250_create_transport_provider_orders.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class CreateTransportProviderOrders < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :transport_provider_orders do |t| | ||
| t.integer :transport_provider_id | ||
| t.string :order_uuid | ||
| t.string :status | ||
| t.datetime :scheduled_at | ||
| t.jsonb :metadata | ||
| t.integer :offer_id | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.