Class: VendorsService::OpenRice::WebhookService

Inherits:
BaseOperationService
  • Object
show all
Defined in:
app/services/vendors_service/open_rice/webhook_service.rb

Constant Summary collapse

MAX_RETRY_ATTEMPTS =
2
RETRY_DELAY_IN_SECONDS =
5
ROUTE =
'booking-webhook'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(partner_booking_id, vendor_reservation_id, status) ⇒ WebhookService

Returns a new instance of WebhookService.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/vendors_service/open_rice/webhook_service.rb', line 14

def initialize(partner_booking_id, vendor_reservation_id, status)
  @partner_booking_id = partner_booking_id
  @vendor_reservation_id = vendor_reservation_id
  @status = status

  @reservation = Reservation.
    includes(:vendor_reservation).
    where(vendor_reservations: { id: vendor_reservation_id, reservation_id: partner_booking_id }).first&.decorate

  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation&.id,
                                         vendor_name: ApiVendorV1::Constants::OPEN_RICE_VENDOR_NAME })
end

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



8
9
10
# File 'app/services/vendors_service/open_rice/webhook_service.rb', line 8

def reservation
  @reservation
end

Instance Method Details

#auto_arrive_reservationObject



87
88
89
90
91
92
93
94
95
# File 'app/services/vendors_service/open_rice/webhook_service.rb', line 87

def auto_arrive_reservation
  # auto arrive reservation would send CONFIRM status to openrice webhook
  # but not change the internal reservation status
  return if reservation.blank?

  if reservation.status_as_symbol == :pending_arrival
    update_reservation_status
  end
end

#update_reservation_status(date = nil, time = nil, adult = nil, child = nil) ⇒ Object



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
81
82
83
84
85
# File 'app/services/vendors_service/open_rice/webhook_service.rb', line 27

def update_reservation_status(date = nil, time = nil, adult = nil, child = nil)
  return if reservation.blank?

  return if reservation.vendor_reservation.oauth_application&.name != ApiVendorV1::Constants::OPEN_RICE_VENDOR_NAME

  valid_statuses = [
    ApiVendorV1::Constants::UPDATE,
    ApiVendorV1::Constants::CONFIRM,
    ApiVendorV1::Constants::NO_SHOW,
    ApiVendorV1::Constants::CANCEL,
  ]

  unless valid_statuses.include?(@status)
    APMErrorHandler.report('Invalid reservation status ', status: @status)
    return
  end

  attempt = 0

  loop do
    attempt += 1

    # Construct the request body
    request_body = {
      partnerBookingId: @partner_booking_id,
      vendorReservationId: @vendor_reservation_id,
      status: @status,
    }

    if @status == ApiVendorV1::Constants::UPDATE
      request_body[:date] = date if date
      request_body[:time] = time if time
      request_body[:adult] = adult if adult
      request_body[:child] = child if child
    end

    # Handle the response
    response = send_to_webhook(request_body)
    if response.status.to_i == 200
      success = JSON.parse(response.body)['success']

      if success
        update_webhook_info(@status)
        return true
      end
    end

    if attempt >= MAX_RETRY_ATTEMPTS
      payload = { response_status: response.status, response_body: response.body, reservation_id: reservation&.id }
      VendorLogger.log_event(:webhook_out, ROUTE, payload: payload, custom_message: :error)
      APMErrorHandler.report("Reservation status update failed (Attempt #{attempt})", payload)
      break
    end

    sleep(RETRY_DELAY_IN_SECONDS)
  end

  false
end