cats_core 1.0.5 → 1.0.9

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: b6ff0706b8636cf94fea0714f73ed8fead005d08a2647f5365f43ac7ba6a53b6
4
- data.tar.gz: ab055e9fc942f99eed1e28ab7a755e0c5f54098a854705cf46c6c9bc880d0cff
3
+ metadata.gz: 90bb7ac6c5367ab8fd22ede292563557c104607222408c8ed5ed94a32b8a82e2
4
+ data.tar.gz: b4fb9a29a7a65ce4036b361102bde71f756247b0ff37762543fe94e8fc94a06c
5
5
  SHA512:
6
- metadata.gz: 82950facfa04e42a6aa4e934bfe9f950c158db65dbf55e61c6aff04ad2c9041354e2560a61222817864c5cd8288649282e099f362e7014f7abf65d3ba628a441
7
- data.tar.gz: 8e951eebc249a57a1868dbc9155c1818bfc791ef6a0c987aaccf203c34e2d9fab7a3598b138c70de6e6c5f910cd8d2970e838c9719198f9ccd1d6380b9989143
6
+ metadata.gz: c8c6c949827ee51f1465507caa6301a49af21d47ed2acbf478cb20902733079d1fb630438a9dad3b04df4a1002d1bb9b1e07c5af6c299d250e7d4b11775e045e
7
+ data.tar.gz: abdb8edd5e5bc49d7b954bd37ad23c22081b8d68d4d71fb9def9af20e45bff3ee094e355bc95a17f6ce91676c6fa0832f3d2a9a8767d17e1fdd04f5e5c942a34
@@ -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,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
@@ -12,6 +12,7 @@ module Cats
12
12
  belongs_to :unit_of_measure
13
13
 
14
14
  validates :quantity, presence: true, numericality: { greater_than: 0 }
15
+ validates :volume_per_metric_ton, numericality: { greater_than: 0, allow_nil: true }
15
16
  end
16
17
  end
17
18
  end
@@ -4,6 +4,14 @@ module Cats
4
4
  include Noticed::Model
5
5
 
6
6
  belongs_to :recipient, polymorphic: true
7
+
8
+ def message
9
+ { id: id, read: !read_at.nil? }.merge(to_notification.message)
10
+ end
11
+
12
+ def self.messages(notifications)
13
+ notifications.map(&:message)
14
+ end
7
15
  end
8
16
  end
9
17
  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
@@ -37,7 +37,18 @@ module Cats
37
37
  end
38
38
 
39
39
  def stacking_rules
40
- rule ||= Cats::Core::StackingRule.first
40
+ rule = Cats::Core::StackingRule.first
41
+
42
+ # If no rule is defined, then define a default rule
43
+ rule ||= Cats::Core::StackingRule.new(
44
+ space_between_stack: 1,
45
+ distance_from_gangway: 2,
46
+ distance_from_ceiling: 1,
47
+ maximum_height: 5,
48
+ maximum_length: 5,
49
+ maximum_width: 5,
50
+ distance_from_wall: 1
51
+ )
41
52
  rule
42
53
  end
43
54
 
@@ -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
@@ -25,16 +25,20 @@ module Cats
25
25
  NOTIFICATION_RULES
26
26
  end
27
27
 
28
+ def create_notifier(rule)
29
+ clazz = rule[:notification].constantize
30
+ clazz.with(**@params)
31
+ end
32
+
28
33
  def notify
29
- rules = notification_rules
30
- clazz = rules[@code][:notification].constantize
31
- app_code = rules[@code][:application]
32
- roles = rules[@code][:recipients]
34
+ rule = notification_rules[@code]
35
+ notifier = create_notifier(rule)
36
+ app_code = rule[:application]
37
+ roles = rule[:recipients]
33
38
 
34
- notification = clazz.with(**@params)
35
39
  users = Cats::Core::User.joins(:application_module)
36
40
  .where(application_module: { prefix: app_code }).with_all_roles(*roles)
37
- notification.deliver(users)
41
+ notifier.deliver(users)
38
42
  end
39
43
  end
40
44
  end
data/config/routes.rb CHANGED
@@ -1,5 +1,23 @@
1
1
  Cats::Core::Engine.routes.draw do
2
+ post '/login', controller: :access, action: :login
2
3
  get '/notifications/unread', controller: :notifications, action: :unread
4
+ get '/notifications/read', controller: :notifications, action: :read
3
5
 
4
- resources :notifications, only: [:index]
6
+ resources :notifications, only: [:index] do
7
+ member do
8
+ post 'mark_as_read', controller: :notifications, action: :mark_as_read
9
+ post 'mark_as_unread', controller: :notifications, action: :mark_as_unread
10
+ end
11
+ end
12
+ resources :roles do
13
+ member do
14
+ get 'users', controller: :roles, action: :users
15
+ end
16
+ end
17
+ resources :users do
18
+ member do
19
+ get 'stores', controller: :users, action: :stores
20
+ get 'warehouses', controller: :users, action: :warehouses
21
+ end
22
+ end
5
23
  end
@@ -21,6 +21,7 @@ class CreateCatsCoreCommodities < ActiveRecord::Migration[6.1]
21
21
  t.string :description
22
22
  t.boolean :hazardous, null: false, default: false
23
23
  t.date :best_use_before
24
+ t.float :volume_per_metric_ton
24
25
 
25
26
  t.timestamps
26
27
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.0.5'.freeze
3
+ VERSION = '1.0.9'.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'
@@ -8,5 +8,6 @@ FactoryBot.define do
8
8
  quantity { 100 }
9
9
  hazardous { false }
10
10
  best_use_before { Date.today + 2.month }
11
+ volume_per_metric_ton { 10 }
11
12
  end
12
13
  end
@@ -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.5
4
+ version: 1.0.9
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-07-29 00:00:00.000000000 Z
11
+ date: 2021-08-05 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,11 @@ 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
206
221
  - app/controllers/cats/core/notifications_controller.rb
222
+ - app/controllers/cats/core/roles_controller.rb
223
+ - app/controllers/cats/core/users_controller.rb
207
224
  - app/jobs/cats/core/application_job.rb
208
225
  - app/models/cats/core/application_module.rb
209
226
  - app/models/cats/core/application_record.rb