Class: PartnerService::Restaurant::Update
- Inherits:
-
ApplicationService
- Object
- ApplicationService
- PartnerService::Restaurant::Update
- Defined in:
- app/services/partner_service/restaurant/update.rb
Instance Attribute Summary
Attributes inherited from ApplicationService
Instance Method Summary collapse
- #add_tag(restaurant_ids, tag_id) ⇒ Object
- #check_valid_restaurant_tags ⇒ Object
- #check_valid_with_outlet_type ⇒ Object
- #delete_tag(tag_id) ⇒ Object
-
#initialize(staff, restaurant = nil, params = {}) ⇒ Update
constructor
A new instance of Update.
- #update_cuisines ⇒ Object
- #update_image ⇒ Object
- #update_primary_dining_style ⇒ Object
- #update_primary_location ⇒ Object
- #update_restaurant ⇒ Object
- #update_restaurant_tags ⇒ Object
- #update_thumbnail ⇒ Object
Methods inherited from ApplicationService
Constructor Details
#initialize(staff, restaurant = nil, params = {}) ⇒ Update
Returns a new instance of Update.
6 7 8 9 10 11 12 |
# File 'app/services/partner_service/restaurant/update.rb', line 6 def initialize(staff, restaurant = nil, params = {}) @staff = staff @restaurant = restaurant || staff.default_restaurant @owner = restaurant.owner @params = params @change_tracker = PartnerService::ChangeTracker.new(staff) end |
Instance Method Details
#add_tag(restaurant_ids, tag_id) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 |
# File 'app/services/partner_service/restaurant/update.rb', line 250 def add_tag(restaurant_ids, tag_id) ActiveRecord::Base.transaction do new_data = restaurant_ids.map do |restaurant_id| Restaurant.fetch(restaurant_id).refresh_view_cache_key RestaurantTagsRestaurant.new(restaurant_tag_id: tag_id, restaurant_id: restaurant_id) end RestaurantTagsRestaurant.import! new_data, on_duplicate_key_update: %i[restaurant_id restaurant_tag_id], raise_error: true RestaurantTag.fetch(tag_id).touch end end |
#check_valid_restaurant_tags ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/services/partner_service/restaurant/update.rb', line 79 def @old_primary = RestaurantTag.where(id: params[:old_primary_id]).where('title_en LIKE ?', 'Cuisine%') @primary = RestaurantTag.where(id: params[:primary_id]).where('title_en LIKE ?', 'Cuisine%') @cuisines = RestaurantTag.where(id: params[:cuisine_ids]).where('title_en LIKE ?', 'Cuisine%') @popular_zones = RestaurantTag.where(id: params[:popular_zone_ids]).where('title_en LIKE ?', 'PopularZone%') @shopping_mall = RestaurantTag.where(id: params[:shopping_mall_id]).where('title_en LIKE ?', 'ShoppingMall%') @bts_route = RestaurantTag.where(id: params[:bts_route_id]).where('title_en LIKE ?', 'BtsRoute%') @mrt_route = RestaurantTag.where(id: params[:mrt_route_id]).where('title_en LIKE ?', 'MrtRoute%') @facilities = RestaurantTag.where(id: params[:facility_ids]).where('title_en LIKE ?', 'Facility%') begin if params[:popular_zone_ids].count <= 3 && params[:facility_ids].count <= 3 && @shopping_mall.count == 1 && @bts_route.count == 1 && @mrt_route.count == 1 && @shopping_mall.present? && @bts_route.present? && @mrt_route.present? && @popular_zones.count == params[:popular_zone_ids].count && @facilities.count == params[:facility_ids].count check_valid_with_outlet_type else false end rescue StandardError false end end |
#check_valid_with_outlet_type ⇒ Object
228 229 230 231 232 233 234 235 236 237 |
# File 'app/services/partner_service/restaurant/update.rb', line 228 def check_valid_with_outlet_type if restaurant_params[:outlet_type] != 'restaurant' true elsif params[:cuisine_ids].count <= 3 && @old_primary.count == 1 && @primary.count == 1 && @old_primary.present? && @primary.present? && @cuisines.count == params[:cuisine_ids].count true else false end end |
#delete_tag(tag_id) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'app/services/partner_service/restaurant/update.rb', line 239 def delete_tag(tag_id) ActiveRecord::Base.transaction do = restaurant..where(restaurant_tag_id: tag_id) .each do |rtr| rtr.restaurant.refresh_view_cache_key end .delete_all RestaurantTag.fetch(tag_id).touch end end |
#update_cuisines ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/services/partner_service/restaurant/update.rb', line 103 def update_cuisines old_ids = restaurant..where('title_en LIKE ?', 'Cuisine%').pluck(:id) @change_tracker.track_tag(old_ids, [params[:cuisine_ids]].flatten) old_ids -= [params[:cuisine_ids]].flatten new_ids = [params[:cuisine_ids]].flatten - old_ids old_ids.compact.each do |old_id| delete_tag(old_id) end new_ids.compact.each do |new_id| add_tag([restaurant.id], new_id) end tag_id = params[:primary_id] return if tag_id.blank? @change_tracker.track_primary_tag(restaurant, tag_id, RestaurantTag::CUISINE) PrimaryTagCpt::Operations::UpdateRelation.call(id: tag_id, restaurant_id: restaurant.id) end |
#update_image ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'app/services/partner_service/restaurant/update.rb', line 262 def update_image @change_tracker.track_changes_by_model(record: restaurant, params: { logo: params[:logo] }, nav: 'restaurant') restaurant.remote_logo_url = params[:logo] return false unless restaurant.save return if params[:cover].blank? picture = restaurant.pictures.find_or_initialize_by(item: File.basename(params[:cover])) @change_tracker.track_changes_by_model(record: picture, params: { item: params[:cover] }, nav: 'restaurant') if picture.blank? picture = restaurant.pictures.new picture.remote_item_url = params[:cover] picture.save end picture.update(cached_tag_list: 'cover') end |
#update_primary_dining_style ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'app/services/partner_service/restaurant/update.rb', line 124 def update_primary_dining_style return if params[:primary_dining_style].blank? tag_id = params[:primary_dining_style] @change_tracker.track_primary_tag(restaurant, tag_id, RestaurantTag::DINING_STYLE) PrimaryTagCpt::Operations::UpdateRelation.call(id: tag_id, restaurant_id: restaurant.id) end |
#update_primary_location ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'app/services/partner_service/restaurant/update.rb', line 134 def update_primary_location return if params[:primary_location_id].blank? tag_id = params[:primary_location_id] @change_tracker.track_primary_tag(restaurant, tag_id, RestaurantTag::LOCATION) PrimaryTagCpt::Operations::UpdateRelation.call(id: tag_id, restaurant_id: restaurant.id) end |
#update_restaurant ⇒ Object
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/services/partner_service/restaurant/update.rb', line 14 def update_restaurant = nil result = true @change_tracker.track_changes_by_model(record: restaurant, params: restaurant_params, nav: 'restaurant') restaurant.skip_validation = true restaurant.assign_attributes(restaurant_params) owner.assign_attributes(owner_params) # keep update image outside of transaction # to avoid image upload success but transaction rollback update_image update_thumbnail if restaurant.errors.present? = restaurant.errors..to_sentence return ServiceResult.new message: , errors: [] end ActiveRecord::Base.transaction do if restaurant_params[:outlet_type] == 'hotel' hotel_information = HotelInformation.find_or_create_by(restaurant: restaurant) hotel_information.assign_attributes(hotel_information_params) unless hotel_information.valid? = hotel_information.errors..to_sentence raise ActiveRecord::Rollback end end if restaurant_params[:outlet_type] == 'experience' experience_information = ExperienceInformation.find_or_create_by(restaurant: restaurant) experience_information.assign_attributes(experience_information_params) unless experience_information.valid? = experience_information.errors..to_sentence raise ActiveRecord::Rollback end end if restaurant.valid? && owner.valid? restaurant.save! owner.save! update_cuisines if restaurant_params[:outlet_type] == 'restaurant' update_primary_dining_style if restaurant_params[:outlet_type] == 'restaurant' update_primary_location if restaurant_params[:outlet_type] == 'restaurant' hotel_information.update!(hotel_information_params) if restaurant_params[:outlet_type] == 'hotel' experience_information.update!(experience_information_params) if restaurant_params[:outlet_type] == 'experience' @change_tracker.notify_managers(restaurant) if result return ServiceResult.new data: { staff: @staff, restaurant: @restaurant } else = (restaurant.errors. + staff.errors.).to_sentence raise ActiveRecord::Rollback end end ServiceResult.new message: , errors: [] rescue ActiveRecord::Rollback # Handle rollback errors if necessary ServiceResult.new message: , errors: [] end |
#update_restaurant_tags ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'app/services/partner_service/restaurant/update.rb', line 143 def old_tag_ids = [] new_tag_ids = [] # Location if params[:location_ids].present? # old_ids = restaurant.restaurant_tags.where('title_en LIKE ?', 'Location%').pluck(:id) old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag(old_ids, [params[:location_ids]].flatten) old_ids -= [params[:location_ids]].flatten new_ids = [params[:location_ids]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end # PopularZone if params[:popular_zone_ids].present? old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag(old_ids, [params[:popular_zone_ids]].flatten) old_ids -= [params[:popular_zone_ids]].flatten new_ids = [params[:popular_zone_ids]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end # ShoppingMall if params[:shopping_mall_id].present? old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag([old_ids[0]].flatten, [params[:shopping_mall_id]].flatten) old_ids -= [params[:shopping_mall_id]].flatten new_ids = [params[:shopping_mall_id]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end # BtsRoute if params[:bts_route_id].present? old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag([old_ids[0]].flatten, [params[:bts_route_id]].flatten) old_ids -= [params[:bts_route_id]].flatten new_ids = [params[:bts_route_id]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end # MrtRoute if params[:mrt_route_id].present? old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag([old_ids[0]].flatten, [params[:mrt_route_id]].flatten) old_ids -= [params[:mrt_route_id]].flatten new_ids = [params[:mrt_route_id]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end # Facility if params[:facility_ids].present? old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag(old_ids, [params[:facility_ids]].flatten) old_ids -= [params[:facility_ids]].flatten new_ids = [params[:facility_ids]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end # DiningStyle if params[:dining_style_ids].present? old_ids = restaurant..order(:id).pluck(:id) @change_tracker.track_tag(old_ids, [params[:dining_style_ids]].flatten) old_ids -= [params[:dining_style_ids]].flatten new_ids = [params[:dining_style_ids]].flatten - old_ids old_tag_ids += old_ids new_tag_ids += new_ids end old_tag_ids.compact.each do |old_tag_id| delete_tag(old_tag_id) end new_tag_ids.compact.each do |new_tag_id| add_tag([restaurant.id], new_tag_id) end end |
#update_thumbnail ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'app/services/partner_service/restaurant/update.rb', line 279 def update_thumbnail return if params[:thumbnails].nil? new_ids = [] old_ids = restaurant.pictures.by_thumbnail.pluck(:id) params[:thumbnails].each do |thumbnail| picture = restaurant.pictures.find_by(id: thumbnail[:id]) next if picture.blank? picture.update(priority: thumbnail[:priority], thumbnail: true) new_ids << thumbnail[:id].to_i end @change_tracker.track_thumbnails(old_ids, new_ids) old_ids -= new_ids old_thumbnail = restaurant.pictures.where(id: old_ids) old_thumbnail.update_all(thumbnail: false) end |