Class: DeliveryChannels::ChangeOrderWorker

Inherits:
ApplicationWorker
  • Object
show all
Defined in:
app/workers/delivery_channels/change_order_worker.rb

Overview

typed: ignore

Instance Method Summary collapse

Instance Method Details

#from_grab_to_lalamove(service_type) ⇒ Object



69
70
71
72
73
# File 'app/workers/delivery_channels/change_order_worker.rb', line 69

def from_grab_to_lalamove(service_type)
  return 'CAR' if service_type != 'INSTANT'

  'MOTORCYCLE'
end

#from_lalamove_to_grab(service_type) ⇒ Object



63
64
65
66
67
# File 'app/workers/delivery_channels/change_order_worker.rb', line 63

def from_lalamove_to_grab(service_type)
  return 'BULK' if service_type != 'MOTORCYCLE'

  'INSTANT'
end

#perform(reservation_id, order_id, service_type) ⇒ Object



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
# File 'app/workers/delivery_channels/change_order_worker.rb', line 10

def perform(reservation_id, order_id, service_type)
  reservation = Reservation.find(reservation_id)
  restaurant = reservation.restaurant
  restaurant_delivery_channel = restaurant.city_delivery_channel

  if restaurant_delivery_channel.present?
    raise(InvalidDriver,
          'Can not change courier type because restaurant has specific delivery channel')
  end

  city_delivery_channels = restaurant.city&.city_delivery_channels
  other_delivery_channel = city_delivery_channels.where.not(id: reservation.delivery_channel_id).first
  if other_delivery_channel.blank? || reservation.delivery_channel_id == other_delivery_channel&.delivery_channel_id
    raise(InvalidDriver, 'there is no another alternative for the same city')
  end

  delivery_channel = other_delivery_channel.delivery_channel
  config = {}
  config[:skip_reorder] = true
  config[:order_id] = order_id
  delivery_order = DeliveryChannel::OrderManager.new(reservation_id, config)
  delivery_order.fetch_order_data
  order_detail = delivery_order.order_data

  unless [Driver::LALAMOVE_FINDING_DRIVER, Driver::GRAB_FINDING_DRIVER].include? order_detail['status']
    raise(InvalidDriver, "status is not finding driver. current status is #{order_detail}")
  end

  raise(InvalidDriver, 'failed cancel existing courier') unless delivery_order.cancel_order

  lib_class_before = reservation.delivery_channel.lib_class
  new_service_type = if lib_class_before == 'DeliveryChannel::Lalamove'
                       from_lalamove_to_grab(service_type)
                     elsif service_type == 'DeliveryChannel::Grab'
                       from_grab_to_lalamove(service_type)
                     else
                       raise NotImplementedError
                     end

  config = {
    service_type: new_service_type,
    lib_class: delivery_channel.lib_class,
  }

  BUSINESS_LOGGER.info('delivery: change order',
                       reservation_id: reservation_id,
                       note: "scheduled to call new driver. old order id is #{order_id}")
  DeliveryChannels::CreateOrderWorker.perform_async(reservation.id, config)
rescue InvalidDriver => e
  BUSINESS_LOGGER.error('delivery: error change order', note: e.message, reservation_id: reservation_id)
  false
end