Class: Admin::UsersController

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

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

#booking_same_emailsObject



64
65
66
67
68
69
# File 'app/controllers/admin/users_controller.rb', line 64

def booking_same_emails
  @grid = Admin::Users::ReservationsGrid.new(params[:admin_users_reservations_grid]) do |scope|
    scope.where(email: @user.email, user_id: nil).page(params[:page])
  end
  render 'admin/users/_booking_same_email', layout: false
end

#booking_same_phonesObject



57
58
59
60
61
62
# File 'app/controllers/admin/users_controller.rb', line 57

def booking_same_phones
  @grid = Admin::Users::ReservationsGrid.new(params[:admin_users_reservations_grid]) do |scope|
    scope.where(user_id: nil).where('phone LIKE ?', "%#{@user.phone_v2}").page(params[:page])
  end
  render 'admin/users/_booking_same_phone', layout: false
end

#bookingsObject



29
30
31
32
33
34
# File 'app/controllers/admin/users_controller.rb', line 29

def bookings
  @grid = Admin::Users::ReservationsGrid.new(params[:admin_users_reservations_grid]) do |scope|
    scope.where(user_id: @user.id).page(params[:page])
  end
  render 'admin/users/_bookings', layout: false
end

#confirmObject



158
159
160
161
162
163
164
# File 'app/controllers/admin/users_controller.rb', line 158

def confirm
  if @user.confirm
    redirect_back fallback_location: back_fallback_location, notice: 'User confirmed successfully'
  else
    redirect_back fallback_location: back_fallback_location, alert: @user.errors.full_messages.to_sentence
  end
end

#delete_accountObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/admin/users_controller.rb', line 118

def 
  if request.post?
    reason = params[:reason]
    user_delete = UserService::Delete.new(@user.id, reason)

    if user_delete.valid? && user_delete.suspend
      redirect_to admin_users_path, notice: 'User deleted successfully'
    else
      redirect_back fallback_location: back_fallback_location, alert: user_delete.error_message_simple
    end

  else
    language = MyLocaleManager.normalize_locale
    language = :en if language.to_sym != :th

    @reason_list = User.deletion_reasons(language)
  end
end

#editObject



22
# File 'app/controllers/admin/users_controller.rb', line 22

def edit; end

#favoritesObject



90
91
92
93
94
95
# File 'app/controllers/admin/users_controller.rb', line 90

def favorites
  @grid = Admin::Users::FavsGrid.new(params[:admin_user_favorites_grid]) do |scope|
    scope.where(user: @user).page(params[:page])
  end
  render 'admin/users/_favorites', layout: false
end

#flagObject



148
149
150
151
152
153
154
155
156
# File 'app/controllers/admin/users_controller.rb', line 148

def flag
  flag = !@user.flag
  if @user.update(flag: flag)
    message = flag ? 'User flagged successfully' : 'User unflagged successfully'
    redirect_back fallback_location: back_fallback_location, notice: message
  else
    redirect_back fallback_location: back_fallback_location, alert: @user.errors.full_messages.to_sentence
  end
end

#heat_mapObject



166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/admin/users_controller.rb', line 166

def heat_map
  @title = 'User Location Heat Map'
  if params[:restaurant_ids].present?
    @locations = Reservation.where(restaurant_id: params[:restaurant_ids].to_s.split(',')).joins(user: :devices).where.not(user_devices: { lat: nil, lng: nil }).pluck(
      :lat, :lng
    )
  else
    @locations = []
  end
end

#indexObject



14
15
16
17
18
19
20
# File 'app/controllers/admin/users_controller.rb', line 14

def index
  @grid = UsersGrid.new(params[:users_grid]) do |scope|
    scope.page(params[:page])
  end
  etag = CityHash.hash32([self.class.to_s, @grid.assets.cache_key])
  nil unless stale?(etag: etag)
end

#make_user_adminObject



177
178
179
180
181
182
183
184
# File 'app/controllers/admin/users_controller.rb', line 177

def make_user_admin
  provider = { provider: 'google_oauth2' }
  if @user.update(provider)
    render json: { status: true }
  else
    render json: { status: false, message: @user.errors.full_messages.to_sentence }
  end
end

#make_view_as_userObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/controllers/admin/users_controller.rb', line 195

def make_view_as_user
  if @user.encrypted_password_backup.present?
    render json: { status: false, message: 'Debugging is still in progress.' }
    return
  end

  @user.encrypted_password_backup = @user.encrypted_password
  @user.password = @user.password_confirmation = '123456'

  if @user.valid?
    begin
      @user.save!
      Rails.logger.info("User ID for worker: #{@user.id}")
      # Enqueue the Sidekiq worker to restore the password after 5 minutes
      ::Users::RestorePasswordWorker.perform_in(5.minutes, @user.id)
      render json: { status: true }
    rescue ActiveRecord::RecordInvalid => e
      render json: { status: false, message: e.message }
    end
  else
    render json: { status: false, message: @user.errors.full_messages.to_sentence }
  end
end

#referralsObject



50
51
52
53
54
55
# File 'app/controllers/admin/users_controller.rb', line 50

def referrals
  @grid = Admin::Users::ReferralsGrid.new(params[:admin_users_referrals_grid]) do |_scope|
    @user.referrals.page(params[:page])
  end
  render 'admin/users/_referrals', layout: false
end

#remove_user_adminObject



186
187
188
189
190
191
192
193
# File 'app/controllers/admin/users_controller.rb', line 186

def remove_user_admin
  provider = { provider: nil }
  if @user.update(provider)
    render json: { status: true }
  else
    render json: { status: false, message: @user.errors.full_messages.to_sentence }
  end
end

#restaurantsObject



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

def restaurants
  @grid = Admin::Users::RestaurantsGrid.new(params[:admin_users_restaurants_grid]) do |_scope|
    @user.restaurants.page(params[:page])
  end
  render 'admin/users/_restaurants', layout: false
end

#reviewsObject



71
72
73
74
75
76
# File 'app/controllers/admin/users_controller.rb', line 71

def reviews
  @grid = Admin::Users::ReviewsGrid.new(params[:admin_users_review_grid]) do |scope|
    scope.where(user: @user).page(params[:page])
  end
  render 'admin/users/_reviews', layout: false
end

#rewardsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/admin/users_controller.rb', line 78

def rewards
  @grid = Admin::Users::RewardsGrid.new(params[:admin_users_rewards_grid]) do |scope|
    order_param = params.dig(:admin_users_rewards_grid, :order)
    if order_param.present?
      scope.where(user: @user).page(params[:page])
    else
      scope.where(user: @user).page(params[:page]).order('rewards.id DESC')
    end
  end
  render 'admin/users/_rewards', layout: false
end

#showObject



24
25
26
27
# File 'app/controllers/admin/users_controller.rb', line 24

def show
  @limit_points_adjustment_active = LimitPointsAdjustment.find_by(status: 1, ended_at: nil)
  @limit_point_default = LimitPointsAdjustment.column_defaults
end

#skip_marketing_emailObject



137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/admin/users_controller.rb', line 137

def skip_marketing_email
  message = 'User unsubscribed from marketing emails successfully'
  if @user.subscribe_marketing_email?
    @user.update!(subscribe_marketing_email: false)
  end
  redirect_back fallback_location: back_fallback_location, notice: message
rescue StandardError => e
  APMErrorHandler.report("Failed to skip marketing email for user: #{@user.id}", error: e)
  redirect_back fallback_location: back_fallback_location, alert: 'Failed to unsubscribe user from marketing emails'
end

#updateObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/admin/users_controller.rb', line 97

def update
  permitted_params = params.require(:user).permit(:email, :username, :phone,
                                                  :verify_code, :verified,
                                                  :calling_code, :phone_v2,
                                                  :r_code, :is_account_manager)
  permitted_params.tap do |p|
    if params[:user][:date].present? && params[:user][:month].present?
      d = params.permit(user: %i[date month]).fetch(:user, {})
      current_year = Time.zone.now.year
      date = "#{d[:date]}/#{d[:month]}/#{current_year}"
      p[:birthday] = Date.parse(date, '%d/%m/%Y')
    end
  end
  if @user.update(permitted_params)
    redirect_to admin_users_path, notice: 'User updated successfully'
  else
    flash[:error] = @user.errors.full_messages.to_sentence
    render :edit
  end
end

#vouchersObject



36
37
38
39
40
41
# File 'app/controllers/admin/users_controller.rb', line 36

def vouchers
  @grid = Admin::Users::VouchersGrid.new(params[:admin_users_vouchers_grid]) do |scope|
    scope.where(user_id: @user.id).page(params[:page])
  end
  render 'admin/users/_vouchers', layout: false
end