Class: Api::Vendor::V1::RestaurantPackagesController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::Authentication
Defined in:
app/controllers/api/vendor/v1/restaurant_packages_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE

Instance Attribute Summary

Attributes inherited from BaseController

#vendor

Instance Method Summary collapse

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#indexObject



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/restaurant_packages_controller.rb', line 11

def index
  restaurant_id = params.require(:restaurant_id)

  restaurant = Restaurant.active.not_expired.find_by(id: restaurant_id)
  raise ApiVendorV1::Errors::RestaurantValidationError if restaurant.blank?

  package_type = params[:package_types]
  payment_type = params[:payment_type]

  # return error if payment_type is not valid
  if payment_type.present? && ApiVendorV1::Constants::PACKAGE_PAYMENT_TYPES.exclude?(payment_type)
    return render json: { success: false, message: 'Invalid payment_type', data: nil }
  end

  # Excluding the Copper Beyond Buffet restaurant_package (ID: 45461) from third-party integrations
  # (e.g., OpenRice, KKday) due to tier price limitations, which cause incorrect pricing.
  restaurant_packages = restaurant.all_restaurant_packages.valid_to_have_agendas_and_not_preview.where.not(id: 45461)
  if params[:package_types].present?
    restaurant_packages = filter_packages_by_type(restaurant_packages,
                                                  params[:package_types])
  end

  if payment_type.present?
    restaurant_packages = filter_packages_by_payment_type(restaurant_packages,
                                                          payment_type)
  end

  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:index:#{restaurant_packages.cache_key}:#{package_type}:#{payment_type}:#{I18n.locale}"
  my_response_cache cache_key, :json, public: true do
    include_restaurant = params.key?('include_restaurant') && params['include_restaurant'].to_s == 'true'

    booking_channel = Channel.find_by oauth_application_id: vendor.id
    options = {
      include: include_restaurant ? ['restaurant'] : [],
      params: {
        vendor_name: vendor.name,
        support_dynamic_pricing: booking_channel&.support_dynamic_pricing,
      },
    }

    resource_as_json(restaurant_packages, Api::Vendor::V1::RestaurantPackageSerializer, options).
      merge(success: true, message: nil)
  end
rescue ApiVendorV1::Errors::RestaurantValidationError
  render_error_response(error_message: ApiVendorV1::Constants::VENDOR_RESTAURANT_VALIDATION_ERROR)
rescue ActionController::ParameterMissing => e
  render_error_response(error_message: e.message)
rescue StandardError => e
  APMErrorHandler.report e
  render_error_response(error_message: e.message)
end

#showObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/api/vendor/v1/restaurant_packages_controller.rb', line 63

def show
  restaurant_package_id = params.require(:id)

  restaurant_package = HhPackage::RestaurantPackage.valid_to_have_agendas_and_not_preview.find_by(id: restaurant_package_id)
  raise ApiVendorV1::Errors::RestaurantPackageValidationError if restaurant_package.blank?

  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:show:#{restaurant_package.cache_key}:#{I18n.locale}"

  my_response_cache cache_key, :json, public: true do
    include_restaurant = params.key?('include_restaurant') && params['include_restaurant'].to_s == 'true'
    booking_channel = Channel.find_by oauth_application_id: vendor.id
    options = {
      include: include_restaurant ? ['restaurant'] : [],
      params: {
        vendor_name: vendor.name,
        support_dynamic_pricing: booking_channel&.support_dynamic_pricing,
      },
    }

    resource_as_json(restaurant_package, Api::Vendor::V1::RestaurantPackageSerializer, options).
      merge(success: true, message: nil)
  end
rescue ActionController::ParameterMissing => e
  render_error_response(error_message: e.message)
rescue ApiVendorV1::Errors::RestaurantPackageValidationError
  render_error_response(error_message: ApiVendorV1::Constants::VENDOR_RESTAURANT_PACKAGE_VALIDATION_ERROR)
rescue StandardError => e
  APMErrorHandler.report e
  render_error_response(error_message: e.message)
end