cats_core 1.1.19 → 1.1.23

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: 4ac17edba232672ae0f11dc2042dbb8c58f7a3bbe6f5f032e3407a23292b1db8
4
- data.tar.gz: d35a4ea6ab6e34d1a3ea13b784056916a22dc60d9da2ab9e015d34863dd7173d
3
+ metadata.gz: 4af6924e969566f74cdb292747651afd308688bc78b8ae4a1f984c9899bc17c8
4
+ data.tar.gz: 78efc6656b32a0455345b69e35d11d0a599707b61da25c68177e77c1184c3598
5
5
  SHA512:
6
- metadata.gz: c46e405c2735c8a73c814c71429c2bfeebf2a39f8130aa574e750f996006722fed3bc7a90508fab437359c9530f50578060b3c619b5cc2a029f617515df92529
7
- data.tar.gz: c53c84bc2f39768f72eaeab44bcee50e70d621028a72bace55aa263a618b4eaf69249ca5969defad1060bbe5428bb1d60ae15926de4028eabed76a4b7a7d8ad8
6
+ metadata.gz: 48502a731da0f5ebf90a0497b86a8585a3af825b3905936e4701bb2ec991766028630be363c9ec6d79d585f3156753b77736e4fb53b14c9f78231b1242c69ba3
7
+ data.tar.gz: 306057c32ef4f4a6636aaf6321a5b80f682465de990cad4c10977928b6dde075b02923bdb4d14195e3bb4774d08843772cb7978cee6b4f1427480ae84c001cd0
@@ -5,6 +5,19 @@ module Cats
5
5
 
6
6
  before_action :set_role, only: [:users]
7
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
+
8
21
  def users
9
22
  data = ActiveModelSerializers::SerializableResource.new(@role.users)
10
23
  render json: { success: true, data: data }
@@ -3,13 +3,44 @@ module Cats
3
3
  class UsersController < ApplicationController
4
4
  include Common
5
5
 
6
- before_action :set_user, only: %i[roles assign_role stores warehouse hub]
6
+ before_action :set_user, only: %i[update roles assign_role revoke_role stores warehouse hub]
7
7
 
8
8
  def index
9
- users = Cats::Core::User.where(application_module_id: params[:id])
9
+ users = Cats::Core::User.joins(:application_module)
10
+ .where(cats_core_application_modules: { prefix: params[:prefix] })
10
11
  render json: { success: true, data: serialize(users) }
11
12
  end
12
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
+
13
44
  def roles
14
45
  data = ActiveModelSerializers::SerializableResource.new(@user.roles)
15
46
  render json: { success: true, data: data }
@@ -18,7 +49,20 @@ module Cats
18
49
  def assign_role
19
50
  role = Cats::Core::Role.find(params[:role_id])
20
51
  @user.roles << role
21
- data = ActiveModelSerializers::SerializableResource.new(@user)
52
+ if role.name == 'warehouse_manager'
53
+ @user.add_detail(:warehouse, params[:warehouse_id])
54
+ elsif role.name =='hub_manager'
55
+ @user.add_detail(:hub, params[:hub_id])
56
+ end
57
+
58
+ data = ActiveModelSerializers::SerializableResource.new(role)
59
+ render json: { success: true, data: data }
60
+ end
61
+
62
+ def revoke_role
63
+ role = Cats::Core::Role.find(params[:role_id])
64
+ @user.roles.delete(role)
65
+ data = ActiveModelSerializers::SerializableResource.new(role)
22
66
  render json: { success: true, data: data }
23
67
  end
24
68
 
@@ -46,7 +90,7 @@ module Cats
46
90
  def model_params
47
91
  params.require(:payload).permit(
48
92
  :first_name, :last_name, :email, :phone_number, :active, :password, :password_confirmation, :details,
49
- :application_module_id
93
+ :application_prefix
50
94
  )
51
95
  end
52
96
  end
@@ -31,6 +31,11 @@ module Cats
31
31
  Cats::Core::Location.find_by(id: details['hub'], location_type: Cats::Core::Location::HUB)
32
32
  end
33
33
 
34
+ def add_detail(key, value)
35
+ details[key] = value
36
+ save!
37
+ end
38
+
34
39
  def validate_phone_number
35
40
  return unless phone_number.present?
36
41
 
data/config/routes.rb CHANGED
@@ -5,7 +5,12 @@ 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
- get '/application_modules/:id/users', controller: :users, action: :index, as: :application_users
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
9
14
 
10
15
  resources :menus, only: [:index]
11
16
  resources :notifications, only: [:index] do
@@ -24,10 +29,12 @@ Cats::Core::Engine.routes.draw do
24
29
  resources :users do
25
30
  member do
26
31
  get 'roles'
32
+ get 'unassigned_roles', controller: :roles, action: :unassigned_roles
27
33
  get 'stores', controller: :users, action: :stores
28
34
  get 'warehouse', controller: :users, action: :warehouse
29
35
  get 'hub', controller: :users, action: :hub
30
36
  post 'assign_role'
37
+ post 'revoke_role'
31
38
  end
32
39
  end
33
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.19'.freeze
3
+ VERSION = '1.1.23'.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.19
4
+ version: 1.1.23
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-04 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