Class: CheckDriverAvailabilityWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/check_driver_availability_worker.rb

Overview

typed: ignore This worker will alert our staff if Lalamove still can not find driver within X minutes, which X = Restaurant#call_driver_time_limit_duration

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(reservation_id) ⇒ Object



7
8
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
# File 'app/workers/check_driver_availability_worker.rb', line 7

def perform(reservation_id)
  reservation = Reservation.find_by id: reservation_id

  if reservation.blank? || reservation.address.blank? || reservation.driver.blank? || reservation.driver.order.present?
    return
  end

  BUSINESS_LOGGER.info('delivery: check driver availability',
                       reservation_id: reservation.id)

  order_id = reservation.driver&.order&.order_ref

  if order_id.blank?
    BUSINESS_LOGGER.info('delivery: check driver availability: invalid order',
                         reservation_id: reservation.id,
                         note: 'does not have order id')
    return false
  end

  order_detail = get_order_detail(reservation, order_id)
  if [Driver::LALAMOVE_FINDING_DRIVER, Driver::GRAB_FINDING_DRIVER].include?(order_detail['status'])
    restaurant = reservation.restaurant
    subject = "Finding driver timedout, Booking ID #{reservation.id}"
    body = "System has been waiting for #{restaurant.minute_before_delivery_time} minutes to find driver for booking ID #{reservation.id}"
    StaffMailer.notify_driver_error_staff(subject, body).deliver_later!

    msg = "Cannot Get Driver within #{restaurant.minute_before_delivery_time} minutes. Booking ID #{reservation.id}. #{reservation.date} at #{reservation.start_time_format}. #{restaurant.short_name_sms} (#{restaurant.id})"
    phones = AdminSetting.phone_receiver_when_failed_order_driver
    SmsWorker.new.perform(msg, phones, reservation.id)

    BUSINESS_LOGGER.info('delivery: check driver availability: can not find driver for long time',
                         reservation_id: reservation.id)
  else
    BUSINESS_LOGGER.info('delivery: check driver availability: status is not "FINDING DRIVER"',
                         reservation_id: reservation.id)
  end
end