cats_core 1.1.18 → 1.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eced600b25c5d8bf5d8a0b55ba7a5d116879c5ee3f346f9e5128144066c14d8c
4
- data.tar.gz: e88b60c25cd88cba88f0cce8ca83fe4acf6873b99b757c791a9806f3b22569c4
3
+ metadata.gz: f7535241fdd71214b07a61f4bde93042bf22673e3cddf8819686da1a7ac2b277
4
+ data.tar.gz: 5b7b01689834652de56c72f5bf836cf2b8e1b476d432beb72f691c634b38ce8f
5
5
  SHA512:
6
- metadata.gz: fb400975341e0ee1361e41f9cf93fdd1473af06f0bed0f06d75d36b0622766556110b3a3f75ab8b283776ff2c23a3263b5d493dd0035b27b965d2c31141eca78
7
- data.tar.gz: edc3f5744ff0f9b4dde1f0b8d07649927b720540004fc891e357bcba30c9561963e4f3ece3238707496f23eb1fdf0d141fad44adbcf5c4ede25a59129c3e8717
6
+ metadata.gz: 12fb00d72b5c265e1ea938fed3ec613ead395a7f34cdc78e0bf814159167eafa04d4343f1a02b3b8ffbcd840fcbb8524dcdb0bae29804a832cb0345ae61304e4
7
+ data.tar.gz: 9460758729591b77bc7108fe3f891f23ac4fed319c3c78a47dd9bb11a9fc349059cadb17ae6b511e2aa36ffc241c2a077042b6e8bfead949f12156dc199bb9dc
@@ -1,8 +1,23 @@
1
1
  module Cats
2
2
  module Core
3
3
  class RolesController < ApplicationController
4
+ include Common
5
+
4
6
  before_action :set_role, only: [:users]
5
7
 
8
+ def index
9
+ roles = Cats::Core::Role.joins(:application_module)
10
+ .where(cats_core_application_modules: { prefix: params[:prefix] })
11
+ render json: { success: true, data: serialize(roles) }
12
+ end
13
+
14
+ def unassigned_roles
15
+ user = Cats::Core::User.find(params[:id])
16
+ roles = Cats::Core::Role.where(application_module_id: user.application_module_id)
17
+ unassigned = roles - user.roles
18
+ render json: { success: true, data: serialize(unassigned) }
19
+ end
20
+
6
21
  def users
7
22
  data = ActiveModelSerializers::SerializableResource.new(@role.users)
8
23
  render json: { success: true, data: data }
@@ -13,6 +28,10 @@ module Cats
13
28
  def set_role
14
29
  @role = Role.find_by(name: params[:name])
15
30
  end
31
+
32
+ def model_params
33
+ params.require(:payload).permit(:name, :application_module_id)
34
+ end
16
35
  end
17
36
  end
18
37
  end
@@ -1,7 +1,64 @@
1
1
  module Cats
2
2
  module Core
3
3
  class UsersController < ApplicationController
4
- before_action :set_user, only: %i[stores warehouse hub]
4
+ include Common
5
+
6
+ before_action :set_user, only: %i[update roles assign_role revoke_role stores warehouse hub]
7
+
8
+ def index
9
+ users = Cats::Core::User.joins(:application_module)
10
+ .where(cats_core_application_modules: { prefix: params[:prefix] })
11
+ render json: { success: true, data: serialize(users) }
12
+ end
13
+
14
+ def create
15
+ prefix = model_params[:application_prefix]
16
+ application_module = Cats::Core::ApplicationModule.find_by(prefix: prefix)
17
+
18
+ obj = Cats::Core::User.new(model_params.except(:application_prefix))
19
+ obj.application_module = application_module
20
+ if obj.save
21
+ render json: { success: true, data: serialize(obj) }, status: :created
22
+ else
23
+ render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
24
+ end
25
+ end
26
+
27
+ def update
28
+ if model_params[:application_prefix]
29
+ prefix = model_params[:application_prefix]
30
+ application_module = Cats::Core::ApplicationModule.find_by(prefix: prefix)
31
+ @user.assign_attributes(model_params.except(:application_prefix))
32
+ @user.application_module = application_module
33
+ else
34
+ @user.assign_attributes(model_params)
35
+ end
36
+
37
+ if @user.save
38
+ render json: { success: true, data: serialize(@user) }
39
+ else
40
+ render json: { success: false, error: @user.errors.full_messages[0] }, status: :unprocessable_entity
41
+ end
42
+ end
43
+
44
+ def roles
45
+ data = ActiveModelSerializers::SerializableResource.new(@user.roles)
46
+ render json: { success: true, data: data }
47
+ end
48
+
49
+ def assign_role
50
+ role = Cats::Core::Role.find(params[:role_id])
51
+ @user.roles << role
52
+ data = ActiveModelSerializers::SerializableResource.new(role)
53
+ render json: { success: true, data: data }
54
+ end
55
+
56
+ def revoke_role
57
+ role = Cats::Core::Role.find(params[:role_id])
58
+ @user.roles.delete(role)
59
+ data = ActiveModelSerializers::SerializableResource.new(role)
60
+ render json: { success: true, data: data }
61
+ end
5
62
 
6
63
  def stores
7
64
  data = ActiveModelSerializers::SerializableResource.new(@user.stores)
@@ -21,7 +78,14 @@ module Cats
21
78
  private
22
79
 
23
80
  def set_user
24
- @user = User.find(params[:id])
81
+ @user = Cats::Core::User.find(params[:id])
82
+ end
83
+
84
+ def model_params
85
+ params.require(:payload).permit(
86
+ :first_name, :last_name, :email, :phone_number, :active, :password, :password_confirmation, :details,
87
+ :application_prefix
88
+ )
25
89
  end
26
90
  end
27
91
  end
@@ -0,0 +1,3 @@
1
+ class RoleSerializer < ActiveModel::Serializer
2
+ attributes :id, :name, :application_module_id
3
+ end
@@ -0,0 +1,3 @@
1
+ class UserSerializer < ActiveModel::Serializer
2
+ attributes :id, :first_name, :last_name, :email, :phone_number, :active, :details, :application_module_id
3
+ end
data/config/routes.rb CHANGED
@@ -5,6 +5,13 @@ Cats::Core::Engine.routes.draw do
5
5
  get '/notifications/unread', controller: :notifications, action: :unread
6
6
  get '/notifications/read', controller: :notifications, action: :read
7
7
 
8
+ resources :application_modules, param: :prefix do
9
+ member do
10
+ get 'roles', controller: :roles, action: :index
11
+ get 'users', controller: :users, action: :index
12
+ end
13
+ end
14
+
8
15
  resources :menus, only: [:index]
9
16
  resources :notifications, only: [:index] do
10
17
  member do
@@ -12,6 +19,8 @@ Cats::Core::Engine.routes.draw do
12
19
  post 'mark_as_unread', controller: :notifications, action: :mark_as_unread
13
20
  end
14
21
  end
22
+
23
+ get '/roles/:id/', controller: :roles, action: :show
15
24
  resources :roles, param: :name do
16
25
  member do
17
26
  get 'users', controller: :roles, action: :users
@@ -19,9 +28,13 @@ Cats::Core::Engine.routes.draw do
19
28
  end
20
29
  resources :users do
21
30
  member do
31
+ get 'roles'
32
+ get 'unassigned_roles', controller: :roles, action: :unassigned_roles
22
33
  get 'stores', controller: :users, action: :stores
23
34
  get 'warehouse', controller: :users, action: :warehouse
24
35
  get 'hub', controller: :users, action: :hub
36
+ post 'assign_role'
37
+ post 'revoke_role'
25
38
  end
26
39
  end
27
40
 
@@ -23,6 +23,6 @@ class CreateCatsCoreRoles < ActiveRecord::Migration[6.1]
23
23
  end
24
24
 
25
25
  add_index(:cats_core_roles, [ :name, :resource_type, :resource_id ])
26
- add_index(:cats_core_users_cats_core_roles, [ :user_id, :role_id ])
26
+ add_index(:cats_core_users_cats_core_roles, [ :user_id, :role_id ], unique: true)
27
27
  end
28
28
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.1.18'.freeze
3
+ VERSION = '1.1.22'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.18
4
+ version: 1.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-02 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -297,7 +297,9 @@ files:
297
297
  - app/serializers/cats/core/receipt_serializer.rb
298
298
  - app/serializers/cats/core/receipt_transaction_serializer.rb
299
299
  - app/serializers/cats/core/role_menu_serializer.rb
300
+ - app/serializers/cats/core/role_serializer.rb
300
301
  - app/serializers/cats/core/unit_of_measure_serializer.rb
302
+ - app/serializers/cats/core/user_serializer.rb
301
303
  - app/services/cats/core/dispatch_service.rb
302
304
  - app/services/cats/core/menu_service.rb
303
305
  - app/services/cats/core/notification_service.rb