Class: Vendors::TagThai::WebhookWorker

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

Instance Method Summary collapse

Instance Method Details

#perform(reservation_id, status, is_auto_arrive = false, vendor_reservation_id = nil) ⇒ 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
# File 'app/workers/vendors/tag_thai/webhook_worker.rb', line 8

def perform(reservation_id, status, is_auto_arrive = false, vendor_reservation_id = nil)
  BUSINESS_LOGGER.set_business_context({ vendor_name: ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME,
                                         reservation_id: reservation_id })
  start_time = Time.zone.now
  RequestStore.store[:reservation_id] ||= reservation_id
  RequestStore.store[:restaurant_id] ||= Reservation.find_by(id: reservation_id)&.restaurant_id

  case status
  when ApiVendorV1::Constants::WAITING_FOR_PAYMENT
    wait_until_reservation_status_has_changed(reservation_id, [:waiting_for_payment])
    result = VendorsService::TagThai::WebhookService.new(reservation_id, status).send_waiting_payment
  when ApiVendorV1::Constants::PENDING_ARRIVAL
    wait_until_reservation_status_has_changed(reservation_id, [:pending_arrival, :pending_confirmation])
    result = VendorsService::TagThai::WebhookService.new(reservation_id, status,
                                                         vendor_reservation_id).send_pending_arrival
  when ApiVendorV1::Constants::CONFIRM
    if is_auto_arrive
      result = VendorsService::TagThai::WebhookService.new(reservation_id, status).auto_arrive_reservation
    else
      wait_until_reservation_status_has_changed(reservation_id, [:arrived])
      result = VendorsService::TagThai::WebhookService.new(reservation_id, status).send_arrived
    end
  when ApiVendorV1::Constants::NO_SHOW
    wait_until_reservation_status_has_changed(reservation_id, [:no_show])
    result = VendorsService::TagThai::WebhookService.new(reservation_id, status).send_noshow_cancel
  when ApiVendorV1::Constants::CANCEL
    wait_until_reservation_status_has_changed(reservation_id, [:cancelled, :rejected])
    result = VendorsService::TagThai::WebhookService.new(reservation_id, status).send_noshow_cancel
  when ApiVendorV1::Constants::CANCEL_MODIFIED
    wait_until_reservation_status_has_changed(reservation_id, [:cancel_modified, :rejected])
    result = VendorsService::TagThai::WebhookService.new(reservation_id, status,
                                                         vendor_reservation_id).send_cancel_modified
  else
    raise NotImplementedError
  end

  end_time = Time.zone.now
  processing_time = end_time - start_time
  HH_LOGGER.debug('WebhookWorker#perform processed', { reservation_id: reservation_id, status: status,
                                                       processing_time: processing_time, result: result })
end

#wait_until_reservation_status_has_changed(reservation_id, status) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/workers/vendors/tag_thai/webhook_worker.rb', line 50

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

      reservation&.reload
      sleep 1
    end
  end
end