Module: Api::Vendor::V1::Concerns::GetRestaurantList

Includes:
PaginationParam
Included in:
RestaurantsController
Defined in:
app/controllers/api/vendor/v1/concerns/get_restaurant_list.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#restaurant_list(named_cache_key, _scope = :default) ⇒ Object



7
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
# File 'app/controllers/api/vendor/v1/concerns/get_restaurant_list.rb', line 7

def restaurant_list(named_cache_key, _scope = :default)
  cache_key = CityHash.hash32([params, 'normal', I18n.locale, named_cache_key,
                               Restaurant.active.not_expired.cache_key,
                               RestaurantPriceSummary.maximum(:updated_at)])

  restaurant_ids = if params[:restaurant_ids].present?
                     params[:restaurant_ids].split(',').map(&:to_i)
                   else
                     query = build_graphql_query
                     response = get_hh_search(query)

                     # Handle response with success: false and return an error immediately
                     unless response.dig('data', 'SearchRestaurants', 'success')
                       render json: { success: false,
                                      message: response[:message] || 'An error occurred while fetching restaurant data' }
                       return
                     end
                     data_array = response.dig('data', 'SearchRestaurants', 'data') || []
                     data_array.map { |restaurant| restaurant['id'].to_i }
                   end

  filter = Api::Vendor::V1::RestaurantsFilter.new
  filter.restaurant_ids = restaurant_ids

  my_response_cache cache_key, :json, public: true do
    includes = ['ticket_groups']
    includes.push('pictures') if params.key?(:include_pictures) && params[:include_pictures].to_s == 'true'

    options = {
      include: includes,
      preview_mode: params[:preview_mode].to_s == 'true',
      user: nil,
      compact_mode: false,
    }

     = {}
    [:price_ranges] = {} # filter.get_price_ranges

    results = filter.as_json(serialization_context, minor_version_param, options)

    [:total_restaurants] = if params[:restaurant_ids].present?
                                      results['data'].count
                                    else
                                      response.dig('data', 'SearchRestaurants', 'metaData', 'totalRestaurants') || 0
                                    end

    option_data = { success: true, message: nil, meta_data:  }

    results.merge(option_data)
  end
end