mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
79 lines
2.3 KiB
Ruby
79 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Admin
|
|
class UsersController < Admin::BaseController
|
|
before_action :set_user, only: %i[update]
|
|
|
|
def index
|
|
authorize User
|
|
scope = policy_scope(User)
|
|
.left_joins(family: :subscription)
|
|
.includes(family: :subscription)
|
|
|
|
scope = scope.where(role: params[:role]) if params[:role].present?
|
|
scope = apply_trial_filter(scope) if params[:trial_status].present?
|
|
|
|
@users = scope.order(
|
|
Arel.sql(
|
|
"CASE " \
|
|
"WHEN subscriptions.status = 'trialing' THEN 0 " \
|
|
"WHEN subscriptions.id IS NULL THEN 1 " \
|
|
"ELSE 2 END, " \
|
|
"subscriptions.trial_ends_at ASC NULLS LAST, users.email ASC"
|
|
)
|
|
)
|
|
|
|
family_ids = @users.map(&:family_id).uniq
|
|
@accounts_count_by_family = Account.where(family_id: family_ids).group(:family_id).count
|
|
@entries_count_by_family = Entry.joins(:account).where(accounts: { family_id: family_ids }).group("accounts.family_id").count
|
|
|
|
user_ids = @users.map(&:id).uniq
|
|
@last_login_by_user = Session.where(user_id: user_ids).group(:user_id).maximum(:created_at)
|
|
@sessions_count_by_user = Session.where(user_id: user_ids).group(:user_id).count
|
|
|
|
@trials_expiring_in_7_days = Subscription
|
|
.where(status: :trialing)
|
|
.where(trial_ends_at: Time.current..7.days.from_now)
|
|
.count
|
|
end
|
|
|
|
def update
|
|
authorize @user
|
|
|
|
if @user.update(user_params)
|
|
Rails.logger.info(
|
|
"[Admin::Users] Role changed - " \
|
|
"by_user_id=#{Current.user.id} " \
|
|
"target_user_id=#{@user.id} " \
|
|
"new_role=#{@user.role}"
|
|
)
|
|
redirect_to admin_users_path, notice: t(".success")
|
|
else
|
|
redirect_to admin_users_path, alert: t(".failure")
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_user
|
|
@user = User.find(params[:id])
|
|
end
|
|
|
|
def user_params
|
|
params.require(:user).permit(:role)
|
|
end
|
|
|
|
def apply_trial_filter(scope)
|
|
case params[:trial_status]
|
|
when "expiring_soon"
|
|
scope.where(subscriptions: { status: :trialing })
|
|
.where(subscriptions: { trial_ends_at: Time.current..7.days.from_now })
|
|
when "trialing"
|
|
scope.where(subscriptions: { status: :trialing })
|
|
else
|
|
scope
|
|
end
|
|
end
|
|
end
|
|
end
|