Class: VendorsService::TagThai::WebhookService

Inherits:
BaseOperationService show all
Defined in:
app/services/vendors_service/tag_thai/webhook_service.rb

Constant Summary collapse

ROUTE =
'booking/callback'.freeze

Instance Attribute Summary collapse

Attributes inherited from BaseOperationService

#outcome

Instance Method Summary collapse

Methods inherited from BaseOperationService

#success?

Constructor Details

#initialize(reservation_id, status, vendor_reservation_id = nil) ⇒ WebhookService

Returns a new instance of WebhookService.



12
13
14
15
16
17
18
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 12

def initialize(reservation_id, status, vendor_reservation_id = nil)
  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation_id,
                                         vendor_name: ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME })
  @reservation = Reservation.find_by(id: reservation_id)&.decorate
  @status = status
  @vendor_reservation = find_vendor_reservation(vendor_reservation_id)
end

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



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

def reservation
  @reservation
end

#vendor_reservationObject (readonly)

Returns the value of attribute vendor_reservation.



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

def vendor_reservation
  @vendor_reservation
end

Instance Method Details

#auto_arrive_reservationObject



140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 140

def auto_arrive_reservation
  # auto arrive reservation would send CONFIRM status to TAGTHAI webhook
  # but not change the internal reservation status
  reservation.with_lock do
    return false if reservation_invalid?
    if reservation.status_as_symbol != :pending_arrival
      return
    end

    send_arrived
  end
end

#send_arrivedObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 68

def send_arrived
  reservation.with_lock do
    return false if reservation_invalid?
    return false if vendor_reservation.oauth_application&.name != ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME
    return false unless valid_status

    response = send_webhook

    if response.status == 200
      update_webhook_status
      if vendor_reservation.webhook_sent_at.blank?
        APMErrorHandler.report('Failed to mark TagThai webhook as arrived sent at',
                               { vendor_reservation: vendor_reservation })
      end
    else
      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('[send_arrived] TagThai Webhook Notif error', payload)
    end
  end

  true
end

#send_cancel_modifiedObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 116

def send_cancel_modified
  reservation.with_lock do
    return false if reservation_invalid?
    return false unless valid_status

    response = send_webhook

    if response.status == 200
      vendor_reservation.update webhook_cancel_modified_sent_at: Time.zone.now
      update_webhook_status
      if vendor_reservation.webhook_cancel_modified_sent_at.blank?
        BUSINESS_LOGGER.error('Failed to mark TagThai webhook cancel modified as sent',
                              vendor_reservation: vendor_reservation)
      end
    else
      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('[send_cancel_modified] TagThai Webhook Notif error', payload)
    end
  end

  true
end

#send_noshow_cancelObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 92

def send_noshow_cancel
  reservation.with_lock do
    return false if reservation_invalid?
    return false if vendor_reservation.oauth_application&.name != ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME
    return false unless valid_status

    response = send_webhook

    if response.status == 200
      update_webhook_status
      if vendor_reservation.webhook_sent_at.blank?
        BUSINESS_LOGGER.error('Failed to mark TagThai webhook as sent',
                              vendor_reservation: vendor_reservation)
      end
    else
      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('[send_noshow_cancel] TagThai Webhook Notif error', payload)
    end
  end

  true
end

#send_pending_arrivalObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 44

def send_pending_arrival
  reservation.with_lock do
    return false if reservation_invalid?
    return false if vendor_reservation.oauth_application&.name != ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME
    return false unless valid_status

    response = send_webhook

    if response.status == 200
      update_webhook_status
      if vendor_reservation.webhook_sent_at.blank?
        APMErrorHandler.report('Failed to mark TagThai webhook as pending arrival sent at',
                               { vendor_reservation: vendor_reservation })
      end
    else
      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('[send_pending_arrival] TagThai Webhook Notif error', payload)
    end

    true
  end
end

#send_waiting_paymentObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 20

def send_waiting_payment
  reservation.with_lock do
    return false if reservation_invalid?
    return false if vendor_reservation.oauth_application&.name != ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME
    return false unless valid_status

    response = send_webhook

    if response.status == 200
      update_webhook_status
      if vendor_reservation.webhook_sent_at.blank?
        BUSINESS_LOGGER.error('Failed to mark TagThai webhook as sent at',
                              vendor_reservation: vendor_reservation)
      end
    else
      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('[send_waiting_for_payment] TagThai Webhook Notif error', error_detail: payload)
    end

    true
  end
end

#update_webhook_statusObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/services/vendors_service/tag_thai/webhook_service.rb', line 153

def update_webhook_status
  if vendor_reservation.present?
    updated_journal_entry = vendor_reservation.journal_entry.to_h.merge(
      Time.zone.now.utc.to_s => "webhook sent for #{@status} status",
    )

    vendor_reservation.update(
      webhook_sent_at: Time.zone.now,
      journal_entry: updated_journal_entry,
    )
  else
    BUSINESS_LOGGER.error('Vendor reservation not found for update_webhook_status',
                          vendor_reservation_id: vendor_reservation_id)
  end
end