Class: Cats::Core::UsersController

Inherits:
ApplicationController show all
Includes:
Common
Defined in:
app/controllers/cats/core/users_controller.rb

Instance Method Summary collapse

Methods included from Common

#show

Methods inherited from ApplicationController

#authenticate, #current_user, #skip_bullet

Instance Method Details

#assign_roleObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/cats/core/users_controller.rb', line 59

def assign_role
  role = Role.find(params[:role_id])
  user = set_object
  user.roles << role

  case role.name
  when "warehouse_manager"
    user.add_detail(:warehouse, params[:warehouse_id])
  when "hub_manager"
    user.add_detail(:hub, params[:hub_id])
  when "regional_manager"
    user.add_detail(:region, params[:region_id])
  end

  render json: {success: true, data: serialize(role)}
end

#change_passwordObject



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/cats/core/users_controller.rb', line 44

def change_password
  service = UserService.new
  payload = params.require(:payload).permit(:email, :old_password, :password, :password_confirmation)
  email, old_pass, new_pass, confirmation = *payload.values
  user = service.change_password(email, old_pass, new_pass, confirmation)

  render json: {success: true, data: serialize(user)}
rescue StandardError => e
  render json: {success: false, error: e}
end

#createObject



15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/cats/core/users_controller.rb', line 15

def create
  super do
    prefix = model_params[:application_prefix]
    application_module = ApplicationModule.find_by(prefix: prefix)

    obj = User.new(model_params.except(:application_prefix))
    obj.application_module = application_module
    obj
  end
end

#hubObject



92
93
94
# File 'app/controllers/cats/core/users_controller.rb', line 92

def hub
  render json: {success: true, data: serialize(set_object.hub)}
end

#indexObject



9
10
11
12
13
# File 'app/controllers/cats/core/users_controller.rb', line 9

def index
  super do
    User.joins(:application_module).where(cats_core_application_modules: {prefix: params[:prefix]})
  end
end

#revoke_roleObject



76
77
78
79
80
81
82
# File 'app/controllers/cats/core/users_controller.rb', line 76

def revoke_role
  user = set_object
  role = Role.find(params[:role_id])
  user.roles.delete(role)
  user.remove_detail(role.name) if %w[warehouse_manager hub_manager regional_manager].include?(role.name)
  render json: {success: true, data: serialize(role)}
end

#rolesObject



55
56
57
# File 'app/controllers/cats/core/users_controller.rb', line 55

def roles
  render json: {success: true, data: serialize(set_object.roles)}
end

#storesObject



84
85
86
# File 'app/controllers/cats/core/users_controller.rb', line 84

def stores
  render json: {success: true, data: serialize(set_object.stores)}
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/cats/core/users_controller.rb', line 26

def update
  user = set_object
  if model_params[:application_prefix]
    prefix = model_params[:application_prefix]
    application_module = ApplicationModule.find_by(prefix: prefix)
    user.assign_attributes(model_params.except(:application_prefix))
    user.application_module = application_module
  else
    user.assign_attributes(model_params)
  end

  if user.save
    render json: {success: true, data: serialize(user)}
  else
    render json: {success: false, error: user.errors.full_messages[0]}, status: :unprocessable_entity
  end
end

#warehouseObject



88
89
90
# File 'app/controllers/cats/core/users_controller.rb', line 88

def warehouse
  render json: {success: true, data: serialize(set_object.warehouse)}
end