Class: TrackDriverWorker

Inherits:
ApplicationWorker show all
Includes:
ProgressStatus
Defined in:
app/workers/track_driver_worker.rb

Overview

typed: ignore

Instance Method Summary collapse

Methods included from ProgressStatus

#available_delivery_status, #convert_from_grab_as_symbol, #convert_from_lalamove, #convert_from_lalamove_as_symbol, #convert_from_reservation, #owner_delivery_progress

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(reservation_id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'app/workers/track_driver_worker.rb', line 9

def perform(reservation_id)
  return if AdminSetting.enable_track_driver.to_s != 'true'

  reservation = Reservation.find_by(id: reservation_id)
  order_id = reservation.driver&.order&.order_ref

  unless order_id.present?
    # try again if driver not found
    TrackDriverWorker.perform_in(rand(1..3).minutes, reservation.id)
    return false
  end

  # stop if delivery channel not courier partner
  return false unless DeliveryChannel::COURIER_PARTNERS.include?(reservation&.delivery_channel&.lib_class)

  @courier = reservation.delivery_channel.lib_class.constantize.new

  if order_id.blank?
    TrackDriverWorker.perform_in(rand(1..3).minutes, reservation_id)
    return
  end

  delivery_order = DeliveryChannel::OrderManager.new(reservation_id, { order_id: order_id })
  delivery_order.fetch_order_data

  order_detail = delivery_order.order_data

  if order_detail.present?
    finished_lalamove_status = [Driver::LALAMOVE_DELIVERED, Driver::LALAMOVE_EXPIRED, Driver::LALAMOVE_CANCELLED,
                                Driver::LALAMOVE_REJECTED]
    finished_grab_status = [Driver::GRAB_DELIVERED, Driver::GRAB_FAILED, Driver::GRAB_CANCELLED]

    if (finished_lalamove_status + finished_grab_status).include? order_detail['status']
      update_firebase_delivery_detail(reservation, order_detail['status'])
      return false
    end

    # if order_detail['status'] == 'PICKED_UP'
    # update_firebase_delivery_detail(reservation, order_detail['status'])
    # return TrackDriverWorker.perform_at(Time.zone.now + 1.minutes, reservation.id)
    # no need to do anything, courier doesn't give driver location
    # return
    # end

    driver_detail = order_detail['driver']
    driver_location = driver_detail['location']
    if driver_location.present?

      driver = reservation.driver
      driver.without_auditing do
        driver.update(lng: driver_location['lng'], lat: driver_location['lat'])
      end

      attr_driver = {
        lng: driver_location['lng'],
        lat: driver_location['lat'],
        name: driver_detail['name'],
        phone: driver_detail['phone'],
        plate_number: driver_detail['plate_number'],
        time_estimation: 0
      }

      result = update_time_estimation(reservation, attr_driver)
      attr_driver[:time_estimation] = result[:data][:time_estimation] if result
      update_firebase_delivery_detail(reservation, order_detail['status'], attr_driver)
    elsif order_detail['status'].present?
      update_firebase_delivery_detail(reservation, order_detail['status'])
    end
  end

  TrackDriverWorker.perform_in(rand(1..3).minutes, reservation.id)
end