Class: Vendors::OpenRice::WebhookWorker

Inherits:
ApplicationWorker
  • Object
show all
Defined in:
app/workers/vendors/open_rice/webhook_worker.rb

Instance Method Summary collapse

Instance Method Details

#perform(reservation_id, vendor_reservation_id, status, date = nil, time = nil, adult = nil, child = nil, is_auto_arrive = false) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/workers/vendors/open_rice/webhook_worker.rb', line 8

def perform(reservation_id, vendor_reservation_id, status, date = nil, time = nil, adult = nil, child = nil,
            is_auto_arrive = false)

  reservation = Reservation.fetch(reservation_id)

  current_restaurant_time = Time.use_zone reservation.restaurant.time_zone do
    Time.zone.now
  end

  RequestStore.store[:reservation_id] ||= reservation_id
  RequestStore.store[:restaurant_id] ||= reservation.restaurant_id
  case status
  when ApiVendorV1::Constants::UPDATE
    wait_until_reservation_status_has_changed(reservation_id, [:pending_arrival, :pending_confirmation])
    VendorsService::OpenRice::WebhookService.new(reservation_id, vendor_reservation_id, status).
      update_reservation_status(date, time, adult, child)
  when ApiVendorV1::Constants::CONFIRM
    if is_auto_arrive
      return VendorsService::OpenRice::WebhookService.new(reservation_id, vendor_reservation_id,
                                                          status).auto_arrive_reservation
    end

    # OpenRice is allow CONFIRM status to be sent 24 hours after the reservation time
    if current_restaurant_time < reservation.reservation_time.twenty_four_hours_later
      ::Vendors::OpenRice::WebhookWorker.
        perform_at(reservation.reservation_time.twenty_four_hours_later + 1.hour,
                   reservation_id, vendor_reservation_id, status, nil, nil, nil, nil, false)
    end

    # Prevent sending CONFIRM status duplication to OpenRice webhook
    return if reservation.vendor_reservation.webhook_reservation_status == status

    wait_until_reservation_status_has_changed(reservation_id, [:arrived])
    VendorsService::OpenRice::WebhookService.new(reservation_id, vendor_reservation_id,
                                                 status).update_reservation_status
  when ApiVendorV1::Constants::NO_SHOW
    # OpenRice is allow NO_SHOW status to be sent 24 hours after the reservation time
    if current_restaurant_time < reservation.reservation_time.twenty_four_hours_later
      ::Vendors::OpenRice::WebhookWorker.
        perform_at(reservation.reservation_time.twenty_four_hours_later + 1.hour,
                   reservation_id, vendor_reservation_id, status)
    end

    # Prevent sending NO_SHOW status duplication to OpenRice webhook
    return if reservation.vendor_reservation.webhook_reservation_status == status

    wait_until_reservation_status_has_changed(reservation_id, [:no_show])
    VendorsService::OpenRice::WebhookService.new(reservation_id, vendor_reservation_id,
                                                 status).update_reservation_status
  when ApiVendorV1::Constants::CANCEL
    # Prevent sending CANCEL status duplication to OpenRice webhook
    return if reservation.vendor_reservation.webhook_reservation_status == status

    wait_until_reservation_status_has_changed(reservation_id, [:cancelled, :rejected])
    VendorsService::OpenRice::WebhookService.new(reservation_id, vendor_reservation_id,
                                                 status).update_reservation_status
  else
    raise NotImplementedError
  end
end

#wait_until_reservation_status_has_changed(reservation_id, status) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/workers/vendors/open_rice/webhook_worker.rb', line 69

def wait_until_reservation_status_has_changed(reservation_id, status)
  retry_times = 0
  loop do
    if status.include?(Reservation.find(reservation_id).status_as_symbol)
      break
    else
      retry_times += 1
      if retry_times == 100
        raise 'Unknown reservation status'
      end

      sleep 1
    end
  end
end