Class: Api::Vendor::V1::Klook::BookingsController

Inherits:
BaseController show all
Includes:
Concerns::ReservationUtils, Concerns::ErrorResponses
Defined in:
app/controllers/api/vendor/v1/klook/bookings_controller.rb

Constant Summary

Constants inherited from BaseController

Api::Vendor::V1::Klook::BaseController::CACHE_NAMESPACE

Instance Attribute Summary collapse

Attributes inherited from BaseController

#vendor

Instance Method Summary collapse

Methods inherited from BaseController

#authentication

Methods included from ResponseCacheConcern

#my_response_cache

Methods included from EncryptableHelper

#decrypt, #encrypt, #generate_signature

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



6
7
8
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 6

def reservation
  @reservation
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



6
7
8
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 6

def restaurant
  @restaurant
end

#restaurant_packageObject (readonly)

Returns the value of attribute restaurant_package.



6
7
8
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 6

def restaurant_package
  @restaurant_package
end

#vendor_reference_idObject (readonly)

Returns the value of attribute vendor_reference_id.



6
7
8
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 6

def vendor_reference_id
  @vendor_reference_id
end

#vendor_reservationObject (readonly)

Returns the value of attribute vendor_reservation.



6
7
8
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 6

def vendor_reservation
  @vendor_reservation
end

Instance Method Details

#cancelObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 129

def cancel
  @vendor_reservation = VendorReservation.find_by(id: params[:uuid])
  @reservation = vendor_reservation&.reservation
  raise BookingValidationError if vendor_reservation.blank? || reservation.blank?

  @vendor_reference_id = vendor_reservation.reference_id
  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation.id,
                                         vendor_reference_id: vendor_reference_id })
  RequestStore.store[:reservation_id] ||= reservation.id
  VendorLogger.log_event(:request, params[:route], payload: params)

  service_options = { require_reason: true }
  service_options[:force_cancel] = integration_test_force_cancel_option if integration_test_mode?
  service = CancelReservationService.new(reservation.id, :user, service_options)
  service.cancel_reason = params[:reason]

  if service.execute
    @reservation = reservation.reload
    serialized_data = Api::Vendor::V1::Klook::BookingSerializer.new(reservation)
    VendorLogger.log_event(:response, params[:route], payload: serialized_data)
    render json: serialized_data, status: :ok
  else
    raise(StandardError, service.error_message_simple)
  end
rescue BookingValidationError
  render_error(error: ApiVendorV1::Constants::KLOOK_INVALID_BOOKING_UUID_ERROR,
               error_message: ApiVendorV1::Constants::KLOOK_INVALID_BOOKING_UUID_ERROR_MESSAGE, method: :cancel)
rescue StandardError => e
  render_error(error: ApiVendorV1::Constants::KLOOK_UNPROCESSABLE_ENTITY_ERROR,
               error_message: e.message, method: :cancel)
end

#confirmObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 82

def confirm
  contact = params.require(:contact)
  confirm_params = contact.permit(:phoneNumber).merge(
    fullName: contact.require(:fullName),
    emailAddress: contact.require(:emailAddress),
    resellerReference: params.permit(:resellerReference)[:resellerReference],
  )
  @vendor_reference_id = confirm_params[:resellerReference]
  BUSINESS_LOGGER.set_business_context({ vendor_reference_id: vendor_reference_id })
  VendorLogger.log_event(:request, params[:route], payload: params)

  # Idempotency check
  @vendor_reservation = VendorReservation.find_by(vendor_id: vendor.id, reference_id: vendor_reference_id)
  return handle_idempotent_request if @vendor_reservation

  @vendor_reservation = VendorReservation.find_by(id: params[:uuid])
  @reservation = vendor_reservation&.reservation
  raise BookingValidationError if vendor_reservation.blank? || reservation.blank?

  @restaurant = reservation.restaurant

  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation.id })
  RequestStore.store[:reservation_id] ||= reservation.id

  guest_user_params = prepare_guest_user_params(confirm_params)
  create_reservation_params = build_reservation_params(reservation, guest_user_params)
  service = ReservationService::Create.new(create_reservation_params, reservation)

  if service.execute
    @reservation = service.outcome
    BUSINESS_LOGGER.info("#{self.class}##{__method__}: create reservation success")
    handle_successful_reservation(confirm_params)
    render_confirm_success_response
  else
    raise(StandardError, service.error_message_simple)
  end
rescue ActionController::ParameterMissing => e
  render_error(error: ApiVendorV1::Constants::KLOOK_BAD_REQUEST_ERROR,
               error_message: "Missing parameter: #{e.param}", method: :confirm)
rescue BookingValidationError
  render_error(error: ApiVendorV1::Constants::KLOOK_INVALID_BOOKING_UUID_ERROR,
               error_message: ApiVendorV1::Constants::KLOOK_INVALID_BOOKING_UUID_ERROR_MESSAGE, method: :confirm)
rescue StandardError => e
  render_error(error: ApiVendorV1::Constants::KLOOK_UNPROCESSABLE_ENTITY_ERROR,
               error_message: e.message, method: :confirm)
end

#createObject



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
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 16

def create
  %i[productId optionId availabilityId unitItems].each { |key| params.require(key) }
  VendorLogger.log_event(:request, params[:route], payload: params)
  create_params = params.permit(:productId, :optionId, :availabilityId, :notes, unitItems: [:unitId])

  valid_units = %w[adult child pack]
  invalid_units = create_params[:unitItems].reject { |u| valid_units.include?(u[:unitId]) }

  if invalid_units.any?
    invalid_ids = invalid_units.map(&:unitId).uniq.join(', ')
    raise(UnitValidationError, "Invalid unitIds: #{invalid_ids}")
  end

  @restaurant = Restaurant.active.not_expired.find_by(id: create_params[:productId])
  raise ProductValidationError if restaurant.blank?

  @restaurant_package = HhPackage::RestaurantPackage.valid_to_have_agendas_and_not_preview.find_by(id: create_params[:optionId],
                                                                                                   restaurant_id: restaurant.id)
  raise OptionValidationError if @restaurant_package.blank?

  res_params = prepare_tmp_reservation_params(create_params)
  service = ReservationService::InitFactory.build(res_params)
  result = service.create_temporary

  if result.success?
    @reservation = result.data
    BUSINESS_LOGGER.set_business_context({ reservation_id: reservation&.id })
    BUSINESS_LOGGER.info("#{self.class}##{__method__}: Create Tmp. Reservation success")

    create_vendor_reservation!(reservation)

    # Sync temporary reservation to partner portal
    begin
      reservation.trigger_summary_sync('full')
    rescue StandardError => e
      APMErrorHandler.report('Failed to sync temporary Klook reservation', {
                               reservation_id: reservation.id,
                               error_message: e.message,
                               exception: e,
                             })
    end

    serialized_data = Api::Vendor::V1::Klook::BookingSerializer.new(reservation)
    VendorLogger.log_event(:response, params[:route], payload: serialized_data)
    render json: serialized_data, status: :ok
  else
    raise(StandardError, result.errors.full_messages.to_sentence)
  end
rescue ActionController::ParameterMissing => e
  render_error(error: ApiVendorV1::Constants::KLOOK_BAD_REQUEST_ERROR,
               error_message: "Missing parameter: #{e.param}", method: :create)
rescue UnitValidationError => e
  render_error(error: ApiVendorV1::Constants::KLOOK_INVALID_UNIT_ID_ERROR,
               error_message: e.message, method: :create)
rescue ProductValidationError
  render_error(error: ApiVendorV1::Constants::KLOOK_INVALID_PRODUCT_ID_ERROR,
               error_message: ApiVendorV1::Constants::KLOOK_INVALID_PRODUCT_ID_ERROR_MESSAGE, method: :create)
rescue OptionValidationError
  render_error(error: ApiVendorV1::Constants::KLOOK_INVALID_OPTION_ID_ERROR,
               error_message: ApiVendorV1::Constants::KLOOK_INVALID_OPTION_ID_ERROR_MESSAGE, method: :create)
rescue StandardError => e
  render_error(error: ApiVendorV1::Constants::KLOOK_UNPROCESSABLE_ENTITY_ERROR,
               error_message: ApiVendorV1::Constants::KLOOK_UNAVAILABILITY_ERROR_MESSAGE,
               error_detail: e.message, method: :create)
end

#indexObject



8
9
10
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 8

def index
  render_filtered_bookings
end

#showObject



12
13
14
# File 'app/controllers/api/vendor/v1/klook/bookings_controller.rb', line 12

def show
  render_filtered_bookings
end