Class: HhMenu::PackagesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- HhMenu::PackagesController
- Defined in:
- app/controllers/hh_menu/packages_controller.rb
Instance Method Summary collapse
- #batch_identifiers ⇒ Object
- #detail ⇒ Object
-
#identifiers ⇒ Object
GET /hh_menu/packages/identifiers?restaurant_id=123 Lightweight endpoint to return only package_type and package_id for a restaurant.
Instance Method Details
#batch_identifiers ⇒ Object
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 81 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 |
# File 'app/controllers/hh_menu/packages_controller.rb', line 48 def batch_identifiers restaurant_ids = params[:restaurant_ids] # Validate input if restaurant_ids.blank? || !restaurant_ids.is_a?(Array) return render json: { success: false, message: 'restaurant_ids array is required', errors: [], }, status: :bad_request end # Sanitize input - ensure all IDs are integers restaurant_ids = restaurant_ids.map(&:to_i).compact.uniq if restaurant_ids.empty? return render json: { success: false, message: 'Valid restaurant_ids are required', errors: [], }, status: :bad_request end # Same query logic as identifiers but for multiple restaurant_ids records = HhPackage::RestaurantPackage. where(restaurant_id: restaurant_ids). where('hh_package_restaurant_packages.start_date <= :today AND hh_package_restaurant_packages.end_date >= :today', today: Date.current). joins('INNER JOIN hh_package_package_attrs pa ON pa.package_id = hh_package_restaurant_packages.package_id AND pa.package_type = hh_package_restaurant_packages.package_type AND pa.menu_group_id IS NOT NULL'). select('DISTINCT hh_package_restaurant_packages.id, hh_package_restaurant_packages.package_id, hh_package_restaurant_packages.package_type, hh_package_restaurant_packages.restaurant_id'). includes(:package) # Group results by restaurant_id for efficient client-side processing data_by_restaurant = {} records.find_each do |rp| restaurant_id = rp.restaurant_id data_by_restaurant[restaurant_id] ||= [] data_by_restaurant[restaurant_id] << { restaurant_package_id: rp.id, package_id: rp.package_id, package_type: rp.package_type, package_name: rp.name, # delegated to underlying package } end # Ensure all requested restaurant_ids are represented (even if empty) restaurant_ids.each do |rid| data_by_restaurant[rid] ||= [] end render json: { success: true, restaurant_ids: restaurant_ids, packages_by_restaurant: data_by_restaurant, total_restaurants: restaurant_ids.size, total_packages: records.count, } rescue StandardError => e # Use BUSINESS_LOGGER with minimal context (no Rails.logger per project standards) BUSINESS_LOGGER.error('packages_batch_identifiers_error', { restaurant_ids: restaurant_ids, error_class: e.class.name, error_message: e., }) render json: { success: false, message: 'internal error' }, status: :internal_server_error end |
#detail ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/controllers/hh_menu/packages_controller.rb', line 115 def detail if params[:package_type] == 'pp' || params[:package_type] == 'HhPackage::Package::PartyPack' params[:package_type] = 'HhPackage::Package::PartyPack' elsif params[:package_type] == 'ayce' || params[:package_type] == 'HhPackage::Package::Ayce' params[:package_type] = 'HhPackage::Package::Ayce' end restaurant_package_relation = HhPackage::RestaurantPackage.where(package_id: params[:package_id], package_type: params[:package_type]) restaurant_package = if params[:restaurant_id].present? restaurant_package_relation.find_by(restaurant_id: params[:restaurant_id]) else restaurant_package_relation.first end if restaurant_package.present? serializer = HhMenu::RestaurantPackageSerializer.new(restaurant_package) render json: { success: true, restaurant_package: serializer.as_json } else render json: { success: false } end end |
#identifiers ⇒ Object
GET /hh_menu/packages/identifiers?restaurant_id=123 Lightweight endpoint to return only package_type and package_id for a restaurant. This replaces calling the heavier /api/v5/restaurant_packages.json when only identifiers are needed by hh-menu frontend for constructing print menu URLs.
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 |
# File 'app/controllers/hh_menu/packages_controller.rb', line 8 def identifiers restaurant_id = params[:restaurant_id] if restaurant_id.blank? return render json: { success: false, message: 'restaurant_id is required', errors: [] }, status: :bad_request end # Narrow select to avoid loading large serialized columns on restaurant_packages # and eager load the polymorphic :package to fetch its name without N+1 queries. # Filter only packages that have a related PackageAttr (hh_package_package_attrs) # with a non-null menu_group_id. We join on (package_id, package_type) to the # polymorphic target. Using INNER JOIN + DISTINCT avoids duplicates if multiple # attrs somehow exist (defensive) while keeping the select lean. records = HhPackage::RestaurantPackage. where(restaurant_id: restaurant_id). where('hh_package_restaurant_packages.start_date <= :today AND hh_package_restaurant_packages.end_date >= :today', today: Date.current). joins('INNER JOIN hh_package_package_attrs pa ON pa.package_id = hh_package_restaurant_packages.package_id AND pa.package_type = hh_package_restaurant_packages.package_type AND pa.menu_group_id IS NOT NULL'). select('DISTINCT hh_package_restaurant_packages.id, hh_package_restaurant_packages.package_id, hh_package_restaurant_packages.package_type'). includes(:package) # Map to concise objects (keeping consistency: package_type + package_id) data = records.find_each.map do |rp| { restaurant_package_id: rp.id, package_id: rp.package_id, package_type: rp.package_type, package_name: rp.name, # delegated to underlying package (see model delegate) } end render json: { success: true, restaurant_id: restaurant_id, packages: data } rescue StandardError => e # Use BUSINESS_LOGGER with minimal context (no Rails.logger per project standards) BUSINESS_LOGGER.error('packages_identifiers_error', { restaurant_id: restaurant_id, error_class: e.class.name, error_message: e., }) render json: { success: false, message: 'internal error' }, status: :internal_server_error end |