Class: DeliveryChannel::Grab

Inherits:
CourierBase show all
Defined in:
app/my_lib/delivery_channel/grab.rb

Overview

typed: ignore

Defined Under Namespace

Classes: RetryRequest, TooManyRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CourierBase

country_to_iso, #destroy_reservation_driver, #find_delivery_channel, generate_delivery_time, #order_model, #reset_delivery_channel, #self_deliver?, #should_reorder_driver, #skip_reorder_driver

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(service_type = 'INSTANT') ⇒ Grab

Returns a new instance of Grab.



5
6
7
# File 'app/my_lib/delivery_channel/grab.rb', line 5

def initialize(service_type = 'INSTANT')
  @service_type = service_type
end

Instance Attribute Details

#service_typeObject

Returns the value of attribute service_type.



3
4
5
# File 'app/my_lib/delivery_channel/grab.rb', line 3

def service_type
  @service_type
end

Instance Method Details

#cancel_order(order_id) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/my_lib/delivery_channel/grab.rb', line 125

def cancel_order(order_id)
  errors.clear
  path = "/v1/deliveries/#{order_id}"
  http_verb = 'DELETE'

  headers = build_headers(path, http_verb, {})
  url = hostname + path
  response = api_call(http_verb, url, nil, headers)

  return false if errors.present?

  response
end

#cancel_order_from_reservation_later(reservation, skip_reorder: default_skip_reorder_value) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'app/my_lib/delivery_channel/grab.rb', line 169

def cancel_order_from_reservation_later(reservation, skip_reorder: default_skip_reorder_value)
  return false if reservation.delivery_channel.blank?

  skip_reorder_driver(reservation) if skip_reorder

  order_id = reservation.driver&.order&.order_ref
  DeliveryChannels::CancelOrderWorker.perform_async(reservation.id, order_id, force_cancel: true,
                                                                              skip_reorder: skip_reorder)
  true
end

#driver_detail(order_id, _driver_id) ⇒ Object



147
148
149
150
151
152
153
154
# File 'app/my_lib/delivery_channel/grab.rb', line 147

def driver_detail(order_id, _driver_id)
  errors.clear
  response = order_detail(order_id)
  courier_detail = response['courier']
  return false if errors.present?

  courier_detail
end

#force_cancel_order(reservation, skip_reorder: default_skip_reorder_value) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/my_lib/delivery_channel/grab.rb', line 156

def force_cancel_order(reservation, skip_reorder: default_skip_reorder_value)
  ActiveRecord::Base.transaction do
    destroy_reservation_driver(reservation)
    reset_delivery_channel(reservation)
    skip_reorder_driver(reservation) if skip_reorder

    MyPusher::ReservationDriver.broadcast_reservation(reservation, :destroy)
    # MyMsgBus::ReservationDriver.broadcast_info(reservation)

    true
  end
end


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/my_lib/delivery_channel/grab.rb', line 9

def generate_tracking_link(reservation)
  order_ref = reservation.driver&.order_ref.presence || false
  if order_ref.present?
    link = Rails.cache.fetch("grab:generate_tracking_link:#{reservation.cache_key}:#{order_ref}") do
      detail = order_detail(order_ref)
      detail['trackingURL'] if detail.present? && (detail['status'] != 'FAILED')
    end
    return link
  end
  ''
rescue StandardError => e
  APMErrorHandler.report(e)
  ''
end

#get_quotation_from_payload(payload) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/my_lib/delivery_channel/grab.rb', line 33

def get_quotation_from_payload(payload)
  errors.clear

  path = '/v1/deliveries/quotes'
  http_verb = 'POST'
  headers = build_headers(path, http_verb, payload)
  url = hostname + path
  response = api_call(http_verb, url, payload, headers)

  return false if errors.present?

  response
end

#get_quotation_from_reservation(reservation) ⇒ Object



24
25
26
27
28
29
30
31
# File 'app/my_lib/delivery_channel/grab.rb', line 24

def get_quotation_from_reservation(reservation)
  errors.clear

  return false unless valid_reservation? reservation

  payload = quotes_payload(reservation).to_json
  get_quotation_from_payload(payload)
end

#order_detail(order_id) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/my_lib/delivery_channel/grab.rb', line 111

def order_detail(order_id)
  errors.clear
  path = "/v1/deliveries/#{order_id}"
  http_verb = 'GET'

  headers = build_headers(path, http_verb, {})
  url = hostname + path
  response = api_call(http_verb, url, nil, headers)

  return false if errors.present?

  response
end

#place_an_order_from_reservation(reservation, _send_sms = true) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/my_lib/delivery_channel/grab.rb', line 47

def place_an_order_from_reservation(reservation, _send_sms = true)
  errors.clear
  return false unless valid_reservation? reservation

  ActiveRecord::Base.transaction do
    restaurant = reservation.restaurant
    address = reservation.address
    reservation.delivery_channel = find_delivery_channel
    reservation.property.courier_called_at = Time.now_in_tz(restaurant.time_zone)
    reservation.save!
    reservation.build_driver.save! if reservation.driver.blank?

    note = if restaurant.delivery_note.present?
             format(restaurant.delivery_note.to_s, order_id: reservation.id, customer_name: reservation.name,
                                                   customer_phone: fix_phone(reservation.phone))
           else
             format(reservation.delivery_channel.delivery_note.to_s, order_id: reservation.id,
                                                                     customer_name: reservation.name, customer_phone: fix_phone(reservation.phone))
           end
    note = note.to_s + address.note_for_driver.to_s
    note += ",contact name: #{address.contact_name} " if address.contact_name.present?
    note += ",phone: #{address.contact_number} " if address.contact_number.present?

    payload = reservation_to_payload(reservation, note: note).to_json

    path = '/v1/deliveries'
    http_verb = 'POST'
    headers = build_headers(path, http_verb, payload)
    url = hostname + path
    response = api_call(http_verb, url, payload, headers)

    if errors.present?
      raise ActiveRecord::Rollback
      return false
    else
      response
    end
  end
end

#place_an_order_from_reservation_later(reservation, service_type) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/my_lib/delivery_channel/grab.rb', line 87

def place_an_order_from_reservation_later(reservation, service_type)
  ActiveRecord::Base.transaction do
    if valid_reservation?(reservation)
      delivery_channel = DeliveryChannel.fetch_by_lib_class self.class.to_s
      reservation.update! delivery_channel: delivery_channel
      reservation.build_driver(status: Driver::FINDING_DRIVER).save!

      config = {
        service_type: service_type,
        lib_class: self.class.to_s,
      }
      DeliveryChannels::CreateOrderWorker.perform_async(reservation.id, config)
      MyPusher::ReservationDriver.broadcast_reservation(reservation, :update)
      # MyMsgBus::ReservationDriver.broadcast_info(reservation)
      true
    else
      reservation.errors.add :base, error_message_simple
      MyPusher::ReservationDriver.broadcast_reservation(reservation, :update)
      # MyMsgBus::ReservationDriver.broadcast_info(reservation)
      false
    end
  end
end

#valid_reservation?(reservation) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
# File 'app/my_lib/delivery_channel/grab.rb', line 139

def valid_reservation?(reservation)
  errors.clear
  payload = reservation_to_payload(reservation)
  valid_reservation_helper(nil, payload)

  errors.blank?
end