cats_core 1.1.16 → 1.1.20

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: b5e9151667e330955db4a89a8bf0bdd3d00c297859866ed57aeb763cbcbc48d4
4
- data.tar.gz: 903b632addec26d908e48b14b06773fc9757b247c50ea8598dd0932a0714ad8f
3
+ metadata.gz: a82354115851db50157d1387858c1b10b65c84fcbf4b78b80f7cbc51e308b6c1
4
+ data.tar.gz: 9810991a8731b4850e3950f1ba0245c49101b65adbb7f5ffc7ec8e7db6fb7cf4
5
5
  SHA512:
6
- metadata.gz: aab3a1d980859f6660fa0dc49c8d13cf41beb586ea0a74599e2ff94a3ec7be22fe21c65b039c706b37bd79b8b9c0827abe723f59d978a10628e03ad7b26f5d9a
7
- data.tar.gz: 6ddf1fbdccd9781098d4ac2f9d3bb42b89d2129bd620c194da91c6508302a19da84aafe3ed2036adee6df36259914cb7e3fa3773c7383cc63fd953436ade2497
6
+ metadata.gz: 75344722c714a1ab8d5b2c82ba808977f88aa141fe83e03520e92d4ccde316819932110693ac78d0523ce78e2f69417e9cb44e54b12045fc4f66870b6ccfa281
7
+ data.tar.gz: 855c87c77fa0c3517e72dc87930c959f86204ef52c46a800d53d67e2adcdd0bdd9a62251355a334707f9e0bd9f26910cde0fe895a75e69d0d7bd910fc345c77a
@@ -8,6 +8,29 @@ module Cats
8
8
  render json: { success: true, data: serialize(receipts) }
9
9
  end
10
10
 
11
+ def create
12
+ p = model_params
13
+ receipt = Cats::Core::Receipt.find_by(
14
+ dispatch_id: p[:dispatch_id],
15
+ commodity_status: p[:commodity_status]
16
+ )
17
+ if receipt
18
+ receipt.quantity += p[:quantity]
19
+ if receipt.save
20
+ render json: { success: true, data: serialize(receipt) }
21
+ else
22
+ render json: { success: false, error: receipt.errors.full_messages[0] }, status: :unprocessable_entity
23
+ end
24
+ else
25
+ obj = Cats::Core::Receipt.new(model_params)
26
+ if obj.save
27
+ render json: { success: true, data: serialize(obj) }, status: :created
28
+ else
29
+ render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
30
+ end
31
+ end
32
+ end
33
+
11
34
  def start_stacking
12
35
  receipt = Cats::Core::Receipt.find(params[:id])
13
36
  service = ReceiptService.new
@@ -1,6 +1,8 @@
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
 
6
8
  def users
@@ -13,6 +15,10 @@ module Cats
13
15
  def set_role
14
16
  @role = Role.find_by(name: params[:name])
15
17
  end
18
+
19
+ def model_params
20
+ params.require(:payload).permit(:name, :application_module_id)
21
+ end
16
22
  end
17
23
  end
18
24
  end
@@ -1,7 +1,27 @@
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[roles assign_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 roles
15
+ data = ActiveModelSerializers::SerializableResource.new(@user.roles)
16
+ render json: { success: true, data: data }
17
+ end
18
+
19
+ def assign_role
20
+ role = Cats::Core::Role.find(params[:role_id])
21
+ @user.roles << role
22
+ data = ActiveModelSerializers::SerializableResource.new(@user)
23
+ render json: { success: true, data: data }
24
+ end
5
25
 
6
26
  def stores
7
27
  data = ActiveModelSerializers::SerializableResource.new(@user.stores)
@@ -21,7 +41,14 @@ module Cats
21
41
  private
22
42
 
23
43
  def set_user
24
- @user = User.find(params[:id])
44
+ @user = Cats::Core::User.find(params[:id])
45
+ end
46
+
47
+ def model_params
48
+ params.require(:payload).permit(
49
+ :first_name, :last_name, :email, :phone_number, :active, :password, :password_confirmation, :details,
50
+ :application_module_id
51
+ )
25
52
  end
26
53
  end
27
54
  end
@@ -12,7 +12,7 @@ module Cats
12
12
  delegate(:name, to: :commodity_category, prefix: true)
13
13
  delegate(:abbreviation, to: :unit_of_measure, prefix: true)
14
14
 
15
- before_save :set_commodity_category
15
+ before_validation :set_commodity_category
16
16
  after_create :update_donation
17
17
 
18
18
  def update_donation
@@ -0,0 +1,27 @@
1
+ module Cats
2
+ module Core
3
+ class DispatchNotification < Noticed::Base
4
+ deliver_by :database
5
+
6
+ param :dispatch
7
+
8
+ def message
9
+ dispatch = params[:dispatch]
10
+ title = 'Dispatch Notification'
11
+ date = Date.today
12
+ body = <<~BODY
13
+ Commodity with the following details has been dispatched to you:
14
+ Dispatch Ref. = #{dispatch.reference_no}
15
+ Batch No. = #{dispatch.allocation_item.allocation.commodity.batch_no}
16
+ Commodity = #{dispatch.allocation_item.allocation.commodity.name}
17
+ Allocated Quantity = #{dispatch.allocation_item.quantity}
18
+ Quantity = #{dispatch.quantity}
19
+ Truck Plate No. = #{dispatch.plate_no}
20
+ Driver Name = #{dispatch.driver_name}
21
+ Driver Phone = #{dispatch.driver_phone}
22
+ BODY
23
+ { title: title, date: date, body: body }
24
+ end
25
+ end
26
+ end
27
+ 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
@@ -109,6 +109,7 @@ module Cats
109
109
  dispatch.save
110
110
  transactions = Cats::Core::DispatchTransaction.where(destination: dispatch)
111
111
  transactions.each(&:commit)
112
+ send_notification(dispatch)
112
113
  dispatch
113
114
  end
114
115
 
@@ -131,6 +132,25 @@ module Cats
131
132
 
132
133
  dispatch
133
134
  end
135
+
136
+ def send_notification(dispatch)
137
+ notification_rule = Cats::Core::NotificationRule.find_by(code: 'dispatch')
138
+ raise(StandardError, 'Notification rule not found for dispatch notification.') unless notification_rule
139
+
140
+ users = Cats::Core::User.joins(:roles).where(cats_core_roles: { name: notification_rule.roles })
141
+ location_id = dispatch.allocation_item.destination_id
142
+ recipients = users.map do |user|
143
+ details = user.details
144
+ if (details.key?('warehouse') && details['warehouse'] == location_id) ||
145
+ (details.key?('hub') && details['hub'] == location_id)
146
+ user
147
+ end
148
+ end.compact
149
+ return if recipients.empty?
150
+
151
+ notification = DispatchNotification.with(dispatch: dispatch)
152
+ notification.deliver(recipients)
153
+ end
134
154
  end
135
155
  end
136
156
  end
data/config/routes.rb CHANGED
@@ -5,6 +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
+ resources :application_modules, param: :prefix do
9
+ member do
10
+ get 'users', controller: :users, action: :index
11
+ end
12
+ end
13
+
8
14
  resources :menus, only: [:index]
9
15
  resources :notifications, only: [:index] do
10
16
  member do
@@ -12,6 +18,8 @@ Cats::Core::Engine.routes.draw do
12
18
  post 'mark_as_unread', controller: :notifications, action: :mark_as_unread
13
19
  end
14
20
  end
21
+
22
+ get '/roles/:id/', controller: :roles, action: :show
15
23
  resources :roles, param: :name do
16
24
  member do
17
25
  get 'users', controller: :roles, action: :users
@@ -19,9 +27,11 @@ Cats::Core::Engine.routes.draw do
19
27
  end
20
28
  resources :users do
21
29
  member do
30
+ get 'roles'
22
31
  get 'stores', controller: :users, action: :stores
23
32
  get 'warehouse', controller: :users, action: :warehouse
24
33
  get 'hub', controller: :users, action: :hub
34
+ post 'assign_role'
25
35
  end
26
36
  end
27
37
 
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.1.16'.freeze
3
+ VERSION = '1.1.20'.freeze
4
4
  end
5
5
  end
@@ -20,4 +20,9 @@ FactoryBot.define do
20
20
  location_type { Cats::Core::Location::WOREDA }
21
21
  parent
22
22
  end
23
+
24
+ factory :zone, parent: :location, class: 'Cats::Core::Location' do
25
+ location_type { Cats::Core::Location::ZONE }
26
+ parent
27
+ end
23
28
  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.16
4
+ version: 1.1.20
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-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -286,6 +286,7 @@ files:
286
286
  - app/models/cats/core/transporter.rb
287
287
  - app/models/cats/core/unit_of_measure.rb
288
288
  - app/models/cats/core/user.rb
289
+ - app/notifications/cats/core/dispatch_notification.rb
289
290
  - app/notifications/cats/core/simple_notification.rb
290
291
  - app/serializers/cats/core/commodity_category_serializer.rb
291
292
  - app/serializers/cats/core/commodity_serializer.rb
@@ -296,7 +297,9 @@ files:
296
297
  - app/serializers/cats/core/receipt_serializer.rb
297
298
  - app/serializers/cats/core/receipt_transaction_serializer.rb
298
299
  - app/serializers/cats/core/role_menu_serializer.rb
300
+ - app/serializers/cats/core/role_serializer.rb
299
301
  - app/serializers/cats/core/unit_of_measure_serializer.rb
302
+ - app/serializers/cats/core/user_serializer.rb
300
303
  - app/services/cats/core/dispatch_service.rb
301
304
  - app/services/cats/core/menu_service.rb
302
305
  - app/services/cats/core/notification_service.rb