cats_core 1.4.8 → 1.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/cats/core/access_controller.rb +2 -2
  3. data/app/controllers/cats/core/application_controller.rb +2 -2
  4. data/app/controllers/cats/core/commodities_controller.rb +2 -15
  5. data/app/controllers/cats/core/commodity_categories_controller.rb +8 -39
  6. data/app/controllers/cats/core/dispatch_plan_items_controller.rb +4 -3
  7. data/app/controllers/cats/core/dispatch_plans_controller.rb +3 -3
  8. data/app/controllers/cats/core/dispatch_transactions_controller.rb +3 -2
  9. data/app/controllers/cats/core/dispatches_controller.rb +16 -20
  10. data/app/controllers/cats/core/locations_controller.rb +5 -4
  11. data/app/controllers/cats/core/lost_commodities_controller.rb +3 -2
  12. data/app/controllers/cats/core/monthly_plans_controller.rb +5 -4
  13. data/app/controllers/cats/core/notifications_controller.rb +1 -0
  14. data/app/controllers/cats/core/receipt_transactions_controller.rb +16 -18
  15. data/app/controllers/cats/core/receipts_controller.rb +9 -13
  16. data/app/controllers/cats/core/roles_controller.rb +7 -7
  17. data/app/controllers/cats/core/routes_controller.rb +1 -1
  18. data/app/controllers/cats/core/stacks_controller.rb +3 -2
  19. data/app/controllers/cats/core/transporters_controller.rb +2 -2
  20. data/app/controllers/cats/core/unit_of_measures_controller.rb +1 -37
  21. data/app/controllers/cats/core/users_controller.rb +33 -44
  22. data/app/controllers/concerns/cats/core/common.rb +23 -6
  23. data/app/models/cats/core/commodity.rb +9 -0
  24. data/app/models/cats/core/transport_requisition.rb +1 -1
  25. data/lib/cats/core/version.rb +1 -1
  26. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 626e9d30bfefd23124b2110a3b10c4a6dcf58ac6aeb660b50803843ececd2517
4
- data.tar.gz: e59fa35f07c89486a5840e613389dd75ed6613077cb0ae3648295134dbff7ffe
3
+ metadata.gz: a1eacca23e0a10e3a707dcc45f51e2c44013fccb7a35182f3d7476c95d0bcad8
4
+ data.tar.gz: bcbcaf4bda959f028521528e8e7c979bca422037c9e4abb913449f831cadc73b
5
5
  SHA512:
6
- metadata.gz: 440d92d2388b2816b1c76b0040d4c6d2a2fe0588b94e4ba134be56058cb74344ad55510e074a9d87ee04b1bf5336a705dd98bc7de5facc7250f3638f17e9ddfd
7
- data.tar.gz: f3adb1d2cc5edd7cf7baf0eeb4b9624602f0987bea3cb5d1b7700af7b894ac43143fc2c0108daed5622e55e5581fc789ae97bd073a2246fdd02836bcb060784a
6
+ metadata.gz: b34d8901501515edaa28a7aea773ccea4156f079124be80200ab77673f22357e3f1e7891e667244ec8ffcc11a6310a5a575a36ab549468dc292fc7ad5bc868b5
7
+ data.tar.gz: bc9c7ea903b2cfaf05db1819391c0c5b2d2999498b3ee488263c240bc2971a67fe85b24911afd1432ef7aef81bc6b6f473cfc0ba34516839a93cc69b06571ad1
@@ -4,7 +4,7 @@ module Cats
4
4
  skip_before_action :authenticate, only: [:login]
5
5
 
6
6
  def login
7
- user = Cats::Core::User.find_by(email: auth_params[:email])
7
+ user = User.find_by(email: auth_params[:email])
8
8
  if user
9
9
  if user.authenticate(auth_params[:password])
10
10
  roles = user.roles.map(&:name)
@@ -18,7 +18,7 @@ module Cats
18
18
  id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles,
19
19
  details: user.details
20
20
  }
21
- jwt = Cats::Core::TokenAuthService.issue(payload)
21
+ jwt = TokenAuthService.issue(payload)
22
22
  render json: { token: jwt, user: payload }
23
23
  else
24
24
  render json: { error: 'Invalid password.' }, status: 400
@@ -6,7 +6,7 @@ module Cats
6
6
  def current_user
7
7
  return if token.nil?
8
8
 
9
- user = Cats::Core::User.find(auth['id'])
9
+ user = User.find(auth['id'])
10
10
  @current_user ||= user
11
11
  end
12
12
 
@@ -27,7 +27,7 @@ module Cats
27
27
  end
28
28
 
29
29
  def auth
30
- Cats::Core::TokenAuthService.decode(token)
30
+ TokenAuthService.decode(token)
31
31
  end
32
32
  end
33
33
  end
@@ -4,28 +4,15 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def approve
7
- commodity = Cats::Core::Commodity.find(params[:id])
7
+ commodity = Commodity.find(params[:id])
8
8
  result = commodity.approve
9
9
  render json: { success: result, data: serialize(commodity) }
10
10
  rescue StandardError => e
11
11
  render json: { success: false, error: e.message }
12
12
  end
13
13
 
14
- def update
15
- if @obj.status == Cats::Core::Commodity::APPROVED
16
- render json: { success: false, error: 'An approved record cannot be edited.' }
17
- return
18
- end
19
-
20
- if @obj.update(model_params)
21
- render json: { success: true, data: serialize(@obj) }
22
- else
23
- render json: { success: false, error: @obj.errors.full_messages[0] }, status: :unprocessable_entity
24
- end
25
- end
26
-
27
14
  def filter
28
- query = Cats::Core::Commodity.ransack(params[:q])
15
+ query = Commodity.ransack(params[:q])
29
16
  render json: { success: true, data: serialize(query.result) }
30
17
  end
31
18
 
@@ -1,62 +1,31 @@
1
1
  module Cats
2
2
  module Core
3
3
  class CommodityCategoriesController < ApplicationController
4
- before_action :set_commodity_category, only: %i[show update]
4
+ include Common
5
5
 
6
6
  def index
7
- commodity_categories = Cats::Core::CommodityCategory.all.reject(&:has_children?)
8
- data = ActiveModelSerializers::SerializableResource.new(commodity_categories)
9
- render json: { success: true, data: data }
7
+ super do
8
+ CommodityCategory.all.reject(&:has_children?)
9
+ end
10
10
  end
11
11
 
12
12
  def all
13
- data = Cats::Core::CommodityCategory.all
13
+ data = CommodityCategory.all
14
14
  render json: { success: true, data: data }
15
15
  end
16
16
 
17
17
  def root
18
- data = Cats::Core::CommodityCategory.roots
18
+ data = CommodityCategory.roots
19
19
  render json: { success: true, data: serialize(data) }
20
20
  end
21
21
 
22
- def show
23
- data = ActiveModelSerializers::SerializableResource.new(@commodity_category)
24
- render json: { success: true, data: data }
25
- end
26
-
27
22
  def children
28
- parent = Cats::Core::CommodityCategory.find(params[:id])
29
- data = ActiveModelSerializers::SerializableResource.new(parent.children)
30
- render json: { success: true, data: data }
31
- end
32
-
33
- def create
34
- obj = Cats::Core::CommodityCategory.new(model_params)
35
- if obj.save
36
- data = ActiveModelSerializers::SerializableResource.new(obj)
37
- render json: { success: true, data: data }, status: :created
38
- else
39
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
40
- end
41
- end
42
-
43
- def update
44
- if @commodity_category.update(model_params)
45
- data = ActiveModelSerializers::SerializableResource.new(@commodity_category)
46
- render json: { success: true, data: data }
47
- else
48
- render json: {
49
- success: false, error: @commodity_category.errors.full_messages[0]
50
- }, status: :unprocessable_entity
51
- end
23
+ parent = CommodityCategory.find(params[:id])
24
+ render json: { success: true, data: serialize(parent.children) }
52
25
  end
53
26
 
54
27
  private
55
28
 
56
- def set_commodity_category
57
- @commodity_category = Cats::Core::CommodityCategory.find(params[:id])
58
- end
59
-
60
29
  def model_params
61
30
  params.require('payload').permit(:code, :name, :description, :parent_id)
62
31
  end
@@ -4,12 +4,13 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- items = Cats::Core::DispatchPlanItem.where(dispatch_plan_id: params[:id])
8
- render json: { success: true, data: serialize(items) }
7
+ super do
8
+ DispatchPlanItem.where(dispatch_plan_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def filter
12
- query = Cats::Core::DispatchPlanItem.ransack(params[:q])
13
+ query = DispatchPlanItem.ransack(params[:q])
13
14
  render json: { success: true, data: serialize(query.result) }
14
15
  end
15
16
 
@@ -4,12 +4,12 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def plans_for_user
7
- plans = Cats::Core::DispatchPlan.where(prepared_by: current_user)
7
+ plans = DispatchPlan.where(prepared_by: current_user)
8
8
  render json: { success: true, data: serialize(plans) }
9
9
  end
10
10
 
11
11
  def filter
12
- query = Cats::Core::DispatchPlan.ransack(params[:q])
12
+ query = DispatchPlan.ransack(params[:q])
13
13
  render json: { success: true, data: serialize(query.result) }
14
14
  end
15
15
 
@@ -23,7 +23,7 @@ module Cats
23
23
 
24
24
  def approve
25
25
  service = DispatchPlanService.new
26
- plan = Cats::Core::DispatchPlan.find(params[:id])
26
+ plan = DispatchPlan.find(params[:id])
27
27
  plan = service.approve(plan)
28
28
  render json: { success: true, data: serialize(plan) }
29
29
  rescue StandardError => e
@@ -4,8 +4,9 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- transactions = Cats::Core::DispatchTransaction.where(dispatch_id: params[:id])
8
- render json: { success: true, data: serialize(transactions) }
7
+ super do
8
+ DispatchTransaction.where(dispatch_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  # This end-point is used to create upstream dispatch transactions.
@@ -4,41 +4,41 @@ module Cats
4
4
  include Common
5
5
 
6
6
  before_action :set_service, only: %i[create_receipt_authorization approve start search confirm]
7
- before_action :set_dispatch, only: %i[approve start confirm commodity]
8
7
 
9
8
  def index
10
- dispatches = Cats::Core::Dispatch.where(dispatch_plan_item_id: params[:id])
11
- render json: { success: true, data: serialize(dispatches) }
9
+ super do
10
+ Dispatch.where(dispatch_plan_item_id: params[:id])
11
+ end
12
12
  end
13
13
 
14
14
  def create
15
- dispatch = Cats::Core::Dispatch.new(model_params)
16
- dispatch.dispatch_status = Cats::Core::Dispatch::DRAFT
17
- dispatch.prepared_by = current_user
18
- if dispatch.save
19
- render json: { success: true, data: serialize(dispatch) }, status: :created
20
- else
21
- render json: { success: false, error: dispatch.errors.full_messages[0] }, status: :unprocessable_entity
15
+ super do
16
+ dispatch = Dispatch.new(model_params)
17
+ dispatch.dispatch_status = Dispatch::DRAFT
18
+ dispatch.prepared_by = current_user
19
+ dispatch
22
20
  end
23
21
  end
24
22
 
25
23
  def approve
26
- @dispatch.approve
27
- render json: { success: true, data: serialize(@dispatch) }
24
+ dispatch = set_object
25
+ dispatch.approve
26
+ render json: { success: true, data: serialize(dispatch) }
28
27
  rescue StandardError => e
29
28
  render json: { success: false, error: e.message }
30
29
  end
31
30
 
32
31
  def start
33
- dispatch = @service.start(@dispatch)
32
+ dispatch = @service.start(set_object)
34
33
  render json: { success: true, data: serialize(dispatch) }
35
34
  rescue StandardError => e
36
35
  render json: { success: false, error: e.message }
37
36
  end
38
37
 
39
38
  def confirm
40
- @dispatch.confirm
41
- render json: { success: true, data: serialize(@dispatch) }
39
+ dispatch = set_object
40
+ dispatch.confirm
41
+ render json: { success: true, data: serialize(dispatch) }
42
42
  rescue StandardError => e
43
43
  render json: { success: false, error: e.message }
44
44
  end
@@ -49,7 +49,7 @@ module Cats
49
49
  end
50
50
 
51
51
  def commodity
52
- data = @dispatch.dispatch_plan_item.commodity
52
+ data = set_object.dispatch_plan_item.commodity
53
53
  render json: { success: true, data: serialize(data) }
54
54
  end
55
55
 
@@ -59,10 +59,6 @@ module Cats
59
59
  @service = DispatchService.new
60
60
  end
61
61
 
62
- def set_dispatch
63
- @dispatch = Cats::Core::Dispatch.find(params[:id])
64
- end
65
-
66
62
  def model_params
67
63
  params.require(:payload).permit(:reference_no, :dispatch_plan_item_id, :transporter_id, :plate_no,
68
64
  :driver_name, :driver_phone, :quantity, :remark)
@@ -4,17 +4,18 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- locations = Cats::Core::Location.where(location_type: params[:location_type])
8
- render json: { success: true, data: serialize(locations) }
7
+ super do
8
+ Location.where(location_type: params[:location_type])
9
+ end
9
10
  end
10
11
 
11
12
  def children
12
- parent = Cats::Core::Location.find(params[:id])
13
+ parent = Location.find(params[:id])
13
14
  render json: { success: true, data: serialize(parent.children) }
14
15
  end
15
16
 
16
17
  def filter
17
- query = Cats::Core::Location.ransack(params[:q])
18
+ query = Location.ransack(params[:q])
18
19
  render json: { success: true, data: serialize(query.result) }
19
20
  end
20
21
 
@@ -4,8 +4,9 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- data = LostCommodity.where(dispatch_id: params[:id])
8
- render json: { success: true, data: serialize(data) }
7
+ super do
8
+ LostCommodity.where(dispatch_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def model_params
@@ -6,17 +6,18 @@ module Cats
6
6
  before_action :set_service, only: %i[generate remove_items generate_monthly_needs]
7
7
 
8
8
  def index
9
- plans = Cats::Core::MonthlyPlan.where(plan: params[:id])
10
- render json: { success: true, data: serialize(plans) }
9
+ super do
10
+ MonthlyPlan.where(plan: params[:id])
11
+ end
11
12
  end
12
13
 
13
14
  def filter
14
- query = Cats::Core::MonthlyPlan.ransack(params[:q])
15
+ query = MonthlyPlan.ransack(params[:q])
15
16
  render json: { success: true, data: serialize(query.result) }
16
17
  end
17
18
 
18
19
  def approve
19
- plan = Cats::Core::MonthlyPlan.find(params[:id])
20
+ plan = MonthlyPlan.find(params[:id])
20
21
  plan.approve
21
22
  render json: { success: true, data: serialize(plan) }
22
23
  rescue StandardError => e
@@ -2,6 +2,7 @@ module Cats
2
2
  module Core
3
3
  class NotificationsController < ApplicationController
4
4
  before_action :set_notification, only: %i[mark_as_read mark_as_unread]
5
+
5
6
  def index
6
7
  data = Notification.messages(current_user.notifications.newest_first)
7
8
  render json: { success: true, data: data }
@@ -4,28 +4,26 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- transactions = Cats::Core::ReceiptTransaction.where(receipt_id: params[:id])
8
- render json: { success: true, data: serialize(transactions) }
7
+ super do
8
+ ReceiptTransaction.where(receipt_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def create
12
- p = model_params
13
-
14
- # Look for a transaction with the same destination as incoming
15
- transaction = Cats::Core::ReceiptTransaction.find_by(
16
- receipt_id: p[:receipt_id],
17
- destination_id: p[:destination_id]
18
- )
19
- if transaction
20
- transaction.quantity += p[:quantity]
21
- else
22
- transaction = Cats::Core::ReceiptTransaction.new(p)
23
- end
13
+ super do
14
+ p = model_params
24
15
 
25
- if transaction.save
26
- render json: { success: true, data: serialize(transaction) }, status: :created
27
- else
28
- render json: { success: false, error: transaction.errors.full_messages[0] }, status: :unprocessable_entity
16
+ # Look for a transaction with the same destination as incoming
17
+ transaction = ReceiptTransaction.find_by(
18
+ receipt_id: p[:receipt_id],
19
+ destination_id: p[:destination_id]
20
+ )
21
+ if transaction
22
+ transaction.quantity += p[:quantity]
23
+ else
24
+ transaction = ReceiptTransaction.new(p)
25
+ end
26
+ transaction
29
27
  end
30
28
  end
31
29
 
@@ -4,25 +4,21 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- receipts = Cats::Core::Receipt.where(dispatch_id: params[:id])
8
- render json: { success: true, data: serialize(receipts) }
7
+ super do
8
+ Receipt.where(dispatch_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def create
12
- service = ReceiptService.new
13
- receipt = service.init(model_params)
14
-
15
- if receipt.save
16
- render json: { success: true, data: serialize(receipt) }
17
- else
18
- render json: { success: false, error: receipt.errors.full_messages[0] }, status: :unprocessable_entity
13
+ super do
14
+ service = ReceiptService.new
15
+ receipt = service.init(model_params)
16
+ receipt
19
17
  end
20
- rescue StandardError => e
21
- render json: { success: false, error: e.message }
22
18
  end
23
19
 
24
20
  def start_stacking
25
- receipt = Cats::Core::Receipt.find(params[:id])
21
+ receipt = Receipt.find(params[:id])
26
22
  service = ReceiptService.new
27
23
  result = service.start_stacking(receipt)
28
24
  render json: { success: true, data: serialize(result) }
@@ -31,7 +27,7 @@ module Cats
31
27
  end
32
28
 
33
29
  def finish_stacking
34
- receipt = Cats::Core::Receipt.find(params[:id])
30
+ receipt = Receipt.find(params[:id])
35
31
  service = ReceiptService.new
36
32
  result = service.finish_stacking(receipt)
37
33
  render json: { success: true, data: serialize(result) }
@@ -6,21 +6,21 @@ module Cats
6
6
  before_action :set_role, only: [:users]
7
7
 
8
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) }
9
+ super do
10
+ Role.joins(:application_module)
11
+ .where(cats_core_application_modules: { prefix: params[:prefix] })
12
+ end
12
13
  end
13
14
 
14
15
  def unassigned_roles
15
- user = Cats::Core::User.find(params[:id])
16
- roles = Cats::Core::Role.where(application_module_id: user.application_module_id)
16
+ user = User.find(params[:id])
17
+ roles = Role.where(application_module_id: user.application_module_id)
17
18
  unassigned = roles - user.roles
18
19
  render json: { success: true, data: serialize(unassigned) }
19
20
  end
20
21
 
21
22
  def users
22
- data = ActiveModelSerializers::SerializableResource.new(@role.users)
23
- render json: { success: true, data: data }
23
+ render json: { success: true, data: serialize(@role.users) }
24
24
  end
25
25
 
26
26
  private
@@ -4,7 +4,7 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def filter
7
- query = Cats::Core::Route.ransack(params[:q])
7
+ query = Route.ransack(params[:q])
8
8
  render json: { success: true, data: serialize(query.result) }
9
9
  end
10
10
 
@@ -4,8 +4,9 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- stacks = Stack.where(store_id: params[:id])
8
- render json: { success: true, data: serialize(stacks) }
7
+ super do
8
+ Stack.where(store_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def filter
@@ -1,10 +1,10 @@
1
1
  module Cats
2
2
  module Core
3
3
  class TransportersController < ApplicationController
4
- include Cats::Core::Common
4
+ include Common
5
5
 
6
6
  def filter
7
- query = Cats::Core::Transporter.ransack(params[:q])
7
+ query = Transporter.ransack(params[:q])
8
8
  render json: { success: true, data: serialize(query.result) }
9
9
  end
10
10
 
@@ -1,46 +1,10 @@
1
1
  module Cats
2
2
  module Core
3
3
  class UnitOfMeasuresController < ApplicationController
4
- before_action :set_unit_of_measure, only: %i[show update]
5
-
6
- def index
7
- data = ActiveModelSerializers::SerializableResource.new(Cats::Core::UnitOfMeasure.all)
8
- render json: { success: true, data: data }
9
- end
10
-
11
- def show
12
- data = ActiveModelSerializers::SerializableResource.new(@unit_of_measure)
13
- render json: { success: true, data: data }
14
- end
15
-
16
- def create
17
- obj = Cats::Core::UnitOfMeasure.new(model_params)
18
- if obj.save
19
- data = ActiveModelSerializers::SerializableResource.new(obj)
20
- render json: { success: true, data: data }, status: :created
21
- else
22
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
23
- end
24
- end
25
-
26
- def update
27
- if @unit_of_measure.update(model_params)
28
- data = ActiveModelSerializers::SerializableResource.new(@unit_of_measure)
29
- render json: { success: true, data: data }
30
- else
31
- render json: {
32
- success: false,
33
- error: @unit_of_measure.errors.full_messages[0]
34
- }, status: :unprocessable_entity
35
- end
36
- end
4
+ include Common
37
5
 
38
6
  private
39
7
 
40
- def set_unit_of_measure
41
- @unit_of_measure = Cats::Core::UnitOfMeasure.find(params[:id])
42
- end
43
-
44
8
  def model_params
45
9
  params.require(:payload).permit(:abbreviation, :name, :unit_type)
46
10
  end
@@ -3,95 +3,84 @@ module Cats
3
3
  class UsersController < ApplicationController
4
4
  include Common
5
5
 
6
- before_action :set_user, only: %i[update roles assign_role revoke_role stores warehouse hub]
7
-
8
6
  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) }
7
+ super do
8
+ User.joins(:application_module).where(cats_core_application_modules: { prefix: params[:prefix] })
9
+ end
12
10
  end
13
11
 
14
12
  def create
15
- prefix = model_params[:application_prefix]
16
- application_module = Cats::Core::ApplicationModule.find_by(prefix: prefix)
13
+ super do
14
+ prefix = model_params[:application_prefix]
15
+ application_module = ApplicationModule.find_by(prefix: prefix)
17
16
 
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
17
+ obj = User.new(model_params.except(:application_prefix))
18
+ obj.application_module = application_module
19
+ obj
24
20
  end
25
21
  end
26
22
 
27
23
  def update
24
+ user = set_object
28
25
  if model_params[:application_prefix]
29
26
  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
27
+ application_module = ApplicationModule.find_by(prefix: prefix)
28
+ user.assign_attributes(model_params.except(:application_prefix))
29
+ user.application_module = application_module
33
30
  else
34
- @user.assign_attributes(model_params)
31
+ user.assign_attributes(model_params)
35
32
  end
36
33
 
37
- if @user.save
38
- render json: { success: true, data: serialize(@user) }
34
+ if user.save
35
+ render json: { success: true, data: serialize(user) }
39
36
  else
40
- render json: { success: false, error: @user.errors.full_messages[0] }, status: :unprocessable_entity
37
+ render json: { success: false, error: user.errors.full_messages[0] }, status: :unprocessable_entity
41
38
  end
42
39
  end
43
40
 
44
41
  def roles
45
- data = ActiveModelSerializers::SerializableResource.new(@user.roles)
46
- render json: { success: true, data: data }
42
+ render json: { success: true, data: serialize(set_object.roles) }
47
43
  end
48
44
 
49
45
  def assign_role
50
- role = Cats::Core::Role.find(params[:role_id])
51
- @user.roles << role
46
+ role = Role.find(params[:role_id])
47
+ user = set_object
48
+ user.roles << role
52
49
 
53
50
  case role.name
54
51
  when 'warehouse_manager'
55
- @user.add_detail(:warehouse, params[:warehouse_id])
52
+ user.add_detail(:warehouse, params[:warehouse_id])
56
53
  when 'hub_manager'
57
- @user.add_detail(:hub, params[:hub_id])
54
+ user.add_detail(:hub, params[:hub_id])
58
55
  when 'regional_manager'
59
- @user.add_detail(:region, params[:region_id])
56
+ user.add_detail(:region, params[:region_id])
60
57
  end
61
58
 
62
- data = ActiveModelSerializers::SerializableResource.new(role)
63
- render json: { success: true, data: data }
59
+ render json: { success: true, data: serialize(role) }
64
60
  end
65
61
 
66
62
  def revoke_role
67
- role = Cats::Core::Role.find(params[:role_id])
68
- @user.roles.delete(role)
69
- @user.remove_detail(role.name) if %w[warehouse_manager hub_manager regional_manager].include?(role.name)
70
- data = ActiveModelSerializers::SerializableResource.new(role)
71
- render json: { success: true, data: data }
63
+ user = set_object
64
+ role = Role.find(params[:role_id])
65
+ user.roles.delete(role)
66
+ user.remove_detail(role.name) if %w[warehouse_manager hub_manager regional_manager].include?(role.name)
67
+ render json: { success: true, data: serialize(role) }
72
68
  end
73
69
 
74
70
  def stores
75
- data = ActiveModelSerializers::SerializableResource.new(@user.stores)
76
- render json: { success: true, data: data }
71
+ render json: { success: true, data: serialize(set_object.stores) }
77
72
  end
78
73
 
79
74
  def warehouse
80
- data = ActiveModelSerializers::SerializableResource.new(@user.warehouse)
81
- render json: { success: true, data: data }
75
+ render json: { success: true, data: serialize(set_object.warehouse) }
82
76
  end
83
77
 
84
78
  def hub
85
- data = ActiveModelSerializers::SerializableResource.new(@user.hub)
86
- render json: { success: true, data: data }
79
+ render json: { success: true, data: serialize(set_object.hub) }
87
80
  end
88
81
 
89
82
  private
90
83
 
91
- def set_user
92
- @user = Cats::Core::User.find(params[:id])
93
- end
94
-
95
84
  def model_params
96
85
  params.require(:payload).permit(
97
86
  :first_name, :last_name, :email, :phone_number, :active, :password, :password_confirmation, :details,
@@ -9,8 +9,12 @@ module Cats
9
9
  end
10
10
 
11
11
  def index
12
- data = serialize(@clazz.all)
13
- render json: { success: true, data: data }
12
+ data = if block_given?
13
+ yield
14
+ else
15
+ @clazz.all
16
+ end
17
+ render json: { success: true, data: serialize(data) }
14
18
  end
15
19
 
16
20
  def show
@@ -18,20 +22,33 @@ module Cats
18
22
  end
19
23
 
20
24
  def create
21
- obj = @clazz.new(model_params)
25
+ obj = if block_given?
26
+ yield
27
+ else
28
+ @clazz.new(model_params)
29
+ end
22
30
  if obj.save
23
31
  render json: { success: true, data: serialize(obj) }, status: :created
24
32
  else
25
33
  render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
26
34
  end
35
+ rescue StandardError => e
36
+ render json: { success: false, error: e.message }
27
37
  end
28
38
 
29
39
  def update
30
- if @obj.update(model_params)
31
- render json: { success: true, data: serialize(@obj) }
40
+ obj = if block_given?
41
+ yield
42
+ else
43
+ obj = @obj
44
+ end
45
+ if obj.update(model_params)
46
+ render json: { success: true, data: serialize(obj) }
32
47
  else
33
- render json: { success: false, error: @obj.errors.full_messages[0] }, status: :unprocessable_entity
48
+ render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
34
49
  end
50
+ rescue StandardError => e
51
+ render json: { success: false, error: e.message }
35
52
  end
36
53
 
37
54
  private
@@ -37,6 +37,7 @@ module Cats
37
37
  validates :volume_per_metric_ton, numericality: { greater_than: 0, allow_nil: true }
38
38
  validates :arrival_status, presence: true, inclusion: { in: ARRIVAL_STATUSES }
39
39
  validates :status, presence: true, inclusion: { in: STATUSES }
40
+ validate :validate_updateability, on: :update
40
41
 
41
42
  delegate(:abbreviation, to: :unit_of_measure, prefix: 'unit')
42
43
  delegate(:reference_no, to: :source, prefix: true)
@@ -45,6 +46,14 @@ module Cats
45
46
  source.commodity_category.name
46
47
  end
47
48
 
49
+ def validate_updateability
50
+ return unless status
51
+
52
+ return if status_changed?
53
+
54
+ errors.add(:base, 'Commodity cannot be updated because it is in approved state.') if status == APPROVED
55
+ end
56
+
48
57
  def approve
49
58
  raise(StandardError, 'Commodity already approved.') if status == APPROVED
50
59
 
@@ -29,7 +29,7 @@ module Cats
29
29
  raise(StandardError, 'Transport requisition is already approved.') if status == APPROVED
30
30
 
31
31
  if transport_requisition_items.count.zero?
32
- raise(StandardError, 'Transport requisition has no requisition items.')
32
+ raise(StandardError, 'Transport requisition has no requisition items.')
33
33
  end
34
34
 
35
35
  self.status = APPROVED
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.4.8'.freeze
3
+ VERSION = '1.4.9'.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.4.8
4
+ version: 1.4.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: 2022-04-18 00:00:00.000000000 Z
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers