Class: PrimaryTagCpt::Operations::UpdateRelation
- Inherits:
-
Trailblazer::Operation
- Object
- Trailblazer::Operation
- PrimaryTagCpt::Operations::UpdateRelation
- 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
-
#remove_existing_primary_tag!(options) ⇒ Object
Remove existing primary tag.
-
#send_update_event!(options) ⇒ Object
Send update event to Kafka.
- #set_error_message!(options) ⇒ Object
- #set_restaurant_model!(options) ⇒ Object
- #touch_restaurant!(options) ⇒ Object
- #update_or_create_primary_tag!(options) ⇒ Object
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!(, *) category = ['model'].category.downcase if ['location', 'popularzone', 'btsroute', 'mrtroute', 'shoppingmall'].include?(category) remove_existing_primary_tag_location!(['model.restaurant'], category) end # Remove existing primary tag if has same category primary_tag = ['model.restaurant'].primary_tag_by_category(['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!(, *) attr_changes = case ['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 = ['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 (, *) if [OpCons::ERRORS].blank? APMErrorHandler.report('unable to update primary tags', restaurant_id: ['model.restaurant'].id, restaurant_tag_id: ['model'].id) [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!(, *) ['model.restaurant'] = ::Restaurant.find(['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!(, *) ['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!(, *) restaurant = ['model.restaurant'] pt = nil PrimaryTag.transaction do pt = ::PrimaryTag.find_or_initialize_by restaurant_id: restaurant.id, restaurant_tag_id: ['model'].id restaurant..reload # counting duplicate tag categories within a specific restaurant group_by_categories = restaurant.. joins(:restaurant_tag). select("SUBSTRING_INDEX(restaurant_tags.title_en, ':', 1) AS category"). where("SUBSTRING_INDEX(restaurant_tags.title_en, ':', 1) = ?", ['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: ['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 [OpCons::ERRORS] = pt.errors if pt.present? false end |