Class: RestaurantService::TagAssignment

Inherits:
ApplicationService show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/restaurant_service/tag_assignment.rb

Constant Summary collapse

TAG_TYPES =
{
  cuisine: { prefix: 'Cuisine:', event_key: 'cuisines' },
  dining_style: { prefix: 'DiningStyle:', event_key: 'dining_styles' },
  facility: { prefix: 'Facility:', event_key: 'facilities' },
  location: { prefix: ['Location%', 'PopularZone%', 'ShoppingMall%', 'MrtRoute%', 'BtsRoute%'],
              event_key: 'locations' },
  award: { prefix: ['AwardBadge%', 'AwardType%'], event_key: 'award_badges' },
}.freeze

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute

Constructor Details

#initialize(restaurant:, tag_type:, tag_ids: []) ⇒ TagAssignment

Returns a new instance of TagAssignment.



18
19
20
21
22
# File 'app/services/restaurant_service/tag_assignment.rb', line 18

def initialize(restaurant:, tag_type:, tag_ids: [])
  @restaurant = restaurant
  @tag_type = tag_type.to_sym
  @tag_ids = Array(tag_ids).map(&:to_i).reject(&:zero?)
end

Instance Attribute Details

#restaurantObject (readonly)

Returns the value of attribute restaurant.



16
17
18
# File 'app/services/restaurant_service/tag_assignment.rb', line 16

def restaurant
  @restaurant
end

#tag_idsObject (readonly)

Returns the value of attribute tag_ids.



16
17
18
# File 'app/services/restaurant_service/tag_assignment.rb', line 16

def tag_ids
  @tag_ids
end

#tag_typeObject (readonly)

Returns the value of attribute tag_type.



16
17
18
# File 'app/services/restaurant_service/tag_assignment.rb', line 16

def tag_type
  @tag_type
end

Instance Method Details

#execute!Object



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
# File 'app/services/restaurant_service/tag_assignment.rb', line 24

def execute!
  validate_tag_type!

  ActiveRecord::Base.transaction do
    restaurant.with_lock do
      remove_existing_tags!
      add_new_tags!
    end
  end

  restaurant.touch
  trigger_search_update! if restaurant.active_and_not_expired?

  ServiceResult.new(message: "#{tag_type.to_s.humanize} tags updated successfully")
rescue StandardError => e
  HH_LOGGER.error('TagAssignment Service Error', {
                    error: e.class.name,
                    message: e.message,
                    restaurant_id: restaurant.id,
                    tag_type: tag_type,
                    tag_ids: tag_ids,
                    backtrace: e.backtrace.first(10),
                  })
  APMErrorHandler.report(e, context: { restaurant_id: restaurant.id, tag_type: tag_type, tag_ids: tag_ids })
  ServiceResult.new(
    errors: [e.message],
    message: "Failed to update #{tag_type.to_s.humanize} tags: #{e.message}",
  )
end