Class: Api::Partner::V1::BaseController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Api::Partner::V1::BaseController
- Includes:
- ControllerHelpers, ElasticApmContext, Knock::Authenticable, LogrageCustomLogger, Pagy::Backend, Pundit::Authorization, ResponseCacheConcern
- Defined in:
- app/controllers/api/partner/v1/base_controller.rb
Direct Known Subclasses
AnalyticsController, BenchmarkController, BillsController, ChannelsController, CitiesController, DashboardsController, DistrictsController, FaqController, InventoriesController, MenuSectionsController, NotificationsController, OwnerReviewsController, PackageMenusController, ReportsController, ReservationsController, RestaurantAddOnsController, RestaurantDocumentsController, RestaurantPackagesController, RestaurantsController, ReviewsController, StaffsController, VouchersController
Defined Under Namespace
Classes: NotAuthorized
Instance Method Summary collapse
-
#default_restaurant ⇒ Restaurant
Retrieves the default restaurant for the current staff member or a restaurant based on the `restaurant_id` parameter.
- #identity_cache_memoization(&block) ⇒ Object
- #render_unauthorize_action ⇒ Object
-
#restaurants ⇒ ActiveRecord::Relation
Retrieves the list of restaurants available to the current staff member.
- #set_options(pagy = {}) ⇒ Object
Methods included from LogrageCustomLogger
Methods included from ControllerHelpers
#check_boolean_param, #get_banners, #inventory_params, #reservation_params
Methods included from ResponseCacheConcern
Instance Method Details
#default_restaurant ⇒ Restaurant
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.
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_action ⇒ Object
104 105 106 |
# File 'app/controllers/api/partner/v1/base_controller.rb', line 104 def render json: { success: false, message: 'Action Unauthorized' }, status: :unauthorized end |
#restaurants ⇒ ActiveRecord::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.
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 (pagy = {}) = {} if pagy.present? [:meta] = { total: (pagy).fetch(:count), page: (pagy).fetch(:page), } [: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 [:include] = params.fetch(:include, '').split(',') || [] [:fields] = params.fetch(:fields, {}).to_unsafe_hash.map do |rel, fields| { rel => fields.split(',').map(&:to_sym) } end.reduce({}, :merge) || {} [:params] = { current_staff: current_staff } () end |