Class: Admin::Restaurants::FeaturedRestaurantsController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/restaurants/featured_restaurants_controller.rb

Overview

typed: ignore frozen_string_literal: true

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Method Summary collapse

Methods inherited from BaseController

#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session

Methods included from LogrageCustomLogger

#append_info_to_payload

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

#setup_locale

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

#destroyObject



44
45
46
47
48
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 44

def destroy
  @restaurant = CompactRestaurant.find(params.require(:id))
  flash[:alert] = @restaurant.errors.full_messages.to_sentence unless @restaurant.update rank: nil
  redirect_to admin_restaurants_featured_restaurants_path
end

#editObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 18

def edit
  @restaurant = CompactRestaurant.find(params.require(:id))
  @collections = compact_restaurants
  @covers = if @restaurant.branch_id.present?
              restaurants = Restaurant.where(branch_id: @restaurant.branch_id)
              covers = restaurants.map do |restaurant|
                restaurant.pictures.covers
              end.flatten.compact
              covers || []
            else
              @restaurant.restaurant.pictures.covers || []
            end
end


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
137
138
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
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 106

def featured_restaurant_translation_status
  translation_id = params[:translation_id]

  if translation_id.blank?
    render json: { success: false, message: 'Translation ID is required.' },
           status: :unprocessable_entity
    return
  end

  begin
    redis_key = "featured_restaurant_translation:#{translation_id}"
    result_json = $persistent_redis.with { |redis| redis.get(redis_key) }

    if result_json.nil?
      # Job is still processing or ID is invalid
      render json: {
        success: true,
        status: 'processing',
        message: 'Translation is still in progress...',
      }, status: :ok
    else
      result = JSON.parse(result_json)

      case result['status']
      when 'completed'
        render json: {
          success: true,
          status: 'completed',
          translations: result['translations'],
          message: result['message'],
          completed_at: result['completed_at'],
        }, status: :ok
      when 'failed'
        render json: {
          success: false,
          status: 'failed',
          error: result['error'],
          failed_at: result['failed_at'],
        }, status: :unprocessable_entity
      when 'processing'
        # Worker has stored processing status in Redis
        render json: {
          success: true,
          status: 'processing',
          message: result['message'] || 'Translation is still in progress...',
          started_at: result['started_at'],
        }, status: :ok
      else
        # Unknown status - treat as processing
        render json: {
          success: true,
          status: 'processing',
          message: 'Translation status unknown, still processing...',
        }, status: :ok
      end
    end
  rescue StandardError => e
    APMErrorHandler.report(e, context: { translation_id: translation_id })
    render json: {
      success: false,
      message: "Failed to check translation status: #{e.message}",
    }, status: :internal_server_error
  end
end

#indexObject



7
8
9
10
11
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 7

def index
  @restaurants = CompactRestaurant.where.not(rank: nil).order('compact_restaurants.rank ASC')
  etag = CityHash.hash32([self.class.to_s, @restaurants.cache_key, I18n.locale])
  return unless stale?(etag: etag)
end

#newObject



13
14
15
16
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 13

def new
  @restaurant = CompactRestaurant.new
  @collections = compact_restaurants
end

#translate_by_aiObject



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 50

def translate_by_ai
  featured_restaurant = CompactRestaurant.find_by(id: params[:id])
  unless featured_restaurant
    render json: { success: false, message: 'Featured restaurant not found.' }, status: :not_found
    return
  end

  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'

  # Only validate selections if blank field mode is NOT enabled
  if !only_blank_languages && target_languages.blank?
    render json: { success: false, message: 'Please select target languages or enable blank field detection.' },
           status: :unprocessable_entity
    return
  end

  if !only_blank_fields && target_fields.blank?
    render json: { success: false, message: 'Please select target fields or enable blank field detection.' },
           status: :unprocessable_entity
    return
  end

  begin
    # Generate unique translation ID for async processing
    translation_id = SecureRandom.uuid

    # Enqueue background job for async translation
    FeaturedRestaurants::AiTranslationWorker.perform_async(
      translation_id,
      featured_restaurant.id,
      source_language,
      target_languages,
      target_fields,
      only_blank_languages,
      only_blank_fields,
    )

    # Return immediately with translation ID for status polling
    render json: {
      success: true,
      translation_id: translation_id,
      message: 'Translation job started. Use the translation_id to check status.',
    }, status: :accepted
  rescue StandardError => e
    APMErrorHandler.report(e, context: { featured_restaurant_id: featured_restaurant.id })
    render json: {
      success: false,
      message: "Failed to start translation: #{e.message}",
    }, status: :internal_server_error
  end
end

#updateObject



32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/admin/restaurants/featured_restaurants_controller.rb', line 32

def update
  @restaurant = CompactRestaurant.find(params.require(:compact_restaurant).require(:real_id))
  @restaurant.updated_by = 'admin'
  if @restaurant.update permitted_params.merge(is_cover_set: true)
    redirect_to admin_restaurants_featured_restaurants_path
  else
    @collections = compact_restaurants
    flash[:alert] = @restaurant.errors.full_messages.to_sentence
    render :edit
  end
end