Class: Admin::GroupLandingPagesController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- ApplicationController
- BaseController
- Admin::GroupLandingPagesController
- Defined in:
- app/controllers/admin/group_landing_pages_controller.rb
Constant Summary
Constants inherited from BaseController
BaseController::INTERNAL_SERVER_ERROR_MESSAGE
Instance Method Summary collapse
- #associated_outlets ⇒ Object
- #clear_ui_cache ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
-
#group_landing_translation_status ⇒ Object
Check translation status.
- #index ⇒ Object
- #new ⇒ Object
- #show ⇒ Object
-
#translate_by_ai ⇒ Object
AI translation endpoint.
-
#translate_field_by_ai ⇒ Object
Field-level AI translation.
-
#translation_status ⇒ Object
Check field translation status.
- #update ⇒ Object
- #update_banners ⇒ Object
- #update_field_translations ⇒ Object
-
#update_tags ⇒ Object
Updates tags and ranks for a group landing page.
Methods inherited from BaseController
#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session
Methods included from LogrageCustomLogger
Methods included from AdminHelper
#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?
Methods included from UpdateLocaleConcern
Methods inherited from ApplicationController
#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=
Methods included from ControllerHelpers
#check_boolean_param, #get_banners, #inventory_params, #reservation_params
Instance Method Details
#associated_outlets ⇒ Object
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 78 79 80 81 82 83 84 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 38 def associated_outlets group_type = params.require(:group_type) group_id = params.require(:group_id) unless GroupLandingPage::GROUP_TYPES.include?(group_type) return render json: { success: false, message: "Invalid group type #{group_type}", data: nil }, status: :bad_request end if group_type == GroupLandingPage::GROUP_TYPE_BRANCH restaurants = Restaurant.includes(:translations, :restaurant_tags).active.not_expired_offers.where(branch_id: group_id) outlets = restaurants.map do |restaurant| city_name = restaurant.city.present? ? restaurant.city.name : 'All Cities' { id: restaurant.id, name: "Restaurant (#{restaurant.id}) #{city_name} - #{restaurant.name}", } end render json: { success: true, message: nil, data: { restaurants: outlets } } else compact_restaurants = CompactRestaurant.where(active: true) outlets = compact_restaurants.map do |compact_restaurant| city_name = compact_restaurant.city.present? ? compact_restaurant.city.name : 'All Cities' compact_name = if compact_restaurant.branch.present? "Branch (#{compact_restaurant.branch.id}) #{city_name} - #{compact_restaurant.name}" else "Restaurant (#{compact_restaurant.restaurant.id}) #{city_name} - #{compact_restaurant.name}" end { id: compact_restaurant.id, name: compact_name, } end render json: { success: true, message: nil, data: { compact_restaurants: outlets } } end rescue ActionController::ParameterMissing => e render json: { success: false, message: e., data: nil }, status: :bad_request rescue StandardError => e APMErrorHandler.report(e) render json: { success: false, message: e., data: nil }, status: :internal_server_error end |
#clear_ui_cache ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 516 def clear_ui_cache if @group_landing_page.nil? return render json: { success: false, message: 'Group landing page not found', data: nil }, status: :not_found end GroupLandingPages::ClearUiCacheWorker.perform_async(@group_landing_page.id) respond_to do |format| format.html do redirect_to admin_group_landing_pages_path, notice: 'Processing clear UI cache for the group landing page, please check again in the next 15 minutes' end format.json do render json: { success: true, message: 'Processing clear UI cache for the group landing page, please check again in the next 15 minutes', data: nil, } end end rescue StandardError => e APMErrorHandler.report(e, context: { controller_action: 'clear_ui_cache', group_landing_page_id: @group_landing_page&.id, }) respond_to do |format| format.html do redirect_to admin_group_landing_pages_path, alert: e. end format.json do render json: { success: false, message: e., data: nil }, status: :internal_server_error end end end |
#create ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 86 def create @group_landing_page = GroupLandingPage.new(group_landing_page_params) if @group_landing_page.save render json: { success: true, message: 'Group landing page created successfully', data: Admin::GroupLandingPageSerializer.new(@group_landing_page), } else render json: { success: false, message: @group_landing_page.errors..to_sentence, data: nil } end rescue ArgumentError => e render json: { success: false, message: e., data: nil }, status: :unprocessable_entity rescue StandardError => e APMErrorHandler.report(e) render json: { success: false, message: e., data: nil }, status: :internal_server_error end |
#destroy ⇒ Object
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 479 def destroy if @group_landing_page.nil? return render json: { success: false, message: 'Group landing page not found', data: nil }, status: :not_found end if @group_landing_page.destroy respond_to do |format| format.html do redirect_to admin_group_landing_pages_path, notice: 'Group landing page deleted successfully' end format.json do render json: { success: true, message: 'Group landing page deleted successfully', data: nil } end end else respond_to do |format| format.html do redirect_to admin_group_landing_pages_path, alert: @group_landing_page.errors..to_sentence end format.json do render json: { success: false, message: @group_landing_page.errors..to_sentence, data: nil } end end end rescue StandardError => e APMErrorHandler.report(e) respond_to do |format| format.html do redirect_to admin_group_landing_pages_path, alert: e. end format.json do render json: { success: false, message: e., data: nil }, status: :internal_server_error end end end |
#edit ⇒ Object
104 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 104 def edit; end |
#group_landing_translation_status ⇒ Object
Check translation status
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 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 185 def group_landing_translation_status translation_id = params[:translation_id] redis_key = "group_landing_translation:#{translation_id}" status_data = $persistent_redis.with { |redis| redis.get(redis_key) } if status_data.nil? return render json: { success: false, message: 'Translation request not found or expired', }, status: :not_found end parsed_status = JSON.parse(status_data) render json: { success: true, status: parsed_status['status'], message: parsed_status['message'], translations: parsed_status['translations'], error: parsed_status['error'], } rescue StandardError => e APMErrorHandler.report(e, context: { group_landing_id: @group_landing_page&.id, translation_id: translation_id, }) render json: { success: false, message: e. }, status: :internal_server_error end |
#index ⇒ Object
11 12 13 14 15 16 17 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 11 def index title: 'Group Landing Pages' @grid = ::Admin::GroupLandingPagesGrid.new(params[:admin_group_landing_pages_grid]) do |scope| scope.page(params[:page]).per(20) end end |
#new ⇒ Object
19 20 21 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 19 def new @group_landing_page = GroupLandingPage.new end |
#show ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 23 def show if @group_landing_page.nil? render json: { success: false, message: 'Group landing page not found', data: nil }, status: :not_found else render json: { success: true, message: nil, data: Admin::GroupLandingPageSerializer.new(@group_landing_page) } end rescue StandardError => e APMErrorHandler.report(e, context: { controller_action: 'show', group_landing_page_id: params[:id] || params[:group_landing_page_id], }) render json: { success: false, message: e., data: nil } end |
#translate_by_ai ⇒ Object
AI translation endpoint
139 140 141 142 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 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 139 def translate_by_ai source_language = params[:source_language] || 'en' target_languages = params[:target_languages] || [] target_fields = params[:target_fields] || [] only_blank_languages = params[:only_blank_languages] == 'true' || params[:only_blank_languages] == true only_blank_fields = params[:only_blank_fields] == 'true' || params[:only_blank_fields] == true # Generate unique request ID request_id = SecureRandom.uuid # Enqueue background job GroupLandingPages::AiTranslationWorker.perform_async( request_id, @group_landing_page.id, source_language, target_languages, target_fields, only_blank_languages, only_blank_fields, ) BUSINESS_LOGGER.info('group_landing_ai_translation_initiated', { group_landing_id: @group_landing_page.id, request_id: request_id, source_language: source_language, target_languages: target_languages, target_fields: target_fields, only_blank_languages: only_blank_languages, only_blank_fields: only_blank_fields, }) render json: { success: true, message: 'Translation job started', translation_id: request_id, } rescue StandardError => e APMErrorHandler.report(e, context: { group_landing_id: @group_landing_page&.id, source_language: source_language, target_languages: target_languages, }) render json: { success: false, message: e. }, status: :internal_server_error end |
#translate_field_by_ai ⇒ Object
Field-level AI translation
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 216 def translate_field_by_ai source_language = params[:source_language] || 'en' target_languages = params[:target_languages] || [] field_name = params[:field_name] source_text = params[:source_text] # Generate unique translation ID translation_id = SecureRandom.uuid # Enqueue background job for field translation GroupLandingPages::AiFieldTranslationWorker.perform_async( @group_landing_page.id, translation_id, field_name, source_language, source_text, target_languages, ) BUSINESS_LOGGER.info('group_landing_field_ai_translation_initiated', { group_landing_id: @group_landing_page.id, translation_id: translation_id, field_name: field_name, source_language: source_language, target_languages: target_languages, }) render json: { success: true, message: 'Field translation job started', translation_id: translation_id, } rescue StandardError => e APMErrorHandler.report(e, context: { group_landing_id: @group_landing_page&.id, field_name: field_name, source_language: source_language, }) render json: { success: false, message: e. }, status: :internal_server_error end |
#translation_status ⇒ Object
Check field translation status
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 258 def translation_status translation_id = params[:translation_id] redis_key = "group_landing_field_translation:#{@group_landing_page.id}:#{translation_id}" status_data = $persistent_redis.with { |redis| redis.get(redis_key) } if status_data.nil? return render json: { status: 'not_found', message: 'Translation request not found or expired', }, status: :not_found end parsed_status = JSON.parse(status_data) render json: { status: parsed_status['status'], data: parsed_status['translations'], error: parsed_status['error'], } rescue StandardError => e APMErrorHandler.report(e, context: { group_landing_id: @group_landing_page&.id, translation_id: translation_id, }) render json: { status: 'error', error: e. }, status: :internal_server_error end |
#update ⇒ Object
106 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 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 106 def update if @group_landing_page.nil? return render json: { success: false, message: 'Group landing page not found', data: nil }, status: :not_found end # Debug logging to check if tag_line is in params permitted_params = group_landing_page_params BUSINESS_LOGGER.info('group_landing_page_update_params', { group_landing_page_id: @group_landing_page.id, tag_line_en: permitted_params[:tag_line_en], tag_line_th: permitted_params[:tag_line_th], has_tag_line_en: permitted_params.key?(:tag_line_en), has_tag_line_th: permitted_params.key?(:tag_line_th), }) if @group_landing_page.update(permitted_params) render json: { success: true, message: 'Group landing page updated successfully', data: Admin::GroupLandingPageSerializer.new(@group_landing_page), } else render json: { success: false, message: @group_landing_page.errors..to_sentence, data: nil } end rescue ArgumentError => e render json: { success: false, message: e., data: nil }, status: :unprocessable_entity rescue StandardError => e APMErrorHandler.report(e) render json: { success: false, message: e., data: nil }, status: :internal_server_error end |
#update_banners ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 286 def if @group_landing_page.nil? return render json: { success: false, message: 'Group landing page not found', data: nil }, status: :not_found end result = false ActiveRecord::Base.transaction do if [:remove_attachments_attributes].present? = [] [:remove_attachments_attributes].each do |_, | << [:id] end @group_landing_page..where(id: ).destroy_all end if [:add_attachments_attributes].present? = [] [:add_attachments_attributes].each do |_, | # validate priority must be unique if @group_landing_page..where(priority: [:priority]).exists? raise ArgumentError, 'Banner priority must be unique' end << end @group_landing_page..create() end result = true end if result render json: { success: true, message: 'Group landing page banners updated successfully', data: Admin::GroupLandingPageSerializer.new(@group_landing_page), } else render json: { success: false, message: @group_landing_page.errors..to_sentence, data: nil } end rescue ArgumentError => e render json: { success: false, message: e., data: nil }, status: :unprocessable_entity rescue StandardError => e APMErrorHandler.report(e) render json: { success: false, message: e., data: nil }, status: :internal_server_error end |
#update_field_translations ⇒ Object
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 551 def update_field_translations group_landing_page = GroupLandingPage.find_by(id: params[:id]) unless group_landing_page render json: { success: false, message: 'Group landing page not found.' }, status: :not_found return end translations = params[:translations] || {} if translations.blank? render json: { success: false, message: 'No translations provided.' }, status: :unprocessable_entity return end begin # Update only the provided translation fields update_hash = {} translations.each do |field_key, value| # Validate field key format (e.g., "title_en", "description_th") if field_key.match?(/\A(title|description|footer_description|tag_line)_(en|th|cn|zh|ru|ko|ja|ms|fr|de|es|id|vi)\z/) update_hash[field_key] = value end end if update_hash.blank? render json: { success: false, message: 'No valid translation fields provided.' }, status: :unprocessable_entity return end # Update group landing page with only the translation fields group_landing_page.update!(update_hash) render json: { success: true, message: "Successfully updated #{update_hash.keys.size} translation field(s)", updated_fields: update_hash.keys, data: update_hash, }, status: :ok rescue ActiveRecord::RecordInvalid => e APMErrorHandler.report(e, context: { controller_action: 'update_field_translations', group_landing_page_id: group_landing_page.id, translations: translations, }) render json: { success: false, message: "Validation error: #{e.}", errors: e.record.errors., }, status: :unprocessable_entity rescue StandardError => e APMErrorHandler.report(e, context: { controller_action: 'update_field_translations', group_landing_page_id: group_landing_page.id, translations: translations, }) render json: { success: false, message: "An error occurred: #{e.}", }, status: :internal_server_error end end |
#update_tags ⇒ Object
Updates tags and ranks for a group landing page. Note: When :group_landing_page param is missing or empty, all existing groups and ranks will be removed (intentional deletion behavior).
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'app/controllers/admin/group_landing_pages_controller.rb', line 338 def if @group_landing_page.nil? return render json: { success: false, message: 'Group landing page not found', data: nil }, status: :not_found end ActiveRecord::Base.transaction do if [:group_landing_page_groups_attributes].present? existing_glp_groups = @group_landing_page.group_landing_page_groups.map do |group| { group_type: group.group_type.to_s, group_id: group.group_id.to_i, priority: group.priority.to_i, rank_sort_type: group.rank_sort_type.to_s, } end new_glp_groups = [:group_landing_page_groups_attributes].values.map do |group| { group_type: group[:group_type].to_s, group_id: group[:group_id].to_i, priority: group[:priority].to_i, rank_sort_type: group[:rank_sort_type].to_s, } end groups_to_add = new_glp_groups - existing_glp_groups groups_to_remove = existing_glp_groups - new_glp_groups groups_to_remove.each do |group| @group_landing_page.group_landing_page_groups.where( group_type: group[:group_type], group_id: group[:group_id], ).destroy_all end @group_landing_page.update!(group_landing_page_groups_attributes: groups_to_add) else # if not present or empty, remove all existing groups @group_landing_page.group_landing_page_groups.destroy_all end if [:glp_restaurant_ranks_attributes].present? existing_glp_restaurant_ranks = @group_landing_page.glp_restaurant_ranks.map do |rank| { group_type: rank.group_type.to_s, group_id: rank.group_id.to_i, restaurant_id: rank.restaurant_id.to_i, rank: rank.rank.to_i, is_ads: rank.is_ads, ads_start_date: rank.ads_start_date, ads_end_date: rank.ads_end_date, } end new_glp_restaurant_ranks = [:glp_restaurant_ranks_attributes].values.map do |rank| { group_type: rank[:group_type].to_s, group_id: rank[:group_id].to_i, restaurant_id: rank[:restaurant_id].to_i, rank: rank[:rank].to_i, is_ads: rank[:is_ads], ads_start_date: rank[:ads_start_date], ads_end_date: rank[:ads_end_date], } end glp_restaurant_ranks_to_add = new_glp_restaurant_ranks - existing_glp_restaurant_ranks glp_restaurant_ranks_to_remove = existing_glp_restaurant_ranks - new_glp_restaurant_ranks glp_restaurant_ranks_to_remove.each do |rank| @group_landing_page.glp_restaurant_ranks.where( group_type: rank[:group_type], group_id: rank[:group_id], restaurant_id: rank[:restaurant_id], ).destroy_all end @group_landing_page.update!(glp_restaurant_ranks_attributes: glp_restaurant_ranks_to_add) else # if not present or empty, remove all existing ranks @group_landing_page.glp_restaurant_ranks.destroy_all end if [:glp_compact_restaurant_ranks_attributes].present? existing_glp_compact_restaurant_ranks = @group_landing_page.glp_compact_restaurant_ranks.map do |rank| { group_type: rank.group_type.to_s, group_id: rank.group_id.to_i, compact_restaurant_id: rank.compact_restaurant_id.to_i, rank: rank.rank.to_i, is_ads: rank.is_ads, ads_start_date: rank.ads_start_date, ads_end_date: rank.ads_end_date, } end new_glp_compact_restaurant_ranks = [:glp_compact_restaurant_ranks_attributes].values.map do |rank| { group_type: rank[:group_type].to_s, group_id: rank[:group_id].to_i, compact_restaurant_id: rank[:compact_restaurant_id].to_i, rank: rank[:rank].to_i, is_ads: rank[:is_ads], ads_start_date: rank[:ads_start_date], ads_end_date: rank[:ads_end_date], } end glp_compact_restaurant_ranks_to_add = new_glp_compact_restaurant_ranks - existing_glp_compact_restaurant_ranks glp_compact_restaurant_ranks_to_remove = existing_glp_compact_restaurant_ranks - new_glp_compact_restaurant_ranks glp_compact_restaurant_ranks_to_remove.each do |rank| @group_landing_page.glp_compact_restaurant_ranks.where( group_type: rank[:group_type], group_id: rank[:group_id], compact_restaurant_id: rank[:compact_restaurant_id], ).destroy_all end @group_landing_page.update!(glp_compact_restaurant_ranks_attributes: glp_compact_restaurant_ranks_to_add) else # if not present or empty, remove all existing ranks @group_landing_page.glp_compact_restaurant_ranks.destroy_all end render json: { success: true, message: 'Group landing page tags updated successfully', data: Admin::GroupLandingPageSerializer.new(@group_landing_page), } rescue ArgumentError => e render json: { success: false, message: e., data: nil }, status: :unprocessable_entity rescue StandardError => e APMErrorHandler.report(e) render json: { success: false, message: e., data: nil }, status: :internal_server_error end end |