Class: Api::Vendor::V1::Getyourguide::BooksController

Inherits:
BaseController show all
Includes:
Concerns::ChargeCalculatorHelper, Concerns::Helpers
Defined in:
app/controllers/api/vendor/v1/getyourguide/books_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#vendor

Instance Method Summary collapse

Methods inherited from BaseController

#authentication

Methods included from ResponseCacheConcern

#my_response_cache

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 Method Details

#cancelObject

GYG Booking Cancellation Response: When a cancellation request is received, GYG expects:

  • On success: an empty JSON object in the “data” field, e.g. `{ “data”: {} }`. This indicates the booking was successfully cancelled.

  • On failure: a JSON object with “errorCode” and “errorMessage” fields describing the error. Optionally, a “detail” field may provide additional error information for our own debugging. Example: `{ “errorCode”: “NO_AVAILABILITY”, “errorMessage”: “This activity is sold out”, “detail”: … }`

The implementation below follows this contract: it returns an empty response on success, and a structured error response on failure.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/api/vendor/v1/getyourguide/books_controller.rb', line 73

def cancel
  params.require(:data)
  params[:data].require(:bookingReference)
  @permitted_params = params.permit(data: [:bookingReference])

  reservation_id = permitted_params.dig(:data, :bookingReference)

  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation_id,
                                         vendor_name: ApiVendorV1::Constants::GET_YOUR_GUIDE_VENDOR_NAME })
  VendorLogger.log_event(:request, params[:route], payload: params)

  @reservation = Reservation.find_by(id: reservation_id)
  return render_invalid_reservation(reservation_id) if reservation.blank?
  return render_empty_response if reservation.status_as_symbol == :cancelled

  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 = 'Cancelled by user'

  return render_empty_response if service.execute

  render_validation_failure(service.error_message_simple, reservation_id)
end

#createObject



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
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/api/vendor/v1/getyourguide/books_controller.rb', line 8

def create
  params.require(:data)
  %i[gygBookingReference reservationReference gygActivityReference
     productId dateTime bookingItems travelers].each { |key| params[:data].require(key) }

  params.require(:data)[:bookingItems].each { |item| %i[category count].each { |k| item.fetch(k) } }
  params.require(:data)[:travelers].each { |traveler| %i[firstName lastName email].each { |k| traveler.fetch(k) } }

  @permitted_params = params.permit(
    data: [
      :gygBookingReference, :reservationReference, :productId, :dateTime, :gygActivityReference, :comment,
      { bookingItems: %i[category count retailPrice groupSize] },
      { travelers: %i[firstName lastName email phoneNumber] }
    ],
  )

  @vendor_reference_id = permitted_params.dig(:data, :gygBookingReference)
  reservation_id = permitted_params.dig(:data, :reservationReference)

  BUSINESS_LOGGER.set_business_context({ vendor_reference_id: vendor_reference_id,
                                         reservation_id: reservation_id,
                                         vendor_name: ApiVendorV1::Constants::GET_YOUR_GUIDE_VENDOR_NAME })
  VendorLogger.log_event(:request, params[:route], payload: params)

  @reservation = Reservation.find_by(id: reservation_id)
  return render_invalid_reservation(reservation_id) if reservation.blank?

  # idempotency check
  vendor_reservation = VendorReservation.find_by(vendor_id: vendor.id, reference_id: vendor_reference_id)
  if vendor_reservation.present?
    BUSINESS_LOGGER.info(
      "#{self.class}##{__method__}: [idempotency check] duplicate create request, returning existing reservation",
    )
    return generate_and_render_tickets
  end

  category = permitted_params.dig(:data, :bookingItems, 0, :category)
  return render_invalid_ticket_category unless valid_category?(category)

  @gyg_product_id = permitted_params.dig(:data, :productId).to_s
  @gyg_option_id = permitted_params.dig(:data, :gygActivityReference)
  return render_invalid_product unless load_restaurant_and_package(gyg_product_id, gyg_option_id)

  service = ReservationService::Create.new(prepare_final_params, reservation)
  if service.execute
    @reservation = service.outcome
    BUSINESS_LOGGER.info("#{self.class}##{__method__}: reservation confirmed successfully")
    handle_successful_reservation
  else
    render_no_availability(service.error_message_simple)
  end
rescue StandardError => e
  render_no_availability(e.message, backtrace: e.backtrace&.last(5))
end