Class: Api::Partner::V1::BaseController

Inherits:
ActionController::API
  • Object
show all
Includes:
ControllerHelpers, ElasticApmContext, Knock::Authenticable, LogrageCustomLogger, Pagy::Backend, Pundit::Authorization, ResponseCacheConcern
Defined in:
app/controllers/api/partner/v1/base_controller.rb

Defined Under Namespace

Classes: NotAuthorized

Instance Method Summary collapse

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#default_restaurantRestaurant

Retrieves the default restaurant for the current staff member or a restaurant based on the `restaurant_id` parameter.

If a `restaurant_id` is passed in `params`, the method tries to find a restaurant with that ID. If no such restaurant exists or the staff member does not have access to it, it raises a `NotAuthorized` error. If no `restaurant_id` is provided, the method returns the staff member's default restaurant.

`default_restaurant` is used when the logic only supports a single branch. It finds one default restaurant either by the staff's default or from the `restaurant_id` parameter.

Returns:

  • (Restaurant)

    The default or selected restaurant associated with the current staff member.

Raises:

  • (NotAuthorized)

    If the current staff member does not have access to the restaurant.



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/api/partner/v1/base_controller.rb', line 68

def default_restaurant
  @default_restaurant ||= begin
    if params[:restaurant_id].present?
      # Use the restaurants method which already validates access
      restaurants.first
    else
     # Get default restaurant directly from staff
      current_staff.default_restaurant
    end
  end
end

#identity_cache_memoization(&block) ⇒ Object



20
21
22
# File 'app/controllers/api/partner/v1/base_controller.rb', line 20

def identity_cache_memoization(&block)
  IdentityCache.cache.with_memoization(&block)
end

#render_unauthorize_actionObject



104
105
106
# File 'app/controllers/api/partner/v1/base_controller.rb', line 104

def render_unauthorize_action
  render json: { success: false, message: 'Action Unauthorized' }, status: :unauthorized
end

#restaurantsActiveRecord::Relation

Retrieves the list of restaurants available to the current staff member.

If the `restaurant_id` parameter is present, the method will attempt to find a restaurant by the provided `restaurant_id`. It checks if the current staff member has access to that restaurant. If they don't, it raises a `NotAuthorized` error. Otherwise, it returns all the restaurants available to the staff member.

`restaurants` is used when the feature supports multiple restaurants (all branches). In this case, it returns the list of all restaurants the staff member has access to.

Returns:

  • (ActiveRecord::Relation)

    A collection of restaurants associated with the current staff member.

Raises:

  • (NotAuthorized)

    If the `restaurant_id` parameter is provided and the staff member does not have access to the specified restaurant.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/api/partner/v1/base_controller.rb', line 38

def restaurants
  @restaurants ||= begin
    if params[:restaurant_id].present?
      # Validate access and return filtered relation
      restaurant_id = params[:restaurant_id].to_i

      unless current_staff.restaurants.exists?(id: restaurant_id)
        raise NotAuthorized
      end

      # Return relation with just this restaurant
      current_staff.restaurants.where(id: restaurant_id)
    else
     # Return all staff restaurants
      current_staff.restaurants
    end
  end
end

#set_options(pagy = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/api/partner/v1/base_controller.rb', line 80

def set_options(pagy = {})
  options = {}
  if pagy.present?
    options[:meta] = {
      total: (pagy).fetch(:count),
      page: (pagy).fetch(:page),
    }

    options[:links] = {
      self: (pagy).fetch(:page_url),
      first: (pagy).fetch(:first_url),
      next: (pagy).fetch(:next_url),
      prev: (pagy).fetch(:prev_url),
      last: (pagy).fetch(:last_url),
    }
  end
  options[:include] = params.fetch(:include, '').split(',') || []
  options[:fields] = params.fetch(:fields, {}).to_unsafe_hash.map do |rel, fields|
    { rel => fields.split(',').map(&:to_sym) }
  end.reduce({}, :merge) || {}
  options[:params] = { current_staff: current_staff }
  fix_include_options(options)
end