Module: EventDrivenServices::HhSearch::Helpers::RestaurantTagHelper

Included in:
Producers::RestaurantTagProducer, Schemas::RestaurantTagSchema
Defined in:
app/services/event_driven_services/hh_search/helpers/restaurant_tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#restaurant_tags_with_min_one_tagged_active_restaurantsActiveRecord::Relation

Get all restaurant tags that have at least one tagged restaurants

Returns:

  • (ActiveRecord::Relation)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/event_driven_services/hh_search/helpers/restaurant_tag_helper.rb', line 9

def restaurant_tags_with_min_one_tagged_active_restaurants
  # Get current time in UTC
  current_time = Time.now.utc

  # Join with active RestaurantTagsRestaurant associations
  tags_from_restaurant_tags_restaurants = RestaurantTag.joins(restaurant_tags_restaurants: :restaurant).
    where(restaurants: { active: true }).
    where('restaurants.expiry_date >= ?', current_time)

  # Join with active PrimaryTag associations
  tags_from_primary_tags = RestaurantTag.joins(primary_tags: :restaurant).
    where(restaurants: { active: true }).
    where('restaurants.expiry_date >= ?', current_time)

  # Perform a union of both associations and ensure uniqueness
  tags_from_restaurant_tags_restaurants.union(tags_from_primary_tags).distinct
end

#total_restaurants_for_all_cities(restaurant_tag) ⇒ Integer

Get the total number of restaurants that tagged with the restaurant_tag

Parameters:

Returns:

  • (Integer)


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
# File 'app/services/event_driven_services/hh_search/helpers/restaurant_tag_helper.rb', line 32

def total_restaurants_for_all_cities(restaurant_tag)
  return 0 if restaurant_tag.blank?
  return 0 unless restaurant_tag.is_a?(RestaurantTag)

  # Get current time in UTC
  current_time = Time.now.utc

  # Query for restaurant_tags_restaurants
  restaurant_tags_restaurants = RestaurantTagsRestaurant.joins(:restaurant).
    where(restaurant_tag_id: restaurant_tag.id).
    where('restaurants.expiry_date >= ?', current_time).
    where(restaurants: { active: true }).
    distinct

  restaurant_tags_restaurants_count = restaurant_tags_restaurants.count('restaurants.id')

  # Query for primary_tags
  primary_tags_count = PrimaryTag.joins(:restaurant).
    where(restaurant_tag_id: restaurant_tag.id).
    where('restaurants.expiry_date >= ?', current_time).
    where(restaurants: { active: true }).
    where.not(restaurant_id: restaurant_tags_restaurants.select(:restaurant_id)).
    distinct.
    count('restaurants.id')

  # Return the total count of restaurants that tagged with the restaurant_tag
  restaurant_tags_restaurants_count + primary_tags_count
end