Class: Dscf::Core::AccountManagementController

Inherits:
ApplicationController show all
Includes:
Common
Defined in:
app/controllers/dscf/core/account_management_controller.rb

Instance Method Summary collapse

Methods included from Common

#create, #show, #update

Methods included from Authorizable

#authorize, #authorize_action!, #policy_scope, #pundit_user

Methods included from Filterable

#filter_records

Methods included from JsonResponse

#render_error, #render_success, #serialize

Methods included from Pagination

#default_per_page, #order_by, #order_direction, #page_no, #paginate, #paginate_offset, #pagination_links, #per_page

Methods included from TokenAuthenticatable

#require_valid_refresh_token, #validate_device_consistency, #validate_token_expiry

Methods included from Authenticatable

#authenticate_user, #authenticate_user!, #current_user, #refresh_token, #sign_in, #sign_out

Instance Method Details

#activateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/dscf/core/account_management_controller.rb', line 58

def activate
  @obj = find_record
  authorize @obj, :activate?, policy_class: Dscf::Core::AccountManagementPolicy

  return render_error(errors: "User is already active", status: :unprocessable_entity) if @obj.active?

  if @obj.update(status: :active, suspended_at: nil)
    notification = Dscf::Core::Notification.create!(
      notifiable: @obj,
      recipient: @obj,
      notification_type: :activation,
      title: "Account Activated",
      body: params[:reason] || "Your account has been activated"
    )
    Dscf::Core::NotificationService.deliver(notification)

    render_success(data: @obj)
  else
    render_error(errors: @obj.errors.full_messages[0], status: :unprocessable_entity)
  end
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/dscf/core/account_management_controller.rb', line 6

def index
  authorize @clazz.new, :index?, policy_class: Dscf::Core::AccountManagementPolicy

  data = policy_scope(@clazz)
  data = data.includes(eager_loaded_associations) if eager_loaded_associations.present?
  data = filter_records(data)

  total_count = data.count if params[:page]
  data = data.then(&paginate) if params[:page]

  options = {}
  includes = serializer_includes_for_action(:index)
  options[:include] = includes if includes.present?

  if params[:page]
    total_pages = (total_count.to_f / per_page).ceil
    count = data.is_a?(Array) ? data.length : data.count
    options[:pagination] = {
      current_page: page_no,
      per_page: per_page,
      count: count,
      total_count: total_count,
      total_pages: total_pages,
      links: pagination_links(total_pages)
    }
  end

  render_success(data: data, serializer_options: options)
end

#suspendObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/dscf/core/account_management_controller.rb', line 36

def suspend
  @obj = find_record
  authorize @obj, :suspend?, policy_class: Dscf::Core::AccountManagementPolicy

  return render_error(errors: "User is already suspended", status: :unprocessable_entity) if @obj.suspended?

  if @obj.update(status: :suspended, suspended_at: Time.current, suspension_reason: params[:suspension_reason])
    notification = Dscf::Core::Notification.create!(
      notifiable: @obj,
      recipient: @obj,
      notification_type: :suspension,
      title: "Account Suspended",
      body: params[:suspension_reason] || "Your account has been suspended"
    )
    Dscf::Core::NotificationService.deliver(notification)

    render_success(data: @obj)
  else
    render_error(errors: @obj.errors.full_messages[0], status: :unprocessable_entity)
  end
end