Class: Webhooks::Vendors::SevenRooms::CallbackController

Inherits:
ActionController::API
  • Object
show all
Includes:
ElasticApmContext, Concerns::Authentication
Defined in:
app/controllers/webhooks/vendors/seven_rooms/callback_controller.rb

Instance Method Summary collapse

Instance Method Details

#callbackObject



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
# File 'app/controllers/webhooks/vendors/seven_rooms/callback_controller.rb', line 10

def callback
  BUSINESS_LOGGER.set_business_context({ vendor_name: ApiVendorV1::Constants::SEVEN_ROOMS_INV_SOURCE_NAME })
  VendorLogger.log_event(:webhook_in, params[:route], payload: { params: params,
                                                                 request_body: JSON.parse(request.body.read) })
  payload = JSON.parse(request.body.read)

  payload_results = validate_payload(payload)
  if payload_results.blank?
    return render json: { success: false, errors: ['Invalid payload'] }, status: :bad_request
  end

  errors = []
  # Process each payload result
  payload_results.each do |payload_result|
    sr_reservation_id = payload_result[:sr_reservation_id]
    sr_reservation = VendorReservation.find_by(supplier_reservation_id: sr_reservation_id)
    reservation = sr_reservation&.reservation
    BUSINESS_LOGGER.set_business_context({ reservation_id: reservation&.id,
                                           vendor_reference_id: sr_reservation_id })
    if reservation.blank?
      BUSINESS_LOGGER.error("#{self.class} Reservation not found")
      APMErrorHandler.report("#{self.class} Reservation not found", sr_reservation_id: sr_reservation_id)

      errors << "Reservation not found for reservation id #{sr_reservation_id}"
      next
    end

    reservation_id = sr_reservation.reservation.id
    Vendors::SevenRooms::WebhookWorker.perform_async(reservation_id, payload_result)
  end

  if errors.any?
    render json: { success: false, errors: errors }, status: :not_found
  else
    render json: { success: true, errors: [] }, status: :ok
  end
rescue JSON::ParserError
  render json: { success: false, errors: ['Invalid JSON format'] }, status: :bad_request
rescue StandardError, NotImplementedError => e
  APMErrorHandler.report("#{self.class} #{e.message}", payload: payload, exception: e)

  render json: { success: false, errors: ['Sorry, something went wrong'] },
         status: :internal_server_error
end