cats_core 1.0.7 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f63ca1ecd41f30f836b69ec7fe22516bfaa33bf2a11ee4ec5e6caed08e03e9bf
4
- data.tar.gz: fa4684f9b135314f7a4ffd8a9f193d999075b01ee5dbdbd2468069bedac98453
3
+ metadata.gz: 32c70181bd52a346e553ef01325a87cb1c72ba19d831bc1b9ebe6e735b792a32
4
+ data.tar.gz: 00f2b8a381e9d969547fe5a16338b8f02ad374f0dfe40db14b1074614877e6bf
5
5
  SHA512:
6
- metadata.gz: 6155d6dde44af9a51af349e9528c1e139cfadb333f90324095a608f95aee7d2c5335f35965839d4f273083530d966188736047d9a9c451fa842cf028eb6cfbda
7
- data.tar.gz: e015635c7c13d4418ecaec59c7d103831f362896732a6386369a22e12c7aa71fdf8e0654a149e8b7926491a6a771f0c5717e7a82e3666a6f9aa8abfb66e446a3
6
+ metadata.gz: 5810cd3ad764b6e64d5ec9ba1a86591b0bd25d26051aea58da57ec8bb2fd768d62566aa08092d82a045b46c6af0327d57a40b2dc4062dae69456686135cb9e35
7
+ data.tar.gz: d25a81609c51dbce17369920073f40295d88c93f91533199e795fc06362f690d36e3c754d5b86c3b2893fc7212b02334e9a02eea820d5565c8a5dc11b4ba317f
@@ -0,0 +1,37 @@
1
+ module Cats
2
+ module Core
3
+ class AccessController < ApplicationController
4
+ skip_before_action :authenticate, only: [:login]
5
+
6
+ def login
7
+ user = Cats::Core::User.find_by(email: auth_params[:email])
8
+ if user
9
+ if user.authenticate(auth_params[:password])
10
+ roles = user.roles.map(&:name)
11
+
12
+ unless roles.count.positive?
13
+ render json: { error: 'User has no roles.' }, status: :unprocessable_entity
14
+ return
15
+ end
16
+
17
+ payload = {
18
+ id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles
19
+ }
20
+ jwt = Cats::Core::TokenAuthService.issue(payload)
21
+ render json: { token: jwt, user: payload }
22
+ else
23
+ rener json: { error: 'Invalid username or password.' }, status: 400
24
+ end
25
+ else
26
+ render json: { error: 'User does not exist.' }, status: 400
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def auth_params
33
+ params.require(:auth).permit(:email, :password)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,6 +1,6 @@
1
1
  module Cats
2
2
  module Core
3
- class ApplicationController < ActionController::Base
3
+ class ApplicationController < ActionController::API
4
4
  before_action :authenticate
5
5
 
6
6
  def current_user
@@ -0,0 +1,19 @@
1
+ module Cats
2
+ module Core
3
+ class MenusController < ApplicationController
4
+ before_action :set_service
5
+
6
+ def index
7
+ menu = @service.fetch_menu(current_user)
8
+ data = ActiveModelSerializers::SerializableResource.new(menu, each_serializer: RoleMenuSerializer)
9
+ render json: { success: true, data: data }
10
+ end
11
+
12
+ private
13
+
14
+ def set_service
15
+ @service = MenuService.new
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,14 +1,36 @@
1
1
  module Cats
2
2
  module Core
3
3
  class NotificationsController < ApplicationController
4
+ before_action :set_notification, only: %i[mark_as_read mark_as_unread]
4
5
  def index
5
- data = { success: true, data: current_user.notifications.newest_first }
6
- render json: data
6
+ data = Notification.messages(current_user.notifications.newest_first)
7
+ render json: { success: true, data: data }
8
+ end
9
+
10
+ def read
11
+ data = Notification.messages(current_user.notifications.read.newest_first)
12
+ render json: { success: true, data: data }
7
13
  end
8
14
 
9
15
  def unread
10
- data = { success: true, data: current_user.notifications.unread.newest_first }
11
- render json: data
16
+ data = Notification.messages(current_user.notifications.unread.newest_first)
17
+ render json: { success: true, data: data }
18
+ end
19
+
20
+ def mark_as_read
21
+ @notification.mark_as_read!
22
+ render json: { success: true, data: @notification.message }
23
+ end
24
+
25
+ def mark_as_unread
26
+ @notification.mark_as_unread!
27
+ render json: { success: true, data: @notification.message }
28
+ end
29
+
30
+ private
31
+
32
+ def set_notification
33
+ @notification = Notification.find(params[:id])
12
34
  end
13
35
  end
14
36
  end
@@ -0,0 +1,18 @@
1
+ module Cats
2
+ module Core
3
+ class RolesController < ApplicationController
4
+ before_action :set_role, only: [:users]
5
+
6
+ def users
7
+ data = ActiveModelSerializers::SerializableResource.new(@role.users)
8
+ render json: { success: true, data: data }
9
+ end
10
+
11
+ private
12
+
13
+ def set_role
14
+ @role = Role.find(params[:id])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Cats
2
+ module Core
3
+ class UsersController < ApplicationController
4
+ before_action :set_user, only: %i[stores warehouses]
5
+
6
+ def stores
7
+ data = ActiveModelSerializers::SerializableResource.new(@user.stores)
8
+ render json: { success: true, data: data }
9
+ end
10
+
11
+ def warehouses
12
+ data = ActiveModelSerializers::SerializableResource.new(@user.warehouses)
13
+ render json: { success: true, data: data }
14
+ end
15
+
16
+ private
17
+
18
+ def set_user
19
+ @user = User.find(params[:id])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -6,7 +6,11 @@ module Cats
6
6
  belongs_to :recipient, polymorphic: true
7
7
 
8
8
  def message
9
- to_notification.message
9
+ { id: id, read: !read_at.nil? }.merge(to_notification.message)
10
+ end
11
+
12
+ def self.messages(notifications)
13
+ notifications.map(&:message)
10
14
  end
11
15
  end
12
16
  end
@@ -2,6 +2,7 @@ module Cats
2
2
  module Core
3
3
  class Role < ApplicationRecord
4
4
  belongs_to :application_module
5
+ has_and_belongs_to_many :users, join_table: :cats_core_users_cats_core_roles
5
6
  has_many :role_menus
6
7
 
7
8
  validates :name, presence: true
@@ -14,6 +14,9 @@ module Cats
14
14
 
15
15
  errors.add(:base, 'Application module does not match for role and menu')
16
16
  end
17
+
18
+ delegate(:label, to: :menu, prefix: false)
19
+ delegate(:icon, to: :menu, prefix: false)
17
20
  end
18
21
  end
19
22
  end
@@ -11,6 +11,18 @@ module Cats
11
11
  validates :first_name, :last_name, :email, presence: true
12
12
  validates :password, length: { minimum: 6 }
13
13
  validates :email, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
14
+
15
+ def warehouses
16
+ return unless details.key?('warehouses')
17
+
18
+ Cats::Core::Location.where(id: details['warehouses'], location_type: Cats::Core::Location::WAREHOUSE)
19
+ end
20
+
21
+ def stores
22
+ return unless details.key?('stores')
23
+
24
+ Cats::Core::Store.where(id: details['stores'])
25
+ end
14
26
  end
15
27
  end
16
28
  end
@@ -1,9 +1,20 @@
1
1
  # This is a simple notification class which delivers a
2
2
  # message via database delivery method. It requires a
3
- # +msg+ parameter to instantiate, which represents the
3
+ # +body+ and +title+ parameter to instantiate, which represents the
4
4
  # text message to deliver.
5
- class SimpleNotification < Noticed::Base
6
- deliver_by :database
5
+ module Cats
6
+ module Core
7
+ class SimpleNotification < Noticed::Base
8
+ deliver_by :database
7
9
 
8
- param :msg
10
+ param :body
11
+ param :title
12
+
13
+ def message
14
+ title = params[:title]
15
+ body = params[:body]
16
+ { title: title, body: body }
17
+ end
18
+ end
19
+ end
9
20
  end
@@ -0,0 +1,11 @@
1
+ module Cats
2
+ module Core
3
+ class RoleMenuSerializer < ActiveModel::Serializer
4
+ attributes :id, :label, :icon, :description, :menu_items
5
+
6
+ def menu_items
7
+ object.menu.menu_items
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Cats
2
+ module Core
3
+ class MenuService
4
+ def fetch_menu(user)
5
+ user.roles.map(&:role_menus).flatten
6
+ end
7
+ end
8
+ end
9
+ end
data/config/routes.rb CHANGED
@@ -1,5 +1,24 @@
1
1
  Cats::Core::Engine.routes.draw do
2
+ post '/login', controller: :access, action: :login
2
3
  get '/notifications/unread', controller: :notifications, action: :unread
3
-
4
- resources :notifications, only: [:index]
4
+ get '/notifications/read', controller: :notifications, action: :read
5
+
6
+ resources :menus, only: [:index]
7
+ resources :notifications, only: [:index] do
8
+ member do
9
+ post 'mark_as_read', controller: :notifications, action: :mark_as_read
10
+ post 'mark_as_unread', controller: :notifications, action: :mark_as_unread
11
+ end
12
+ end
13
+ resources :roles do
14
+ member do
15
+ get 'users', controller: :roles, action: :users
16
+ end
17
+ end
18
+ resources :users do
19
+ member do
20
+ get 'stores', controller: :users, action: :stores
21
+ get 'warehouses', controller: :users, action: :warehouses
22
+ end
23
+ end
5
24
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.0.7'.freeze
3
+ VERSION = '1.0.11'.freeze
4
4
  end
5
5
  end
data/lib/cats_core.rb CHANGED
@@ -4,3 +4,4 @@ require 'ancestry'
4
4
  require 'rolify'
5
5
  require 'jwt'
6
6
  require 'noticed'
7
+ require 'active_model_serializers'
@@ -1,7 +1,7 @@
1
1
  FactoryBot.define do
2
2
  factory :notification, class: 'Cats::Core::Notification' do
3
3
  recipient factory: :user
4
- type { nil }
4
+ type { 'ExampleNotification' }
5
5
  params { {} }
6
6
  read_at { Date.today }
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.11
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-08-04 00:00:00.000000000 Z
11
+ date: 2021-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_model_serializers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: ancestry
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -202,8 +216,12 @@ files:
202
216
  - MIT-LICENSE
203
217
  - README.md
204
218
  - Rakefile
219
+ - app/controllers/cats/core/access_controller.rb
205
220
  - app/controllers/cats/core/application_controller.rb
221
+ - app/controllers/cats/core/menus_controller.rb
206
222
  - app/controllers/cats/core/notifications_controller.rb
223
+ - app/controllers/cats/core/roles_controller.rb
224
+ - app/controllers/cats/core/users_controller.rb
207
225
  - app/jobs/cats/core/application_job.rb
208
226
  - app/models/cats/core/application_module.rb
209
227
  - app/models/cats/core/application_record.rb
@@ -228,6 +246,8 @@ files:
228
246
  - app/models/cats/core/way_bill.rb
229
247
  - app/models/cats/core/way_bill_item.rb
230
248
  - app/notifications/cats/core/simple_notification.rb
249
+ - app/serializers/cats/core/role_menu_serializer.rb
250
+ - app/services/cats/core/menu_service.rb
231
251
  - app/services/cats/core/notification_service.rb
232
252
  - app/services/cats/core/token_auth_service.rb
233
253
  - config/routes.rb