Class: PrimaryTagCpt::Operations::UpdateRelation

Inherits:
Trailblazer::Operation
  • Object
show all
Includes:
Tagging::DistanceHelper
Defined in:
app/concepts/primary_tag_cpt/operations/update_relation.rb

Overview

Create or Update a PrimaryTag to make a relation between Restaurant and RestaurantTag

Instance Method Summary collapse

Methods included from Tagging::DistanceHelper

#decide_primary_tag, #find_distance_between

Instance Method Details

#remove_existing_primary_tag!(options) ⇒ Object

Remove existing primary tag



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
# File 'app/concepts/primary_tag_cpt/operations/update_relation.rb', line 26

def remove_existing_primary_tag!(options, *)
  category = options['model'].category.downcase
  if ['location', 'popularzone', 'btsroute', 'mrtroute', 'shoppingmall'].include?(category)
    remove_existing_primary_tag_location!(options['model.restaurant'], category)
  end

  # Remove existing primary tag if has same category
  primary_tag = options['model.restaurant'].primary_tag_by_category(options['model'].category)

  if primary_tag.present?
    primary_tag.destroy!
    check_sub_tag = RestaurantTagsRestaurant.where(
      restaurant_id: primary_tag.restaurant_id,
      restaurant_tag_id: primary_tag.restaurant_tag_id,
    )
    if check_sub_tag.blank?
      add_sub_tag_from_primary = RestaurantTagsRestaurant.new(
        restaurant_id: primary_tag.restaurant_id,
        restaurant_tag_id: primary_tag.restaurant_tag_id,
      )
      add_sub_tag_from_primary.save!
    end
  end
  true
end

#send_update_event!(options) ⇒ Object

Send update event to Kafka



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/concepts/primary_tag_cpt/operations/update_relation.rb', line 107

def send_update_event!(options, *)
  attr_changes = case options['model'].category.downcase
                 when 'cuisine'
                   { primary_cuisine: {} }
                 when 'diningstyle'
                   { primary_dining_style: {} }
                 when 'location', 'popularzone', 'btsroute', 'mrtroute', 'shoppingmall'
                   { primary_location: {} }
                 when 'awardbadge'
                   { primary_award_badge: {} }
                 when 'awardtype'
                   { primary_award_type: {} }
                 else
                   {}
                 end

  restaurant = options['model.restaurant']

  if restaurant.bookable_and_not_expired?
    CleanCompactRestaurantCacheWorker.perform_async(restaurant.id)
    Restaurants::SlugWorker.perform_async(restaurant.id)

    EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
      EventDrivenClient::Constants::RESTAURANTS_TOPIC,
      EventDrivenClient::Constants::UPDATE_EVENT,
      restaurant.id,
      attr_changes,
      { source: 'PrimaryTagCpt::Operations::UpdateRelation#send_update_event!' },
    )
  end

  true
end

#set_error_message!(options) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'app/concepts/primary_tag_cpt/operations/update_relation.rb', line 96

def set_error_message!(options, *)
  if options[OpCons::ERRORS].blank?
    APMErrorHandler.report('unable to update primary tags',
                           restaurant_id: options['model.restaurant'].id,
                           restaurant_tag_id: options['model'].id)
    options[OpCons::ERRORS] = 'Something went wrong'
  end
  true
end

#set_restaurant_model!(options) ⇒ Object



21
22
23
# File 'app/concepts/primary_tag_cpt/operations/update_relation.rb', line 21

def set_restaurant_model!(options, *)
  options['model.restaurant'] = ::Restaurant.find(options['params'][:restaurant_id])
end

#touch_restaurant!(options) ⇒ Object



90
91
92
93
94
# File 'app/concepts/primary_tag_cpt/operations/update_relation.rb', line 90

def touch_restaurant!(options, *)
  options['model.restaurant'].touch

  true
end

#update_or_create_primary_tag!(options) ⇒ Object



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
# File 'app/concepts/primary_tag_cpt/operations/update_relation.rb', line 52

def update_or_create_primary_tag!(options, *)
  restaurant = options['model.restaurant']
  pt = nil

  PrimaryTag.transaction do
    pt = ::PrimaryTag.find_or_initialize_by restaurant_id: restaurant.id,
                                            restaurant_tag_id: options['model'].id

    restaurant.primary_tags.reload

    # counting duplicate tag categories within a specific restaurant
    group_by_categories = restaurant.primary_tags.
      joins(:restaurant_tag).
      select("SUBSTRING_INDEX(restaurant_tags.title_en, ':', 1) AS category").
      where("SUBSTRING_INDEX(restaurant_tags.title_en, ':', 1) = ?", options['model'].category).
      group("SUBSTRING_INDEX(restaurant_tags.title_en, ':', 1)").
      having('COUNT(*) > 1')

    if group_by_categories.present?
      pt.errors.add :base, 'there is duplicate tag, please try again'
      raise ActiveRecord::Rollback
    end

    distance = find_distance_between(restaurant: restaurant, restaurant_tag: options['model'])
    if pt.persisted?
      pt.update(distance_to_restaurant: distance)
    else
      pt.distance_to_restaurant = distance
      pt.save!
    end

    return true if pt.persisted?
  end

  options[OpCons::ERRORS] = pt.errors if pt.present?
  false
end